Introduction to the dynamic loading technology of Android virtual machines

Source: Internet
Author: User
11I want to add this article to my favorites one month ago (
), 147 reads, 0 comments in total

The dynamic loading technology of Android virtual machines is divided into two types: one is to load the so Library Based on ndk; the other is to load the zip package developed in Java. I will mainly discuss the latter today.

First, let's briefly describe how to load the so library. Ndk is highly efficient and well-encrypted, but it is difficult to get started at the same time. It is generally used for encryption and decryption, mathematical operations, and other occasions. So loading is very simple. If the APK has carried the so file at the time of release, you only need to call the system. loadlibrary (libname) method at the time of loading. Because there is no write permission to store the so directory in the installation directory of the software, developers cannot change the content of this directory. Therefore, to dynamically load the so file stored elsewhere, use system. load (pathname) method.

Now let's focus on developing Android dynamic packages using Java. Jar and APK are actually in ZIP format. Android virtual machines support packages with these two file suffixes. Android virtual machines support loading Dex-format code files in the zip package. So we need to use a very important class dexclassloader, which is the key to dynamic addition technology. When talking about dynamic loading, we also need to use the Java reflection technology. The following is a pseudo code call:

Dexclassloader DCL = new dexclassloader (absolute path of the ZIP file, directory of the ZIP file, so directory, classloader In the context );
Class <?> Clazz = DCL. loadclass (full package name of the class to be loaded );
Method getinstance = clazz. getmethod (method name, parameter class );
Getinstance. Invoke (null, parameter); // The first parameter is null, indicating a static method

In this way, the class in the ZIP file is loaded, and images and other resources can also be loaded in the zip package. With this technology, many execution logics can be dynamically adjusted by means of online downloads, realizing the diversity of applications.

However, there are some restrictions on dynamic loading. For example, the activity and service classes in the zip package cannot be dynamically loaded because the Declaration is missing. Even if you make a declaration in the manifest file, by default, the system searches for classes in the path where the APK is installed, so you will encounter a classnotfound exception. In the plug-in, you can use resources such as layout and strings put in the APK first. However, the built-in interface of the plug-in can only be written in pure code, and the XML in the zip package cannot be loaded in the plug-in for layout and other resources. Therefore, it is more difficult to develop some special effects. We recommend that you implant the APK in advance.

There is a work und for dynamic loading of activity and service, that is, reserve various activity and service packaging classes during APK development and define the most common method for event processing, then, call the method defined in the plug-in to process the event.

Zip plugin creation: Export Code and resources to jar files. On the basis of this step, we usually use obfuscators to confuse the code. Finally, use the command line DX command to convert the class file in jar to the DEX file.

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.