【问题标题】:classnotfoundException getting if i start activity which is in dynamic module after successful download如果我在成功下载后启动动态模块中的活动,则会出现 classnotfoundException
【发布时间】:2019-05-24 12:58:31
【问题描述】:

通过内部测试,我可以下载动态功能模块。成功下载后,我正在使用包名称打开动态模块中的活动,但我发现类未找到异常。我检查了 APK 大小,但大小没有增加。下面是我的代码。请帮忙

下面是我下载模块的代码。我从 Playstore 中提供的内部测试中使用。

     public void loadFeatureTwo() {
    // Builds a request to install the feature1 module
    SplitInstallRequest request =
            SplitInstallRequest
                    .newBuilder()
                    // You can download multiple on demand modules per
                    // request by invoking the following method for each
                    // module you want to install.
                    .addModule("feature2")
                    .build();

    // Begin the installation of the feature1 module and handle success/failure
    splitInstallManager
            .startInstall(request)
            .addOnSuccessListener(new OnSuccessListener<Integer>() {
                @Override
                public void onSuccess(Integer integer) {
                    // Module download successful
                   /* Intent intent = new Intent().setClassName(getPackageName(), "com.bapspatil.feature2.FeatureTwoActivity");
                    startActivity(intent);*/

                    Toast.makeText(getApplicationContext(), "successfully download feature2: ", Toast.LENGTH_LONG).show();
                    try {
                        Intent myIntent = new Intent(MainActivity.this, Class.forName("com.bapspatil.feature2.FeatureTwoActivity"));
                        startActivity(myIntent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    // Module download failed; handle the error here
                    Toast.makeText(getApplicationContext(), "Couldn't download feature: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
}

【问题讨论】:

  • 您是否与 SplitCompat 集成? developer.android.com/guide/app-bundle/…
  • @Pierre 非常感谢。我没有放那个。
  • @Pierre 我仍然收到此异常 java.lang.ClassNotFoundException:在路径上找不到类“com.bapspatil.feature2.FeatureTwoActivity”:DexPathList [[zip 文件“/data/app/ com.imimobile.androidappbundledemo-xo4sNaGIZ60bGZFxT5o_GQ==/base.apk”,压缩文件“/data/app/com.imimobile.androidappbundledemo-xo4sNaGIZ60bGZFxT5o_GQ==/split_config.en.apk”,压缩文件“/data/app/com. imimobile.androidappbundledemo-xo4sNaGIZ60bGZFxT5o_GQ==/split_config.xxhdpi.apk"],nativeLibraryDirectories=[/data/app/com.imimobile.androidappbundledemo-xo4sNaGIZ60bGZFxT5o_GQ==/lib/x86, /system/lib]]
  • @Pierre 我在应用模块清单 android:name="com.google.android.play.core.splitcompat.SplitCompatApplication" 中添加了这一行

标签: java android android-app-bundle


【解决方案1】:

我遇到了同样的问题,我发现有时当您从 android studio 创建项目时,您的动态模块没有放置 apply plugin: 'kotlin-android' 在模块的 build.gradle 文件中。请验证它,如果不存在,请在模块 gradle 文件中应用它。

如果您仍然无法找到错误,请检查以下链接:
1.How to create dynamic module?
2.How to download dynamic module ?

【讨论】:

    【解决方案2】:

    确保您与SplitCompat 正确集成。

    尤其是,您似乎没有听到正确的事件。您应该致电registerListener 并提供自定义SplitInstallStateUpdatedListener。查看documentation的示例

    // Creates a listener for request status updates.
    SplitInstallStateUpdatedListener listener = state -> {
        if (state.sessionId() == mySessionId) {
          // Read the status of the request to handle the state update.
        }
    };
    
    // Registers the listener.
    splitInstallManager.registerListener(listener);
    

    【讨论】:

      【解决方案3】:

      你所做的并不完整。您必须使用SplitInstallStateUpdatedListener。 您需要确保两件事:

      1. 会话 id 与您的相同。
      2. 获得的状态是SplitInstallSessionStatus.INSTALLED

      当以上两点得到保证后,您才可以继续打开/访问您的动态模块。

      我现在看到了这个问题,但这绝对可以。

      相关代码如下:

      int mSessionId = 0; 
      SplitInstallStateUpdatedListener splitInstallStateUpdatedListener = new SplitInstallStateUpdatedListener() {
                      @Override
                      public void onStateUpdate(SplitInstallSessionState state) {
                          // Only if session id matches the one that is ours 
                          // and also the state is "Installed" that we proceed 
                          // with opening our dynamic activity.
                          if (state.sessionId() == mySessionId) {
                                  if(state.status() == SplitInstallSessionStatus.INSTALLED) {
                                      startDynamicFeatureActivity();
                                  }
                          }
                      }
                  };
                  splitInstallManager.registerListener(splitInstallStateUpdatedListener);
      
      splitInstallManager.startInstall(request)
                          .addOnSuccessListener(sessionId -> mySessionId = sessionId);
      

      【讨论】:

        猜你喜欢
        • 2014-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多