簡體   English   中英

加載動態功能時出錯。 應用程式套件Android

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

在Android API級別(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);
    }
}

如果活動是在動態功能中定義的,則需要在attachBaseContext方法中調用SplitCompat.install(this)以確保所有新資源可用於您的應用程序。

您可以看一下此示例中如何實現動態功能。

有關如何從基本模塊中打開動態模塊中的某些資源的示例代碼。

另外,樣本還包括其他動態模塊,這些模塊具有以Java / Kotlin和本機實施的活動。

請記住,playcore v1.3.4包含一個類似錯誤的修復程序。 確保使用v1.3.4或更高版本。 另一個要求是在動態模塊中的活動中覆蓋attachBaseClassContext

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

這是在帶有此抽象類的示例中顯示的

我可以確認@pfmaggi提供的解決方案是正確的。

當您傳遞應用程序上下文時,它可以工作,因此您需要執行以下操作:

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

然后在基礎模塊的清單中添加應用程序名稱(注意:將應用程序名稱添加到名為“ app”的模塊的清單文件中,或在build.gradle文件中的帶有Apply apply plugin: 'com.android.application'的模塊中)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM