Your Android project is very powerful, docking a lot of third-party open source libraries, the more complete the project, the more the code hit more and more cool. But suddenly one day the newspaper was abnormal.
Error: The number of method references in a. dex file cannot exceed 64K.
The compiler reminds you that your project has more than 64k of methods.
Androidstudio will remind you:
Learn how to resolve the issue at https://developer.android.com/tools/building/multidex.html
If you feel tired of reading English documents, then use the sub-package strategy provided by Google website (that is: Split Dex) To solve the problem of 64k limit.
But what about the eclipse compiler? It looks like you're going to use Google's official website solution to move the project to Androidstudio first.
"Maybe there's a better solution on the web, like using a plug-in framework: https://github.com/singwhatiwanna/dynamic-load-apk"
I still think Google's solutions are more reliable, so let's start.
The first step: Introducing the Multidex Library (com.android.support:multidex:1.0.1) provided by Google
Step Two: turn on multidexenabled (that is, add the attribute in Defaultconfig: multidexenabled true)
Step Three: change the project original application to Multidexapplication
There are two scenarios:
1> If your project does not rewrite the application class, you only need to change the application tag of the Androidmanifest.xml file, that is, add the attribute under the Application tab: Android:name= " Android.support.multidex.MultiDexApplication "can.
2> If your project overrides the application class, Then you need to remove your application class from the inherited Android.app.Application, inherit the Android.support.multidex.MultiDexApplication class instead, and then rewrite the Attachbasecontext (Context base), method, and initialize the Multidex. As follows:
public class MyApplication extends MultiDexApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); // 初始化 }}
This time the subcontract is finished.
Please also note: Out of memory issues
1> for projects with many dependencies, compilation may be interrupted by the following error
Error:Execution failed for task ‘:app:dexDebug‘. Error Code:3Output: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: GC overhead limit exceeded at...
Workaround:
To edit the Build.gralde file under the app, add the following properties under Android:
dexOptions { true javaMaxHeapSize ‘2g‘}
2> If your project has some dependent library (Module) projects, you may want to add properties to the Defaultconfig of its Build.gralde file: multidexenabled True
at this point, the subcontracting strategy is finished. Can basically solve the problem of more than 64k limit of the number of methods. But there are also some side effects, such as causing the project to start very slowly or even ANR.
Android apps more than 64k workaround: Split Dex