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.
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'
}
//...
}
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);
}
}
hey you can try my trick, it might be useful for you
First of all you should avoid external libraries. Like in that libraries have same dependencies. So, try to merge that library with android application code. Due to this save much space for methods.
Use useful dependencies and libraries like if you want google play services then include only useful services not all.See example
compile "com.google.android.gms:play-services-location:9.4.0"
compile "com.google.android.gms:play-services-maps:9.4.0"
Avoid this
compile 'com.google.android.gms:play-services:9.4.0'
Remove unwanted dependencies.Use very chooseable dependencies in your code.
If all things not work really well then use multidex in your code.But it create Multiple dex files.So, your code will take much time for compilation.
Thanks hope this will help you.
You can open the terminal and run command gradlew app:dependencies to test which dependencies are already included in the others as transitive dependencies for your project along with the respective versions of each.
For example I got the following dependency chart for one of my projects for the com.android.support:design library I used:
+--- com.android.support:design:23.3.0
| +--- com.android.support:appcompat-v7:23.3.0
| | +--- com.android.support:support-vector-drawable:23.3.0
| | | \--- com.android.support:support-v4:23.3.0
| | | \--- com.android.support:support-annotations:23.3.0
| | +--- com.android.support:animated-vector-drawable:23.3.0
| | | \--- com.android.support:support-vector-drawable:23.3.0 (*)
| | \--- com.android.support:support-v4:23.3.0 (*)
| +--- com.android.support:support-v4:23.3.0 (*)
| \--- com.android.support:recyclerview-v7:23.3.0
| +--- com.android.support:support-v4:23.3.0 (*)
| \--- com.android.support:support-annotations:23.3.0
defaultConfig
{
multiDexEnabled true
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
and also add this method into your application class
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With