The Assertmanager of Android application plug-in research

Source: Internet
Author: User

Recently in the study of Android application plug-in development, saw several related open source projects. Plug-ins are addressing several of the following issues:

    • How to load the code and resources from the plugin apk into the current virtual machine.
    • How to register the four components in the plugin apk into the process.
    • How to prevent conflicts in the resources in the plugin APK and the resource references in the host apk.

On these issues, I began to study the plug-in development implementation of the relevant technologies. In the previous article, I talked about how to load the class from the plugin apk into the current process, and this article focuses on the 1th 2nd: How to load the resources from another apk into the current app.

Assetmanager Introduction

When we get the resource in the component, we use GetResource to get the resource object, through which we can access related resources such as text, picture, color, etc. By tracking the source code discovery, in fact GetResource method is a context of an abstract method, GetResource implementation is implemented in Contextimp. Gets the Resource object is the applied global variable, and then continues to trace the source code, found that there is a assertmanager global variable in the Resource, passed in the Resource constructor, so the final acquisition of resources are through Assertmanager Get, so we focus on the Assertmanager. We have to solve the following two questions.

First, how to get the Assertmanager object.

Second, how to get the resources of APK in plugin by Assertmanager object.

We find the answer through the relevant source tracking for Assertmanager.

The Assertmanager constructor is not exposed to the API, cannot be created using new, and context. Getassets () can be used to get the assertmanager of the current context; take advantage of reflection AssetManager.class.newInstance () can be used to get objects.

Second, how to get the resources in the plugin apk. We found an important method in Assetmanager.

/** Add An additional set of assets to the asset manager. * This can is either a directory or ZIP file.* not for use by applications. Returns the cookie of the added asset, * or 0 on failure.*@{hide}*/int addassetpath (String path);

We can add a package containing a resource to the assets. This is the first path for Assertmanager to find a resource. This method is a hidden method that we can invoke by reflection.

Assetmanager Assetmanager = Assetmanager.  //context. Getassets ()? Assetmanager. Class.getdeclaredmethod ("Addassetpath", String.  Class). Invoke (Assetmanager, apkpath);   

As mentioned above, we can get the assetmanager of the current context, or we can create a assetmanager by reflection. We are analyzing the latter one here. The first can be, this question is left to the reader to think first, the subsequent article will be discussed separately. Learn more about Lao Luo's article, "Creating process Analysis for Android Application Explorer (Asset Manager)." Let's write a demo: Get the plugin's text resources and picture resources.

Create a plugin apk project module:apkbeloaded

I named the package name of the plugin: Laodresource.demo.com.loadresourcedemo.

The first step:

We don't have to write any code in this project. In the drawable directory, a picture is placed: icon_be_load.png.

The string is defined in strings: <string name= "Text_beload" >i am from apkbeloaded</string>.

Step Two:

Package Build apk:apkbeloaded-debug.apk.

Copy to the test machine file path: $ adb Push < your path >/apkbeloaded-debug.apk sdcard/

Create a host APK project

Define a text control and a picture control in the layout file to display the text and pictures obtained in the plug-in.

<imageview android:layout_width="wrap_content"android:src="@mipmap/ic_launcher"Android:id="@+id/icon"Android:layout_height="wrap_content"/><TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:id="@+id/text"Android:text="Hello world!"Android:layout_below="@+id/icon"android:layout_centerinparent="true"/>

Mainactivity.javaoncrease:

@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); ImageView ImageView=(ImageView) Findviewbyid (R.id.icon); TextView TextView=(TextView) Findviewbyid (R.id.text); /** * Plugin apk path*/String Apkpath= Environment.getexternalstoragedirectory () +"/apkbeloaded-debug.apk"; /** * Plugin Resource object*/Resources Resources= Getbundleresource ( This, Apkpath); /** * Get Picture Resources*/drawable drawable= Resources.getdrawable (Resources.getidentifier ("Icon_be_load","drawable",            "laodresource.demo.com.apkbeloaded")); /** * Get text resources*/String Text= Resources.getstring (Resources.getidentifier ("Text_beload","string",            "laodresource.demo.com.apkbeloaded"));    Imageview.setimagedrawable (drawable); Textview.settext (text);}

To create a resource object:

 Public Resources Getbundleresource (context context, String Apkpath) {    = Createassetmanager (apkpath);     return New Resources (Assetmanager, Context.getresources (). Getdisplaymetrics (), Context.getresources (). GetConfiguration ( ));}

To create a Assetmanager object:

PrivateAssetmanager Createassetmanager (String apkpath) {Try{Assetmanager Assetmanager= Assetmanager.class. newinstance (); Assetmanager.class. Getdeclaredmethod ("Addassetpath", String.class). Invoke (Assetmanager, Apkpath); returnAssetmanager; } Catch(throwable th) {th.printstacktrace (); }    return NULL;}

Demo Source: Https://github.com/liuguangli/LoadResourceDemo

The Assertmanager of Android application plug-in research

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.