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
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
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.
Click On File Then Click On Project Structure Click On app on the left bottom of the popup. You will see multiple Tabs click on Dependencies Tab Then Click On plus at right top chose Library dependency search your desire Library it will show Those Libraries which are available in your IDE
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);
}
Though using Multidex Support Library library solves the problem, it is not perfect. The first thing to do should be to determine how many methods your app currently has and how many of them are being added by each of its dependencies. Earlier you had to do this manually, but now there are a bunch of tools available:
‘compile’
statement on this website and it’ll tell you it’s method count, dependencies, JAR size and DEX size.Irrespective of what you tool you end up using, use the information it provides to perform an audit of your app’s dependencies. You should look for unused libraries or ones that can be minimized or even replaced with your own simpler solution. If you are not using
Proguard
to remove unused code, first enable it and see if it solves the problem(Check this). UsingProguard
is well… not fun, but once you manage to make it work properly, it’ll significantly reduce the method count. If all else fails, you’ll have to use the MultiDex support library.
Check out gradle task to strip unused packages on Google Play Services library.
Conclusion
While the Multidex Support Library
fixes the DEX 64K problem in most cases, it should be treated as a last resort. Before attempting to use it, you should audit your project for unwanted dependencies and remove as much unused code as possible using ProGuard
. If you do go ahead and use it, make sure you test your app on older devices.
Hope this will help you.