在应用程序中不能引用库模块的活动

时间:2017-10-31 13:35:02

标签: android gradle android-gradle android-studio-2.3

我使用的是Android Studio 2.3.3,Android Plugin for Gradle 2.3.3

请参阅我的示例项目https://github.com/Uni-/AndroidModuleTest。在此项目中,有两个示例模块(作为Android Studio名称),应用程序

图书馆com.example.test.myapplication.MainActivity个课程,并在应用AndroidManifest.xml中引用了该课程。应用程序编译得很好,但它在启动时崩溃了:

10-31 22:20:55.031 30084-30084/? I/zygote64: Late-enabling -Xcheck:jni
10-31 22:20:55.312 30084-30084/? I/InstantRun: starting instant run server: is main process
10-31 22:20:55.325 30084-30084/? D/AndroidRuntime: Shutting down VM
10-31 22:20:55.327 30084-30084/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.test.myapplication, PID: 30084
                                                   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test.myapplication/com.example.test.myapplication.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.test.myapplication.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/base.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/lib/arm64, /system/lib64, /vendor/lib64]]
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.test.myapplication.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/base.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.test.myapplication-dsBQ8b1LFh9WtKzvdfrLYg==/lib/arm64, /system/lib64, /vendor/lib64]]
                                                       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
10-31 22:20:55.328 30084-30084/? E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                       at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
                                                        ... 9 more
10-31 22:21:00.439 30084-30093/com.example.test.myapplication I/zygote64: Waiting for a blocking GC ObjectsAllocated
10-31 22:21:00.537 30084-30093/com.example.test.myapplication I/zygote64: WaitForGcToComplete blocked for 97.913ms for cause ObjectsAllocated

根据日志,MainActivity类似乎不存在于应用程序工件中。

事实上, app build.gradle包含以下行:

debugCompile project(path: ':library', configuration: 'debugCompile')
releaseCompile project(path: ':library', configuration: 'releaseCompile')

当我用下面的一行替换这些行时,错误会得到修复:

compile project(':library')

但是,这不是我想要的。我在我的真实项目中有各种产品风格和自定义构建类型,因此只需compile,甚至implementation in 3.0,就无法成为解决方案。

我的示例项目出了什么问题?

2 个答案:

答案 0 :(得分:1)

我自己解决了这个问题。

关键点是在图书馆publishNonDefault true设置build.gradle

此外,上下文依赖项应声明为:

debugCompile project(path: ':library', configuration: 'debug')
releaseCompile project(path: ':library', configuration: 'release')

已修复151f6e4。感谢。

答案 1 :(得分:0)

在我的案例中,添加模块gradle实现项目(':yourLib')