I had this issue where I overflow the 64k method limit in Dalvik when compiling all the library I used. I started to have this issue when I imported the Support Library, as some are already contained in others, it ended up overflowing the limit.
Is there a way to verify if a library is unused in the current project or already imported through another library dependencies?
Currently, I'm excluding those I know for sure but it seems weird to have to do this by hand.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:multidex:1.0.1'
compile "com.android.support:percent:${supportLibVersion}"
compile("com.android.support:design:${supportLibVersion}") {
exclude module: 'support-v4'
}
compile("com.android.support:cardview-v7:${supportLibVersion}") {
exclude module: 'support-v4'
}
compile('com.github.nkzawa:socket.io-client:0.4.1') {
exclude group: 'org.json', module: 'json'
}
compile('com.astuetz:pagerslidingtabstrip:1.0.1') {
exclude module: 'support-v4'
}
//...
}
Solutions so far:
- Use a Gradle plugin – We wrote an extremely easy to use Gradle plugin some time back that lists the number of methods by package along with the total number. You can find more information about it here.
- www.methodscount.com – Wondering how many methods a particular library will add to your application? Just enter the ‘compile’ statement on this website and it’ll tell you it’s method count, dependencies, JAR size and DEX size.
- Android Studio Plugin – This excellent plugin shows the method count of each dependency right inside Android Studio.
- Using the MultiDex Support Library If you are using Android Studio, the process is very straight-forward. If you are not, I highly recommend migrating, as Google may soon drop support for the Eclipse ADT plugin and the old Ant based build system.
Step 1
Add the dependency for the MultiDex support library in your build.gradle
dependencies {
...
compile 'com.android.support:multidex:'
...
}
Step 2
Enable multi-dexing by setting the multiDexEnabled flag in the buildType or productFlavor section of your gradle configuration.
defaultConfig {
...
multiDexEnabled true
...
}
Now depending on your project, you have 3 options:
If you haven’t created your own Application class, simply declare android.support.multidex.MultiDexApplication
as your application class in AndroidManifest.xml
....
android:name="android.support.multidex.MultiDexApplication"
...
If you already have your own Application class, make it extend android.support.multidex.MultiDexApplication
instead of android.app.Application
If your Application class is extending some other class and you don’t want to or can’t change it, override attachBaseContext() as shown below:
public class MyApplication extends FooApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}