I have an app with a few libs, that reached the red-line of 65536 method count.
I achieved to setup the app as an multidex APK.
For size optimisation, I decided to use Proguard, because I just use a few feature of Guava and common.java.lang, and those libs bring their whole family with them.
After Proguard job, my app ref ~ 45 Kmethods
I often read that multidex app may crash time to time
And that because of second-dex runtime loading, this take time.
Does 4 and 5 are true ?
Then I just tried to not using mutidex, because my end methods count is < 56Kmethods with prodGuard, but it failed as if it has more !
To do so, I just put the gradle parameter multiDexEnabled to false
Is there something else to check/do ?
Here is a part of my Gradle :
android {
compileSdkVersion ANDROID_BUILD_SDK_VERSION
buildToolsVersion ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
applicationId "XXXX"
targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION
minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION
versionCode ANDROID_BUILD_VERSION_CODE
versionName ANDROID_BUILD_APP_VERSION_NAME
// Enabling multidex support.
multiDexEnabled false
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
debuggable false
ext.enableCrashlytics = true
renderscriptOptimLevel 3
signingConfig android.signingConfigs.release
zipAlignEnabled true
minifyEnabled true
// shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', 'proguard-rules-new.pro', 'proguard-rules-eventbus.pro', 'proguard-rules-firebase.pro', 'proguard-rules-fabric.pro', 'proguard-rules-leakcanary.pro'
}
debug {
debuggable true
renderscriptOptimLevel 3
applicationIdSuffix ".debug"
versionNameSuffix "debug"
}
}
I often read that multidex app may crash time to time
From the android developers documentation page (http://developer.android.com/tools/building/multidex.html#limitations):
Applications using a multidex configuration that make very large
memory allocation requests may crash during run time due to a Dalvik
linearAlloc limit (Issue 78035). The allocation limit was increased in
Android 4.0 (API level 14), but apps may still run into this limit on
Android versions prior to Android 5.0 (API level 21)
ART has built in support for multi-dex apks, so multi dexing should not cause any problems in lollipop and above. You may see issues on some devices running kitkat and below, although this should be rare unless you have a very high method count or memory requirements.
And that because of second-dex runtime loading, this take time.
Yes, multidex does slow down the very first start-up time of your app significantly. (upto 200% in case of yelp, when they went 20k methods above the limit) Even cold-start times increase.
Hence, if you can avoid multi-dexing, it is strongly recommended that you do so.
Even if you go above the limit, you should still try to minimize the method count as more and more methods slow down the app startup time on pre-lollipop devices.
In your case, if your build succeeded but if you're seeing run-time crashes (especially such as "No Class def. found") then it could be that you haven't configured proguard correctly, and it may be striping away some required components.
Timothy Meller from yelp has given a detailed talk on this issue, in which he also shares some multi-dex optimizations and the importance of proguard configurations:
https://www.youtube.com/watch?v=skmOBriQ28E
I'd recommend you watch this if you want a better understanding of multi-dexing on android
Related
I have some big chunks of code which test the behaviour of some incompletely documented Android APis (sigh) which seem to behave differently for different Android versions and I want to switch them in and out of debug builds (they are always removed from release builds) which I try to do using buildConfigFields in build.gradle like this
buildTypes {
release {
minifyEnabled true
buildConfigField("boolean", "RINGERMODETEST", "false")
...
}
debug {
// set this to false to remove the optional code
buildConfigField("boolean", "RINGERMODETEST", "true")
...
}
}
Then in my code I have
if (BuildConfig.RINGERMODETEST) {
// optional code
}
To get the optional code removed I need to turn on the optimiser, but setting minifyEnabled would turn on obfuscation as well which I don't want in a debug build. There is an earlier answer to this question for Proguard at Proguard shrinking and optimizing without obfuscation, but there doesn't seem to be any documentation which says how to do it for R8. I really don't want to have to learn how to construct a complete proguard control file when it's supposed to be superseded by R8. and useProguard will apparently soon be deprecated.
My wapp was being compiled and targeting api 27 and using a previous version of firebase ads. Now, after updating the app and compiling and targeting api 28 and using the last version of each sdk, including firebase ads (which is the same as admob), also compiling with R8 instead of proguard, I'm getting a lot of exceptions like this in the google play console Crashes section:
java.lang.NoClassDefFoundError:
at gk.b (gk.java:3)
at gl.a (gl.java:3)
at gn.a (gn.java:18)
at com.google.android.gms.ads.internal.util.ar.a (ar.java:5)
at fo.a (fo.java:19)
at fo.run (fo.java:8)
Caused by: java.lang.ClassNotFoundException:
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:171)
at java.lang.ClassLoader.loadClass (ClassLoader.java:379)
at ab.loadClass (ab.java:4)
at java.lang.ClassLoader.loadClass (ClassLoader.java:312)
at gk.b (gk.java:3)
at gl.a (gl.java:3)
at gn.a (gn.java:18)
at com.google.android.gms.ads.internal.util.ar.a (ar.java:5)
at fo.a (fo.java:19)
at fo.run (fo.java:8)
I don't know why it's obfuscated, because I uploaded the mapping file, maybe it is because the newer android studio version uses R8 instead of proguard.
Some users are putting bad reviews because they see the crashes since app update. I can't get the app crash in any of my devices.
The crashes are reported on Xiaomi MI 8 and OnePlus 5T devices, but for sure must be more.
What is the problem?
One solution is to disable both minifyEnabled (code shrinking, obfuscation, and optimization) and shrinkResources fields in your build.gardle configuration.
buildTypes {
release {
minifyEnabled false
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
I think it's happening due to minifyEnabled code shrinking which rename names of classes.
Since removing unused resources requires unused code shrinking to be turned on, you have to disable shrinkResources too.
See android documentation on shrink-code
Dears,
I read in many blog posts that multidex apps startup is slower than normal apps.
My app uses a lot of libraries that exceed 64k methods so I use multidex. But when I use proguard in release build, the final apk becomes less than 64k methods
So My question is: Can I enable multidex in Android debug build only so I don't have runtime error? and disable multi dex in release build as I don't need it?
If yes, how ?
If No, Is Android smart enough to speedup startup as it should recognize that app didn't exceed 64k even if it is multi dex app ?
Yes, you can. When you declare your buildTypes include multidex only for debug:
buildTypes {
release {
multiDexEnabled false
}
debug {
multiDexEnabled true
}
}
Instead of enabling multidex only for debug, you can change your min sdk version to 21 only for debug so gradle can speed up dexing with ART:
android {
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion 14
}
}
...
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
http://developer.android.com/tools/building/multidex.html
suggested methods are not needed anymore as android studio became "smart enough".
In fact, it will now give you a warning when you use minSdkVersion 21 (the old way) to speed up build time with dex:
You no longer need a dev mode to enable multi-dexing during
development, and this can break API version checks less...
In the past, our documentation recommended creating a dev product
flavor with has a minSdkVersion of 21, in order to enable multidexing
to speed up builds significantly during development. That workaround
is no longer necessary, and it has some serious downsides, such as
breaking API access checking (since the true minSdkVersion is no
longer known.) In recent versions of the IDE and the Gradle plugin,
the IDE automatically passes the API level of the connected device
used for deployment, and if that device is at least API 21, then
multidexing is automatically turned on, meaning that you get the same
speed benefits as the dev product flavor but without the downsides.
Yes, it even works with the multidex support library for Android versions prior to Lollipop with a little trick.
First specify multiDexEnabled for the debug build in build.gradle:
buildTypes {
...
debug {
...
multiDexEnabled true
}
}
Then create an AndroidManifest.xml file under src/debug.
src/debug/AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name="android.support.multidex.MultiDexApplication"
tools:replace="android:name"/>
</manifest>
That should do the trick. If your app uses a custom application class then you have to create a subclass of your application class and specify the name of that subclass in the manifest.
The application subclass should look like this:
public class MyDebugApplication extends MyApplication {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
When I add to my project the multidex:true, and make an Application class that extends from the MultiDexApplication, my project build time passed from 20 sec to around 90 sec.How to do some faster?
If you are like me who already tried Vic Vu's solution but still can't avoid enabling multiDex then you can try this (as long as your are using a device that has Android 5.0 and above).
Note This will only speed up your development build. Your production build will still be slow.
Basically you need to introduce 2 product flavors one for dev and one for prod.
Add multiDexEnabled true
android {
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion 14
}
}
...
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
defaultConfig {
applicationId "com.something.something"
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
multiDexEnabled true
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
And I have a class which extends Application so I had to override attachBaseContext()
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
If you are not extending Application simply use MultiDexApplication in your AndroidManifest.xml application tag.
Ensure that in your Android Studio Build Variants you are pointing to devDebug.
Read the complete instructions here https://developer.android.com/studio/build/multidex.html#dev-build
Supplying as an answer because this is better fit with the formatting.
To simply answer your question: No, there is no way. Multidex is a process meant to help lift the burden of the 65k method limit. This process is complicated and will simply make your build times longer.
The best you can can do is lower your method count.
In your build.gradle (supplied here) you're using:
`compile 'com.google.android.gms:play-services:8.3.0'`
But if you look at the most recent play services api you can pick and choose what services you actually need.
Look at Table 1 on this page.
Only use the ones you need. Google play services as a whole is somewhere around 30k methods.
That should help.
Multidexing uses more memory. As you get closer to your max heap size in Java you'll find Java spends more time doing GC than it does doing any real work, this can slow things down a lot.
I'd strongly recommend increasing the max heap size when using multidex. Add the following to the android closure in your build.gradle file to make the max heap size 4GB (Make it larger/smaller if you wish):
dexOptions {
javaMaxHeapSize "4g"
}
It depends.
You haven't specified it in your question, but if you just want to speed-up your development builds - then you can avoid the extra work. Official documentation includes a whole section about that.
According to this Blog BuildConfig.DEBUG was unreliable.
Since my colleague is using BuildConfig.DEBUG extensively (seemingly like test code in production code), I'm wondering if this flag is still as bugged as it was a few years ago.
I can confirm this bug still exists, tested with Android Studio 1.2 Build AI-140.1782451 and Gradle 1.1 compiling against Android API Level 21.
The issue is visible with a Nexus 10 on Android 5.0.2 or a similar device.
If you open BuildConfig.DEBUG in the source editor it says:
public static final boolean DEBUG = Boolean.parseBoolean("true");
But if you debug the app under question, DEBUG stays on false.
This hinders my Retrofit-debugging, as I wanted to enable it conditionally depending on the build-type.
It seems the issue to which you are referring is specific to ADT + Eclipse. So I believe that if you're using Gradle and Android Studio, this should not be an issue.
Crucially: this only occurs if you're using the Build Automatically option, and you don't clean your project. Because of this, I would hardly consider this a bug. After all, who says what should and shouldn't be rebuilt whenever you make a code change and have Build Automatically enabled?
As a matter of good practice, you should always clean and rebuild your project prior to an actual release, in which case this is a non-issue.
So yes, this is still a problem if you're using this setting, and not rebuilding your project prior to release, and you're still using ADT and Eclipse (which seems to be destined for deprecation).
Here's the discussion of the bug: https://code.google.com/p/android/issues/detail?id=27940
I had always problems with the predefined variable, so I created my own:
buildTypes {
// If we use the same keystore for debug and release, we don't have to wipe all preferences
debug {
//noinspection GroovyAssignabilityCheck
signingConfig signingConfigs.releaseConfig
zipAlignEnabled true
resValue "bool", "DEBUG", "true"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//noinspection GroovyAssignabilityCheck
signingConfig signingConfigs.releaseConfig
zipAlignEnabled true
resValue "bool", "DEBUG", "false"
}
}
I your code you can read this variable:
if (!MyApplication.get().getResources().getBoolean(R.bool.DEBUG)) {
// Firebase Crash reporting
FirebaseCrash.report(e);
}