无法在应用程序中引用库模块 activity
Cannot reference activity of library module in application
我正在使用 Android Studio 2.3.3,Android Plugin for Gradle 2.3.3。
请查看我的示例项目,https://github.com/Uni-/AndroidModuleTest。在这个项目中,有两个示例 modules(作为 Android 工作室名称),app 和 library.
library 有 com.example.test.myapplication.MainActivity
class,并且在 app 的 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
class 似乎不存在于应用程序工件中。
事实上,app 的 build.gradle
包含这些行:
debugCompile project(path: ':library', configuration: 'debugCompile')
releaseCompile project(path: ':library', configuration: 'releaseCompile')
当我用下面一行替换这些行时,错误得到修复:
compile project(':library')
然而,这不是我想要的。我的实际项目中有各种产品风格和自定义构建类型,因此 compile
甚至 implementation
in 3.0 都不是解决方案。
我的示例项目有什么问题?
我自己解决了这个问题。
重点是在库的build.gradle
中设置publishNonDefault true
。
此外,上下文相关性应声明为:
debugCompile project(path: ':library', configuration: 'debug')
releaseCompile project(path: ':library', configuration: 'release')
已在 151f6e4
中修复。谢谢。
在我的案例中添加模块 gradle 实施项目 (':yourLib') 解决了问题
我正在使用 Android Studio 2.3.3,Android Plugin for Gradle 2.3.3。
请查看我的示例项目,https://github.com/Uni-/AndroidModuleTest。在这个项目中,有两个示例 modules(作为 Android 工作室名称),app 和 library.
library 有 com.example.test.myapplication.MainActivity
class,并且在 app 的 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
class 似乎不存在于应用程序工件中。
事实上,app 的 build.gradle
包含这些行:
debugCompile project(path: ':library', configuration: 'debugCompile')
releaseCompile project(path: ':library', configuration: 'releaseCompile')
当我用下面一行替换这些行时,错误得到修复:
compile project(':library')
然而,这不是我想要的。我的实际项目中有各种产品风格和自定义构建类型,因此 compile
甚至 implementation
in 3.0 都不是解决方案。
我的示例项目有什么问题?
我自己解决了这个问题。
重点是在库的build.gradle
中设置publishNonDefault true
。
此外,上下文相关性应声明为:
debugCompile project(path: ':library', configuration: 'debug')
releaseCompile project(path: ':library', configuration: 'release')
已在 151f6e4
中修复。谢谢。
在我的案例中添加模块 gradle 实施项目 (':yourLib') 解决了问题