360 plug-in solution RePlugin learning: resource sharing, 360 replugin

Source: Internet
Author: User

360 plug-in solution RePlugin learning: resource sharing, 360 replugin

I did not write this, but later I found it still had a pitfall.

1. Use "plug-in" Resources

1. Check whether the "plug-in" is installed. If it is not installed, install the "plug-in" first"

2. You have installed the plug-in to obtain the resource ID of the plug-in: (Obtain R. string. app_name as an example)

Reflection

// Obtain the "plugin" loading class val classLoader = RePlugin. fetchClassLoader ("plugin1") // obtain the values/strings in the ins. val loadClass = classLoader. loadClass ("com. test. qby. myapplication. R \ $ string ") // obtain the Resource Name app_nameval field = loadClass. getField ("app_name") // obtain the "plug-in" id r. string. app_nameval identifier = field. get (loadClass)

Use the RePlugin. fetchResourceIdByName Method

val identifier = RePlugin.fetchResourceIdByName("plugin1", "string/app_name")

Use the RePlugin. fetchResources and getIdentifier Methods

val fetchResources = RePlugin.fetchResources("plugin1")val identifier = fetchResources.getIdentifier("app_name", "string", RePlugin.fetchPackageInfo("plugin1").packageName)

3. You can obtain the resource ID in all three methods, and then call the RePlugin. fetchResources ("plugin1"). getXXX method to obtain the resource, or directly use the ID. In addition, RePlugin provides a separate method for obtaining the View corresponding to layout.

// Obtain layout/activity_plugin.xmlval view = RePlugin. fetchViewByLayoutName
 
  
("Plugin1", "activity_plugin", null)
 

2. "plug-ins" use "host" Resources

(Take "host" layout as an example)
Obtain the host resource ID

Try {// obtain the corresponding layout class val loadClass = RePlugin through the host class loader. getHostClassLoader (). loadClass ("com. test. qby. myapplication. R \ $ layout ") // obtain the content_main field val field = loadClass. getField ("content_main") // gets the resource ID val any = field corresponding to content_main. get (loadClass)} catch (e: ClassNotFoundException) {e. printStackTrace ()} catch (e: NoSuchFieldException) {e. printStackTrace ()} catch (e: IllegalAccessException) {e. printStackTrace ()}

Use layout resource ID

// Method 1 // obtain XmlResourceParserval layout = RePlugin. getHostContext (). resources. getLayout (any as Int) // obtain Viewval view = layoutInflater Based on XmlResourceParser. inflate (layout, null) // The second val view = View. inflate (RePlugin. getHostContext (), any as Int, null)

III. It is difficult to use the "host" resource using the "plug-in" through reflection. here we can use a tool-class method.

Tool methods:

/*** Obtain the host resource ID ** @ param packageName package name, host package name * @ param className Class Name, for example, layout \ string \ drawable \ dimen \ color * @ param fieldName field name. For example, the field content_main corresponds to layout \ content_main.xml * @ return resource ID */fun getHostResourceId: string, fieldName: String): Int {try {// obtain the corresponding layout class val loadClass = RePlugin through the host class loader. getHostClassLoader (). loadClass ("$ packageName. R \ $ className ") // obtain the content_main field val field = loadClass. getField (fieldName) // obtain the resource ID return field corresponding to content_main. get (loadClass) as Int} catch (e: ClassNotFoundException) {e. printStackTrace ()} catch (e: NoSuchFieldException) {e. printStackTrace ()} catch (e: IllegalAccessException) {e. printStackTrace ()} return 0}

Obtain the resource ID using the tool Method

val layoutId = MyUtil.getHostResourceId(packageName,"layout","content_main")

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.