Troubleshoot an Android single Dex file with no more than 65,536 method issues

Source: Internet
Author: User

When our project code is too large, the compile run will report unable to execute Dex:method ID not in[0, 0xFFFF]: 65536) error. When this error occurs, you have too many methods in your own engineering code, or the third-party plug-in jar package referenced under your project Lib folder has too many methods, both of which add up to more than 65536 of this number. Google requires that the method in a single Dex file not exceed the 65536 limit.

So this time, we need to deal with subcontracting. The general solution is to divide the entire project, including the jar, into two dex files.

There are many of these solutions on the web, some of which are packaged in a separate module in the project code as jar files, and then use the DX tool to turn the packaged jar file into the jar of the Dex file and then put it on the SD card for dynamic loading. Such a scheme is not in line with our needs.

So the question is, how better to split the Dex file, bypassing Google's prescribed 65536? In fact, there are some people on the internet to help us put forward a lot of solutions, especially on GitHub. In particular, Mmin18 proposed the Githut address as follows:

https://github.com/mmin18/Dex65536

The principle of this solution is almost like this:

1. Create the Custom_rules.xml file in the project directory and modify the compilation policy. Package The third-party plug-in jar packages contained in the files of the project Lib into libs.apk, and then use them as the second Dex file to compile the runtime.

2. Finally, perform the operation through the Ant command, running the entire project or signing encryption to package the entire project.

How, through the above introduction is not feel very simple, actually, if you want to really understand the whole principle, or very difficult, first you have to Custom_rules.xml files related configuration and Android Project compilation strategy is very familiar. However, here we do not care about it, since the ox people have helped us to write, then we just need to know how to use our project on the line.

The next step is how to use our project code (of course, the interested comrade can study its implementation principle).

A Configure and run the engineering steps as follows:

1. To use ant, first download Ant and configure ant Environment, download link address: http://ant.apache.org/bindownload.cgi. After downloading the Apache-ant-1.9.4-bin.zip package, unzip to the specified directory. Then configure the environment variable, create the variable named Ant_home, the value is the path corresponding to the ant file, for example my is Ant_home = E:\apache-ant-1.9.4-bin\apache-ant-1.9.4. %ant_home%/bin;%ant_home%/lib is then appended to the value of the path variable. This allows the ant environment variable to be configured.

2. The next step is to copy the files Custom_rules.xml and Pathtool.jar to our project's root directory.

3. Then just add code execution before our project runs to load the second Dex file, and the following Dextool method is to execute the function code that loads the second Dex file, just copy it to our custom application class, and the code is as follows:

[Java]View Plaincopy
  1. @SuppressLint ("Newapi")
  2. Privatevoid Dextool () {
  3. Filedexdir = New File (Getfilesdir (), "Dlibs");
  4. Dexdir.mkdir ();
  5. Filedexfile = New File (Dexdir, "libs.apk");
  6. filedexopt = Getcachedir ();
  7. try{
  8. Inputstreamins = Getassets (). Open ("libs.apk");
  9. if (dexfile.length () = Ins.available ()) {
  10. Fileoutputstreamfos = new FileOutputStream (Dexfile);
  11. byte[]buf = new byte[4096];
  12. Intl
  13. While ((L = ins.read (BUF))! =-1) {
  14. Fos.write (buf,0, L);
  15. }
  16. Fos.close ();
  17. }
  18. Ins.close ();
  19. }catch (Exception e) {
  20. Thrownew RuntimeException (e);
  21. }
  22. CLASSLOADERCL = getClassLoader ();
  23. Applicationinfoai = Getapplicationinfo ();
  24. Stringnativelibrarydir = null;
  25. if (Build.VERSION.SDK_INT > 8) {
  26. Nativelibrarydir= Ai.nativelibrarydir;
  27. }Else {
  28. nativelibrarydir= "/data/data/" + ai.packagename + "/lib/";
  29. }
  30. DEXCLASSLOADERDCL = New Dexclassloader (Dexfile.getabsolutepath (),
  31. Dexopt.getabsolutepath (), Nativelibrarydir, Cl.getparent ());
  32. try{
  33. Fieldf = ClassLoader.  Class.getdeclaredfield ("parent");
  34. F.setaccessible (true);
  35. F.set (CL,DCL);
  36. }catch (Exception e) {
  37. Thrownew RuntimeException (e);
  38. }
  39. }

The Dextool is then called in the OnCreate method of the custom application class.

4. Automatically generate Build.xml files.  Open a Command window, enter the project root directory, enter the following command Android update project-p. Before you enter this command, make sure that you have Android.bat and Find_java.bat files in the Sdk/tools directory and the Sdk/tools/lib folder that you configured.

5. The project is then run. Enter the command ant clean Debug Install run to ensure that your ant environment is configured without problems before entering this command.

Two Signature Obfuscation Code:

The above run apk is not garbled and signed by code, in general we need to generate a code-confusing and signed apk, then how to configure in the ant environment to generate code confusion and signature apk? The next step is to explain.

1. Create the Ant.properties file in the project root that has just been configured, the file will not be generated automatically when the project is created, we need to create it ourselves. This file is declared in the Build.xml file.

2. Then add the relevant information to the created ant.properties, such as the information I added below

The first line is to configure the associated encryption information file (also proguard.config = proguard.cfg)

The second line is the path of the specified signature file,./keystore.eking, indicating that the signature file is in the project root directory (copy the signature file to the project root)

The third line is a signature file with an alias value of eking

The four and five lines are the corresponding store and alias password for the signature file.

3. Then in the project directory to execute the following command antrelease, after execution will automatically generate the appname-release.apk file in the bin directory of the project, this is the signature of the generated apk.

demo:http://download.csdn.net/detail/stevenhu_223/8184135

Note: The demo has been validated. Lib/60k-methods.jar There are 60k methods, the demo project also has 60k methods, but the demo can be executed smoothly, indicating that the program is feasible.

This concludes with an introduction to splitting the Dex file to solve the Dex 65536 effective solution. Interested comrades can also go deep into the principle of its implementation.

Troubleshoot an Android single Dex file with no more than 65,536 method issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.