Android uses the dynamic loading framework DL for plug-in development, androiddl

Source: Internet
Author: User
Tags vector font

Android uses the dynamic loading framework DL for plug-in development, androiddl

If you have reprinted, please declare the Source: sand: http://blog.csdn.net/t12x3456 (from the sand csdn blog)

Overview:

With the continuous iteration of applications, the application volume increases, projects become bloated, and redundancy increases. new features of the project cannot be matched with users. Serious exceptions often lead to sudden changes. You can only urgently release patch versions and force users to update them. the results are updated frequently, which reduces user stickiness. or with the continuous development of the company's business, there are more and more applications in the same department. in the traditional way, you need to download and install through the main project with the largest number of users.

What should I do? Refer to browser-plug-in Development Mode:

I. you can split your own applications. Some functions can be implemented in the plug-in and downloaded later without installation. if a new function is added, you do not need to update the application. You only need to manage the reserved plug-in. You can dynamically update your application by adding a plug-in. This function needs to be improved or expanded, update the plug-in without frequent installation or uninstallation ).

II. corresponding to the same application, the normal method of drainage can only guide users to download and install new applications. If you use plug-in development, you do not need to install the application. It is also very convenient to disable the plug-in function, the process of application installation and uninstallation is omitted to achieve seamless drainage.

Here I would like to recommend an open-source dynamic loading framework DL. This project was initiated by singwhatiwanna. Currently there are three developers, and I have the honor to become a contributor.

If you are not familiar with the DL dynamic loading framework, we recommend that you read this article first:

APK dynamic loading framework DL resolution http://blog.csdn.net/singwhatiwanna/article/details/39937639

If it is not clear after reading it, see the panorama of the DL plug-in framework, as shown below:


Here I will mainly introduce the specific steps for using the DL framework for development:

1. First, we need to get the project code from github:

Https://github.com/singwhatiwanna/dynamic-load-apk

The downloaded directory is as follows:

 

The lib directory is our public plug-in library.

The sample directory corresponds to the demo. for specific projects, refer to the three modes in the DL panorama above:

In general projects, code management and development teams are relatively independent. Generally, plug-in engineering teams have difficulty accessing the code of the main project team. therefore, the first and most recommended method is provided here. The plug-in is developed independently of the host. there is no need for too much interaction between the two teams, and the development efficiency is relatively high.


2. Import the lib project as follows:



We can see the dl-lib.jar under the bin directory, if we need to modify the lib project, re-build to get the corresponding dl-lib.jar.


3. Develop the plug-in project and import the main-plugin project in the demo.


Pay attention to the development of plug-ins to avoid unnecessary errors.

The plug-in also needs to reference the DL jar package, but cannot be placed under the libs directory of the plug-in project. In other words, is the plug-in compiled when dependent jar package but package into apk do not put the jar package in, this is because the dl-lib.jar already exists in the host project, if the plug-in also has this jar package, there will be a class Link error, the reason is very simple, there are two identical classes in the memory, repeated. As for the support-v4 is the same truth. For eclipse is simple, you just need to create a directory in the plug-in project, such as external-jars, then put the dl-lib.jar and support-v4.jar in, and append the following two sentences in. classpath:

<classpathentry kind="lib" path="external-jars/dl-lib.jar"/><classpathentry kind="lib" path="external-jars/android-support-v4.jar"/>
Then, the specific steps in Plug-in development

(1) If the original is Activity, you need to inherit DLBasePluginActivity here. If the original is FragmentActivity, You Need To Inherit DLBasePluginFragmentActivity, for example:

public class MainActivity extends DLBasePluginActivity 
TestFragmentActivity extends DLBasePluginFragmentActivity

(2) If the plug-in needs to be installed and run independently, you only need to put the jar under libs. If dynamic loading is supported, you still need to add the above considerations to exteral-jars.

(3) permissions required by the plug-in must be declared in the host project.

For actual development, we generally obtain the plug-in from the server. Here we can debug the demo by ourselves. We will generate the corresponding plug-in apk and put it in the DynamicLoadHost directory on the SD card.


4. Add the above-generated dl-lib.jar to libs in the host project, as shown below for the host project provided in the demo


In the host project, we first need to obtain the MainActivity corresponding to the plug-in apk to be called. In the DL demo, the plug-in path is the DynamicLoadHost directory on the SD card. If not, we need to create a directory, or modify it as needed.


        String pluginFolder = Environment.getExternalStorageDirectory() + "/DynamicLoadHost";        File file = new File(pluginFolder);        File[] plugins = file.listFiles();        if (plugins == null || plugins.length == 0) {            mNoPluginTextView.setVisibility(View.VISIBLE);            return;        }        for (File plugin : plugins) {            PluginItem item = new PluginItem();            item.pluginPath = plugin.getAbsolutePath();            item.packageInfo = DLUtils.getPackageInfo(this, item.pluginPath);            if (item.packageInfo.activities != null && item.packageInfo.activities.length > 0) {                item.launcherActivityName = item.packageInfo.activities[0].name;            }            mPluginItems.add(item);        }

Next is the response to call the apk, then you need to use the dl-lib.jar:

 (1) obtain the MainActivity Class object in the plug-in apk that we need to call through class. forName

(2) As mentioned above, we need to determine whether the object inherits from DLProxActivity or DLProxFragmentActivity to obtain the corresponding proxy class object.

(3) Use the corresponding proxy class object to call the plug-in apk

 PluginItem item = mPluginItems.get(position);        Class<?> proxyCls = null;        try {            Class<?> cls = Class.forName(item.launcherActivityName, false,                    DLClassLoader.getClassLoader(item.pluginPath, getApplicationContext(), getClassLoader()));            if (cls.asSubclass(DLBasePluginActivity.class) != null) {                proxyCls = DLProxyActivity.class;            }        } catch (ClassNotFoundException e) {            e.printStackTrace();            Toast.makeText(this,                    "load plugin apk failed, load class " + item.launcherActivityName + " failed.",                    Toast.LENGTH_SHORT).show();        } catch (ClassCastException e) {            // ignored        } finally {            if (proxyCls == null) {                proxyCls = DLProxyFragmentActivity.class;            }            Intent intent = new Intent(this, proxyCls);            intent.putExtra(DLConstants.EXTRA_DEX_PATH,                    mPluginItems.get(position).pluginPath);            startActivity(intent);        }

Finally run the host projectMain-hostTo see the final effect:




I believe that after the above steps, you can have a certain understanding of the development of the DL dynamic loading framework. the DL framework is still being expanded. Welcome to star, fork for our project, or provide valuable suggestions ., if you have any questions, please give us feedback in time. We will fix or improve them in later versions.



What is the five application development frameworks of android?

The android Application Development Framework consists of five parts: Linux Kernel, Android Runtime, Libraries, Application Framework, and Applications. The second part will detail these five parts. The bottom-up analysis layers are as follows.

Android Architecture

1. Linux Kernel

Android provides core system services based on Linux 2.6, such as security, memory management, process management, network stack, and driver model. Linux Kernel is also an abstraction layer between hardware and software. It hides specific hardware details and provides unified services for the upper layer. If you have learned computer networks and know OSI/RM, you will know that the benefit of layering is to use the services provided by the lower layer to provide unified services for the upper layer, shielding the differences between the current layer and the following layers, when the current layer and the following layers change, the upper layer is not affected. That is to say, each layer performs its own duties, and each layer provides a fixed SAP (Service Access Point). The professional points can be said to be high cohesion and low coupling. If you are only engaged in application development, you do not need to have a deep understanding of the Linux Kernel layer.

2. Android Runtime

Android contains a collection of core libraries, providing most of the functions available in the core library of Java programming language. Every Android application is an instance in the Dalvik Virtual Machine and runs in its own process. The Dalvik virtual machine is designed to efficiently run multiple virtual machines on one device. The Dalvik Virtual Machine executable file format is. dex. dex is a compression format designed for Dalvik, suitable for systems with limited memory and processor speed. Most virtual machines, including JVM, are stack-based, while Dalvik virtual machines are register-based. The two architectures have their own advantages and disadvantages. Generally, the stack-based machine requires more commands, while the register-based machine requires more commands. Dx is a set of tools that can convert Java. class to. dex format. A dex file usually has multiple. classes. Because dex sometimes has to be optimized, it will increase the file size by 1 to 4 times and end with ODEX. The Dalvik Virtual Machine relies on the Linux kernel to provide basic functions, such as thread and underlying memory management.

3. Libraries

Android contains a collection of C/C ++ libraries for various components of the Android system. These functions are exposed to developers through the Android application framework. The following lists some core libraries: system C library-the BSD derivative of the Standard C system library (libc), adjusted to the embedded Linux device media library-The PacketVideo-based OpenCORE. These libraries support playing and recording many popular audio and video formats, as well as static image files, including MPEG4 and H. 264. Manage MP3, AAC, AMR, JPG, and PNG interfaces-manage the access display subsystem and seamlessly combine the 2D and 3D graphics layers LibWebCore of multiple applications-a new Web browser engine, driver Android browser and Embedded web View SGL-Basic 2D graphics engine 3D library-Based on OpenGL ES 1.0 APIs implementation. Libraries use hardware 3D acceleration or include highly optimized 3D software grating FreeType -- bitmap and vector font rendering SQLite -- powerful and lightweight relational database engines available for all applications

4. Application Framework

By providing an open development platform, Android enables developers to develop extremely rich and novel applications. Developers can freely take advantage of the hardware advantages of devices, access location information, run background services, Set alarms, and add notifications to the status bar. Developers can fully use the APIs framework used by core applications. Architecture of the application ...... the remaining full text>

Android development framework

Google's gson package can be used for json parsing.
The Network request and cache mechanism (generally referred to as image cache) can be viewed by volley.
The database is relatively simple. The android app is already encapsulated. But you can check FinalDb in Afinal.
The UI is useless. It is written by yourself. You can master the view and viewgroup. Basically, you can master all the controls.

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.