In Android development, there is a little before heard, the recent occasional rumors heard the problem, is the 65k problem. What is a 65k problem? In fact, it is simple, Android has a limit, you can only have a maximum of 65,536 functions per App.
This limitation is actually the case, because when compiling into Dalvik bytecode, that is, putting your Class generation into a classes.dex, the compiler assigns an ID to all the function methods in your App, and then each Classes.dex ID The range is [0, 0xFFFF]. So, you know, there's a 65k problem.
Of course, most of the applications in the road will not encounter this problem, after all, 65k functions are also many, but considering the introduction of the SDK now, and especially the large company's programs tend to have a lot of unthinkable features, the final code is more and more bloated, refers to the day will encounter. But fortunately, this problem is not difficult to solve, Google has given the official solution:
Multidex Support Library
Simply translated, the principle is that the compilation is no longer simply compiled into a dex file, but cut into multiple Dex files. This allows 65,536 functions per file, which is certainly sufficient. The following is the specific operation.
The first is to import this library, this Eclipse and Android Studio is not quite the same, but specifically not to say, because it is too common. However, if the new version of Android Studio seems to have no need to import this library, it seems that the IDE directly support. Just set the multidexenabled in the config file to true, which is probably the following:
defaultConfig { "com.example.hecun.myapplication" 15 23 1 "1.0" true }
Then let your application class inherit from the Multidexapplication class, for example:
publicclass MyApplication extends MultiDexApplication { @Override publicvoidonCreate() { super.onCreate(); }}
If it is not convenient to modify the inheritance (for example, has inherited the other encapsulated application) so long a little more complicated, overloaded Attachbasecontext, the code is as follows:
public class myapplication extends multidexapplication { @Override public void oncreate () {super . OnCreate (); } @Override protected void attachbasecontext (Context base) {super . Attachbasecontext (base); Multidex.install (this ); }}
As to why the overloaded Attachbasecontext method is not very clear to me, why not write directly in the OnCreate method, just know that the execution order of the methods in application is as follows
There is also a situation is not inherited application class what to do? This is the most convenient, open your Androidmainfest file and add this in the application tag:
android:name="android.support.multidex.MultiDexApplication"
In this situation, the problem is solved. But this is not perfect, before Android 5.0, you also need to ensure that your Activity, service and so on basically Dalvik need to directly find your things are placed in the first Dex, or your App will be hung off. This part of the content is more complex, I suggest to go directly to Google's official note, Building Apps with over 65K Methods. Of course, if your program only supports Android 5.0 after the machine, then don't worry about it, because ART will combine multiple dex into one oat file, there is no problem found.
Solve the legendary Android 65k problem