Switch between launch configurations of two modules without cleaning when one of them uses Multidex

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

The problem that I am facing is that when I switch from one configuration (drop-down list next to the "Start" button) and start another, I always get some errors. The only way to make it work is to make a clean project clean.

When switching from A to B without clearing, 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, application A starts, but does not work immediately at run time, stating that it cannot find its own 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 indicated the use of Multidex because I firmly believe that where the problem is, because if I turn it on for both projects, there are no errors, and there is no need to clean.

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

+5
source share
1 answer

There was a problem in multidex in android studio. But from gradle 1.4.0 beta this problem is solved. See this official release note here .

The problem was that the projects generated different mockable android.jar . What conflict is multidex built.

But from gradle 1.4.0: Several modules (e.g. application and lib) now use the same mockable android.jar (for unit testing), which is generated only once. Remove $ rootDir / build to restore it.

Just for your information, Android Studio 2.1.0 has a serious security problem. Therefore, a new version of Android Studio is recommended.

+1
source

All Articles