Detailed explanation of method number overrun and startup optimization in Android

preface

Recently, I wrote an article about the conversion of eclipse project to Android studio project, and the direct reason why the company's project needs to be transferred to as is that the number of topic C methods to be written today exceeds the limit. I believe most Android projects will encounter this problem.

The traditional eclipse solution is to add DEX. Force. Jumbo = true in project.properties, and then clean up the project and recompile. However, when there are more and more methods, this method will not solve the problem. At this time, we need to use the solution multidex officially given by Google.

Multidex solutions

1、 If your minsdkversion > = 21, just add in the build.gradle of the module:

2、 If your minsdkversion is < 21, just add the following in the build.gradle of the module:

Then, if you don't write your own application class, you just write your own application class and inherit multidexapplication; If you have written your own application class and / or do not want the application class to inherit the multidexapplication, you need to override the attachbasecontext method of the application:

Problems with Google multidex

Although Google's subcontracting scheme is very simple, the effect is not so good. Google itself has enumerated the disadvantages of the subcontracting scheme:

Start optimization

Although the official solution is simple, it also has some limitations. For example, when loading an application for the first time, it will take more time to load the DEX file, resulting in slow startup speed, affecting the user experience, and even ANR.

To solve the problem of loading DEX, the meituan technical team does this: streamline the main DEX package, and load the second DEX package asynchronously after the application is started. This is a very good idea, but it is difficult to implement. Scripts need to be written to distinguish which classes should be placed in the main DEX package, and many third-party SDKs are used in general projects, which may lead to the main DEX package not being as compact as we want.

Of course, in addition, there are more suitable solutions for us. The solution idea of wechat development team is very interesting. They went against many apps and finally referred to and improved Facebook's solution. The general idea is to open a new process to execute the multidex. Install () method.

The idea of wechat development team is also relatively simple to implement. Now go directly to my code (and optimize the startup experience ~):

Attachbasecontext method in application:

Process loading DEX:

Configuration of loaddexactivity in manifest:

Main interface oncreate method:

Multidexutils tool class:

Another startup optimization scheme

There is also a simple startup optimization scheme, which can only optimize the startup experience and can not solve the anr problem.

When clicking the desktop icon to start the application, give a background picture. After starting, set the background back to empty.

1. Add the theme background in the entry activity

Add configuration to style.xml:

value:

Value-v21: (solve the problem of excessive animation effect of version 21 and above)

2. Set the background back as it is

Style.xml is configured as follows:

reference resources

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>