简体   繁体   English

加载动态功能时出错。 应用程式套件Android

[英]Error in Loading Dynamic feature. App Bundle Android

In Android API level, 26 (Oreo) and above I am getting this error. 在Android API级别(26(奥利奥)及以上)中,我遇到此错误。 If I run dynamic feature from android studio direct to the device, the dynamic feature works fine, but when I upload app bundle on play store and try to run dynamic feature from the app which downloaded from play store I am getting 如果我从android studio直接向设备运行动态功能,则动态功能可以正常运行,但是当我在Play商店上载应用捆绑销售商品并尝试从Play商店下载的应用中运行动态功能时,

Runtime Exception: Resources$ NotFoundException on every resource (string.xml, atrr.xml etc). 运行时异常:每个资源(string.xml,atrr.xml等)上的Resources $ NotFoundException。

Below Android API level, 26 its working fine. 在Android API级别以下,其26正常工作。

Error Log: 错误日志:

2018-08-27 18:23:41.097 20926-20926/? 2018-08-27 18:23:41.097 20926-20926 /? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ak.ta.dainikbhaskar.activity.release, PID: 20926 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ak.ta.dainikbhaskar.activity.release/com.dainik.bhaskar.fitness.activities.EntryActivity}: android.content.res.Resources$NotFoundException: String resource ID E / AndroidRuntime:致命例外:主进程:com.ak.ta.dainikbhaskar.activity.release,PID:20926 java.lang.RuntimeException:无法启动活动ComponentInfo {com.ak.ta.dainikbhaskar.activity.release / com .dainik.bhaskar.fitness.activities.EntryActivity}:android.content.res.Resources $ NotFoundException:字符串资源ID

0x7e0b000e 0x7e0b000e

  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2792) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:172) at android.app.ActivityThread.main(ActivityThread.java:6590) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7e0b000e at android.content.res.Resources.getText(Resources.java:339) at android.content.res.Resources.getString(Resources.java:433) at android.content.Context.getString(Context.java:556) at com.dainik.bhaskar.fitness.activities.base.BaseActivity.onCreate(BaseActivity.java:34) at com.dainik.bhaskar.fitness.activities.EntryActivity.onCreate(EntryActivity.java:31) at android.app.Activity.performCreate(Activity.java:7023) at android.app.Activity.performCreate(Activity.java:7014) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:172) at android.app.ActivityThread.main(ActivityThread.java:6590) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

code: 码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

SplitInstallManager installManager;
String EpaperModuleName = "epaper_dynamic_lib";
private final String EPaperLauncherclassName = "com.bhaskar.epaper.ui.SplashActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    (findViewById(R.id.btn_epaper)).setOnClickListener(this);
    installManager = SplitInstallManagerFactory.create(this);
    installManager.registerListener(splitInstallStateUpdatedListener);

}


private void loadAndLaunchModule(String moduleName, String launcherclassName) {
    // Skip loading if the module already is installed. Perform success action directly.
    if (installManager.getInstalledModules().contains(moduleName)) {
        Toast.makeText(getApplicationContext(), "onSuccessfulLoad " + moduleName, Toast.LENGTH_LONG).show();
        onSuccessfulLoad(moduleName, launcherclassName, true);
        return;
    }

    // Create request to install a feature module by name.
    SplitInstallRequest request = SplitInstallRequest.newBuilder()
            .addModule(moduleName)
            .build();

    // Load and install the requested feature module.
    installManager.startInstall(request);
    Toast.makeText(getApplicationContext(), "startInstall " + moduleName, Toast.LENGTH_LONG).show();
}

private final SplitInstallStateUpdatedListener splitInstallStateUpdatedListener = new SplitInstallStateUpdatedListener() {
    @Override
    public void onStateUpdate(SplitInstallSessionState splitInstallSessionState) {
        List<String> splitModules = splitInstallSessionState.moduleNames();

        boolean multiInstall = splitModules.size() > 1;
        Toast.makeText(getApplicationContext(), "multiInstall " + multiInstall, Toast.LENGTH_LONG).show();

        for (String moduleName : splitModules) {

            int status = splitInstallSessionState.status();
            if (status == SplitInstallSessionStatus.DOWNLOADING) {
                Toast.makeText(getApplicationContext(), "Downloading " + moduleName, Toast.LENGTH_LONG).show();
            } else if (status == SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION) {
                try {
                    Toast.makeText(getApplicationContext(), "REQUIRES_USER_CONFIRMATION " + moduleName, Toast.LENGTH_LONG).show();
                    startIntentSender(splitInstallSessionState.resolutionIntent().getIntentSender(), null,
                            0, 0, 0);
                } catch (IntentSender.SendIntentException e) {
                    e.printStackTrace();
                }
            } else if (status == SplitInstallSessionStatus.INSTALLED) {

                    onSuccessfulLoad(moduleName, EPaperLauncherclassName, true);


                Toast.makeText(getApplicationContext(), "INSTALLED " + moduleName, Toast.LENGTH_LONG).show();
            } else if (status == SplitInstallSessionStatus.INSTALLING) {
                Toast.makeText(getApplicationContext(), "INSTALLING " + moduleName, Toast.LENGTH_LONG).show();
            } else if (status == SplitInstallSessionStatus.FAILED) {
                Toast.makeText(getApplicationContext(), "FAILED " + moduleName, Toast.LENGTH_LONG).show();
                Log.e(this.getClass().getName(), "Error: " + splitInstallSessionState.errorCode() + "for module " + moduleName);
            }
        }
    }
};

/**
 * Request uninstall of all features.
 */
private void requestUninstall(final String moduleName) {

    Toast.makeText(getApplicationContext(), "Requesting uninstall of all modules." +
            "This will happen at some point in the future.", Toast.LENGTH_LONG).show();

    final Set<String> installedModules = installManager.getInstalledModules();
    installManager.deferredUninstall(new ArrayList<String>(installedModules)).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(getApplicationContext(), "Uninstalling " + moduleName, Toast.LENGTH_LONG).show();
        }
    });
}

/**
 * Define what to do once a feature module is loaded successfully.
 *
 * @param moduleName The name of the successfully loaded module.
 * @param launch     `true` if the feature module should be launched, else `false`.
 */
private void onSuccessfulLoad(String moduleName, String launcherclassName, boolean launch) {
    if (launch) {
        Toast.makeText(getApplicationContext(), "onSuccessfulLoad " + moduleName,
                Toast.LENGTH_LONG).show();
            launchActivity(launcherclassName);

    }

}

/**
 * Launch an activity by its class name.
 */
private void launchActivity(String className) {

    try {


        Intent intent = new Intent();
        intent.setClassName(this.getPackageName(), className);
        startActivity(intent);


    } catch (
            Exception e)

    {
        Toast.makeText(getApplicationContext(), e.getMessage() + " ", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}


@Override
protected void onDestroy() {
    installManager.unregisterListener(splitInstallStateUpdatedListener);
    super.onDestroy();
}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.btn_epaper) {
        loadAndLaunchModule(EpaperModuleName, EPaperLauncherclassName);
    }
}

如果活动是在动态功能中定义的,则需要在attachBaseContext方法中调用SplitCompat.install(this)以确保所有新资源可用于您的应用程序。

You can take a look at how Dynamic Features are implemented in this sample . 您可以看一下此示例中如何实现动态功能。

There's sample code on how to open some resources, located in a dynamic module, from the base module . 有关如何从基本模块中打开动态模块中的某些资源的示例代码。

Plus, the samples includes others dynamic modules with activities implemented in Java/Kotlin and native. 另外,样本还包括其他动态模块,这些模块具有以Java / Kotlin和本机实施的活动。

Keep in mind that playcore v1.3.4 includes a fix a similar bug. 请记住,playcore v1.3.4包含一个类似错误的修复程序。 Be sure to use v1.3.4 or newer. 确保使用v1.3.4或更高版本。 The other requirement is to override the attachBaseClassContext in the activities you've in the dynamic modules: 另一个要求是在动态模块中的活动中覆盖attachBaseClassContext

override fun attachBaseContext(newBase: Context?) {
    super.attachBaseContext(newBase)
    SplitCompat.install(this)
}

This is shown in the sample with this abstract class. 这是在带有此抽象类的示例中显示的

I can confirm that the solution provided by @pfmaggi is correct. 我可以确认@pfmaggi提供的解决方案是正确的。

It works when you pass the application context, so you have do something like: 当您传递应用程序上下文时,它可以工作,因此您需要执行以下操作:

class MyApplication: SplitCompatApplication() {
...
    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        SplitCompat.install(this)
    }
...
}

and then add the application name in Manifest of your base module ( Note: add application name to the manifest file in your module named 'app' or the module with apply plugin: 'com.android.application' in its build.gradle file ) 然后在基础模块的清单中添加应用程序名称(注意:将应用程序名称添加到名为“ app”的模块的清单文件中,或在build.gradle文件中的带有Apply apply plugin: 'com.android.application'的模块中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态功能。 错误:处理清单失败 - Dynamic feature. Error: failed processing manifest 使用群组消息传递功能构建应用程序。 有什么建议吗? - Building an app with group messaging feature. Suggestions? App Bundle - 动态功能模块:在动态功能模块错误中找不到基础项目 - App Bundle - Dynamic feature modules : Base project not found in dynamic feature module error 将动态功能插入到现有应用程序包中 - Inserting a dynamic feature to an esixting app bundle App Bundle动态功能模块中的访问资源 - Access Resources In Dynamic Feature Module of App Bundle Android App Bundle:Google Play 150MB 限制似乎包括动态功能模块大小? - Android App Bundle: Google Play 150MB limit seems to include the Dynamic Feature Module size? 在动态模块中使用样式的错误 - android app bundle - error using styles in dynamic modules - android app bundle 签署App Bundle基本模块和动态功能模块 - Signing The App Bundle Base Module And Dynamic Feature Module App Bundle,在动态功能模块中,安装前可用的模块 - App Bundle, In Dynamic Feature Module, Module available before install 与动态功能模块(App Bundle)一起安装时出现“没有足够的存储空间” - "Not enough storage space" when installed with Dynamic Feature Module (App Bundle)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM