1

I'm using Android Studio 2.3.3, with Android Plugin for Gradle 2.3.3.

Please see my sample project, https://github.com/Uni-/AndroidModuleTest. In this project, there are two sample modules (as Android Studio names), app and library.

library has com.example.test.myapplication.MainActivity class, and it's referenced in AndroidManifest.xml of app. The application is compiled well, but it crashes on startup:

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

The MainActivity class seems not existing in the application artifact, according to the log.

In fact, build.gradle of app contains these lines:

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

When I replace these lines by a line below, the error goes fixed:

compile project(':library')

However, this is not what I want. I have various product flavors and custom build types in my real project, so just a compile, or even implementation in 3.0, can't be a solution.

What did I wrong in my sample project?

eonj
  • 109
  • 1
  • 1
  • 12
  • " have various product flavors and custom build types in my real project, so just a compile, or even implementation in 3.0, can't be a solution." Do only some of the build types use this class? Which build type are you running that gives the error? – Code-Apprentice Oct 31 '17 at 13:37
  • Indeed you don't have any java folder in your repo (it should be in src/main/java) . Where is your activity? – Jibbo Oct 31 '17 at 13:40
  • @Jibbo See /library/src/main/java/com/example/test/myapplication/. – eonj Oct 31 '17 at 13:49
  • @Code-Apprentice All build types cause this error; in this sample project, both debug and release. – eonj Oct 31 '17 at 13:51
  • Thanks for answering one of my questions. Please answer the other one as well. – Code-Apprentice Oct 31 '17 at 13:54
  • Did you already try to move the declaration of the activity in the library manifest instead of the app? since it is in that module, it should be declared in that manifest – Jibbo Oct 31 '17 at 13:58
  • @Jibbo Activities used in the app should be declared in the app manifest, not in the library manifest. See . When I declare the activity only in the library manifest, launch fails because there is no default activity. If I declare the activity in both manifest files, same error occurs. Please suggest a solution after you try it. – eonj Oct 31 '17 at 14:09
  • @Code-Apprentice all build types of app uses MainActivity class of library, in this sample case. But the real project does not. – eonj Oct 31 '17 at 14:11
  • @Uni- You should modify your example project to reflect that. I do not have time to investigate this further this morning. I will see whay I can do later tonight or tomorrow. – Code-Apprentice Oct 31 '17 at 14:20
  • @Code-Apprentice Please look over the problem in the sample project. The sample project also has a problem, and it's exactly same as the one in the real project. – eonj Oct 31 '17 at 14:26

2 Answers2

1

I fixed this by myself.

The key point is setting publishNonDefault true in build.gradle of library.

Also, the contextual dependencies should be declared as:

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

Fixed in 151f6e4. Thanks.

eonj
  • 109
  • 1
  • 1
  • 12
0

adding in module gradle implementation project(':yourLib') in my case solved the problem

Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78