Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between run configurations of two modules without Clean when one of them uses Multidex

In my Android Studio project, I have several modules, of which only two are application modules (let's call them A and B), and the others are library modules, some used by both A and B.
For the module A, the multidex is enabled, while for the B it's not.

The issue I'm having is that when switching from one configuration (the dropdown next to the Run button), and running the other, I always get some errors. The only way to make it work is to do a full project clean.

When switching from A to B without clean, I get the following error:

    UNEXPECTED TOP-LEVEL EXCEPTION:
java.util.zip.ZipException: error in opening zip file
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:215)
    at java.util.zip.ZipFile.<init>(ZipFile.java:145)
    at java.util.zip.ZipFile.<init>(ZipFile.java:159)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:244)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:672)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)
8 errors; aborting (all are similar to the one above, so I omitted them)
Error:Execution failed for task ':Bapp'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

When switching from B to A, the A app then runs, but fails immediately at runtime, saying that it can't find my custom Application class in the APK :

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.kushtrim.mobile, PID: 3457
              java.lang.RuntimeException: Unable to instantiate application com.kushtrim.application.MyAApplication: java.lang.ClassNotFoundException: Didn't find class "com.kushtrim.application.MyAApplication" on path: DexPathList[[zip file "/data/app/com.kushtrim.mobile-1/base.apk"],nativeLibraryDirectories=[/data/app/com.kushtrim.mobile-1/lib/x86, /data/app/com.kushtrim.mobile-1/base.apk!/lib/x86, /vendor/lib, /system/lib]]
                  at android.app.LoadedApk.makeApplication(LoadedApk.java:578)
                  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4680)
                  at android.app.ActivityThread.-wrap1(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I specified the Multidex usage because I strongly believe that that's where the problem lies, because If I turn it on for both projects, no errors pop up, and no clean is needed.

So to summarize, is there a way to fix this, so that the clean step can be avoided ?

like image 873
Rick Sanchez Avatar asked Nov 08 '22 11:11

Rick Sanchez


1 Answers

In android studio, there was problem in multidex. But from gradle 1.4.0-beta this issue get solve. See this official release note from here.

Issue was that, projects was generating different mockable android.jar. Which conflict multidex build.

But from gradle 1.4.0 : Multiple modules (e.g. app and lib) now share the same mockable android.jar (for unit testing) which is generated only once. Delete $rootDir/build to regenerate it.

Just for your information, Android studio 2.1.0 have some serious security issue. So new version of android studio is recommended.

like image 64
Dharvik shah Avatar answered Nov 15 '22 06:11

Dharvik shah