2

在 AndroidAPI级别,26(奥利奥)及以上我收到此错误。如果我从 android studio 直接向设备运行动态功能,则动态功能可以正常工作,但是当我在 Play 商店上传应用程序包并尝试从从 Play 商店下载的应用程序运行动态功能时,我得到了

运行时异常:每个资源(string.xml、atrr.xml 等)上的 Resources$ NotFoundException。

在 Android API 级别以下,26 可以正常工作。

错误日志:

2018-08-27 18:23:41.097 20926-20926/? 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

    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)

代码:

公共类 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);
    }
}
4

3 回答 3

3

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

于 2018-08-28T12:02:15.860 回答
2

我可以确认@pfmaggi提供的解决方案是正确的。

它在您传递应用程序上下文时起作用,因此您可以执行以下操作:

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

然后在基本模块的清单中添加应用程序名称(注意:将应用程序名称添加到名为“app”的模块apply plugin: 'com.android.application'中的清单文件或其 build.gradle 文件中的模块)

于 2019-07-04T11:55:17.903 回答
1

您可以查看此示例中的动态功能是如何实现的。

有关于如何从基本模块打开位于动态模块中的一些资源的示例代码。

此外,这些示例还包括其他动态模块,这些模块具有在 Java/Kotlin 和本机中实现的活动。

请记住,playcore v1.3.4 包含一个类似错误的修复。请务必使用 v1.3.4 或更新版本。另一个要求是覆盖attachBaseClassContext您在动态模块中的活动:

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

这在带有此抽象类的示例中显示。

于 2018-09-19T16:02:16.430 回答