4

I know how to enable MultiDex support, but my problem is the opposite. Seems like my Android Studio's MultiDex option is enabled by default for some reason. When I unziped the resulting APK file and checked inside, there were two files.

classes.dex and classes2.dex.

You may say my project may hit 64K limit, but no, it doesn't. I also tried without any dependencies and with some other gradle settings, but the result was all the same. I even put the line multiDexEnabled false, but it didn't help either. So this time I created another new empty project, and hit the Run button without any modification. Result? Same.

enter image description here

enter image description here

As you can see, I have two .dex files with very low references. I don't understand what's happening here..

The reason why I'm trying to disable MultiDex is,

First, my app is really simple with a few dependencies so just don't need it.

Second, the app needs to support even older Android OS.

Third, I want to avoid the Dalvik linearAlloc limitation in the old Android devices.

Finally, I want to know the exact cause of this.

One more fun fact. I decompiled and looked into the the apk file, and couldn't find the class named MainActivity which was created by default while the app ran on my phone as expected. The same is true for my real project. The app works well but there isn't my code in the classes*.dex. Yeah, it's fine if the app works without any problem, but it's somewhat annoying because I sometimes want to decompile and see the resulted code.

Jenix
  • 2,996
  • 2
  • 29
  • 58
  • 1
    I just created a scrap project in Android Studio 3.0 (`minSdkVersion` 21, Empty Activity, no `appcompat-v7`), built the APK via Build > Build APK(s) from the main menu, and I cannot reproduce your problem. I changed the `minSdkVersion` to 9, and I still cannot reproduce the problem. Try using Build > Build APK(s) instead of Run and see if that changes your behavior. – CommonsWare Nov 20 '17 at 16:54
  • @CommonsWare How stupid.. I thought it's the exactly same process but as you said Build APK(s) solved the problem. Thank you so much. By the way, does your Play button have the same issue? – Jenix Nov 20 '17 at 17:04
  • 1
    Assuming by "Play" you mean the one with the "Run 'app'" tooltip, then no, I get the same results as I do with Build APK(s). However, I have Instant Run disabled. With things like Instant Run, the APK that you get from the Run process will be different than what you get from Build APK(s) -- see [this blog post](https://commonsware.com/blog/2017/10/31/android-studio-3p0-flag-test-only.html) for another difference. So, try temporarily disabling Instant Run, and see what happens. – CommonsWare Nov 20 '17 at 17:07
  • @CommonsWare Firstly, thank you for the link. Even though I'm still using Android Studio 2.2, it was really helpful, I would never know. But when I checked the resulted AndroidManifest.xml in the APK, it didn't have android:testOnly attribute. Maybe it's because it's not 3.0's. And yes, turning off Instant Run did the job! If you post your answer, I'll accept that. – Jenix Nov 20 '17 at 17:38
  • 1
    "Maybe it's because it's not 3.0's" -- yes, that behavior is new to 3.0. – CommonsWare Nov 20 '17 at 17:42

1 Answers1

1

Things like Instant Run change the nature of the APK. What you get when you run the app from the IDE is different than what you get when you build the app by other means (Build APK(s) in the menu, gradle tasks, etc.).

This is one of the reason why I disable Instant Run, as I'm one of those crazy people who wants to run the same app that my users would run.

Android Studio 3.0 makes another change to the APK, compared to what your users will run: it adds android:testOnly="true", preventing that APK from being installed normally. Probably this is a safety measure, so that you only distribute APKs made through some other build mechanism.

In your case, based on the comments, it appears that Instant Run was what was causing the multidex-style behavior. That may be tied to how Instant Run attempts to patch an already-installed APK, rather than push the fresh APK to the device or emulator.

So, either disable Instant Run or don't analyze the Run output, but instead focus on APKs built by other means.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I don't know if it's only me but anyway, I did another test out of curiosity and turned out pressing the Run button with Instant Run on always creates MultiDex files whether it's fresh install or not :) – Jenix Nov 20 '17 at 20:28