Android plugin development, made dynamic load

Source: Internet
Author: User

Why plug-in development is required:

I believe you should have heard about the limit of Android methods to 65K, and as the application function continues to enrich, one day you will encounter an exception:

Conversion to Dalvik format failed:unable toexecute dex:method ID not in [0, 0xFFFF]: 65536

Some students may say that to solve this problem is very simple, we only need to configure a sentence in the Project.proterty OK,

Dex.force.jumbo=true

Yes, by adding this phrase, you can actually make your app compile, but it's easy to appear on some 2.3-system machines

Install_failed_dexopt exception

For the above two exceptions, let's start by analyzing the reasons:

1, the Android system, a Dex file stored method ID with the short type of data, so that your Dex in the method can not exceed 65k

2, before the 2.3 system, the virtual machine memory is allocated only 5M

Know the reason, we have to solve the above problem, first of all, for the 65k problem, we can not change the structure of the Android system in the application layer, so we can not change the data type from short to int or other type, In other words, the number of methods in a Dex cannot exceed 65k, and we can only reduce the number of methods in one Dex, and the easiest scenario is to get rid of some useless jar packages and set some properties to public so that the Get/set method is removed. This method can only temporarily solve the problem, with the passage of time, one day there will be more than 65k of methods, after all, an application is generally in addition function, will not reduce functionality.

Solution:

(1) Some functions are made into plug-ins, dynamic loading.

(2) Subcontracting scheme, the Dex file in an APK is divided into multiple Dex files, and then dynamic to load the Dex file.

In fact, the core idea of the two programs is the same, the plug-in is the future to develop new features into the APK and Dex dynamic loading, and the subcontracting is to have completed the function of multiple Dex file load dynamically, in fact, I personally feel that the plug-in Scheme score package solution to better solve the problem of 65k, Because the plug-in solution can not only solve the 65k problem, but also let our application volume reduce, and sub-package can only solve the problem of 65k.

How to develop plug-ins for dynamic loading:

Research articles Android dynamic loading apk (Activity): Android apk dynamic loading mechanism research of Android APK dynamic loading mechanism (II): Resource management and Activity statement cycle management

Implement Android dynamic load APK (Fragment) implementation

Plug-in APK development specifications

At the beginning of the article, to want to become a plug-in apk, is to meet certain conditions, as follows is the use of this mechanism to develop plug-in APK needs to follow the specification:

1. Cannot use this: because this refers to the current object, which is the activity in the APK, this is meaningless because the activity is not an activity in the regular sense.

2. Use that: Since this is not available, use That,that as a member of the base class baseactivity of the activity in the APK, which points to this when the APK installation is running. When not installed, point to Agent Activity,anyway,that is better than in the host program.

3. The member method of the activity cannot be called directly: but must be called through that, because that is the dynamic allocation feature, through that to invoke the activity member method, after the APK installation can still run normally.

4. Start the new activity constraint: Start the external activity unrestricted, start the activity inside the APK is limited, first of all because the activity in the APK is not registered, so it does not support implicit invocation, Second, we must pass the new method defined in Baseactivity startactivitybyproxy and Startactivityforresultbyproxy, there is no support launchmode.

5. Currently, service, Broadcastreceiver, etc. are not supported for components that need to be registered to use.

Note: The dynamically loaded APK cannot contain the same jar package as the host app, or it will get an error.

Android plugin development, made dynamic load

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.