360 plug-in solution RePlugin learning notes-the plug-in uses the classes in the host, replugin learning notes

Source: Internet
Author: User

360 plug-in solution RePlugin learning notes-the plug-in uses the classes in the host, replugin learning notes

Scenario 1: When the xml layout of the plug-in uses the full Class Name of the host as the node

When the application configures RePluginConfig, there is a line of code:

// Allow the "agent to use the host class ". The default value is "off" c. setUseHostClassIfNotFound (true );

When this line of code is set to true,This class cannot be found in the plug-in project and will be found in the host project.

Scenario 2: When the plug-in code is used in the host project class, fields in the class, and internal class

Host Project

/*** Created by qby on 0023. * demonstrate "ins" using "host" class */class SpUtil {companion object {fun builder (): SpBuilder {return SpBuilder ()} class SpBuilder {var sp: SharedPreferences? = Null var str: String? = Null fun getSp (context: Context): SpBuilder {if (sp = null) {synchronized (SpUtil: class. java) {if (sp = null) {sp = context. applicationContext. getSharedPreferences ("testReplugin", Context. MODE_PRIVATE) }}return this} fun putString (key: String, value: String): SpBuilder {sp !!. Edit (). putString (key, value). apply () return this} fun getString (key: String, defValue: String): SpBuilder {str = sp !!. GetString (key, defValue) return this }}}

Storage before jump to the plug-in page:

SpUtil. builder (). getSp (this). putString ("first", "test ")

After you jump to the plug-in page, set the value to the plug-in code (Using Reflection)

// Obtain the host classloader val hostClassLoader = RePlugin. getHostClassLoader () // get the SpUtil class val clazz = hostClassLoader. loadClass ("com. test. qby. myapplication. utils. spUtil ") // gets the internal class, only one SpBuilder val declaredClass = clazz. declaredClasses [0] // initialize the SpBuilder object val newInstance = declaredClass. newInstance () // obtain the getSp method val method0 = declaredClass. getDeclaredMethod ("getSp", Context: class. java) // call the getSp method and assign the value to sp method0.invoke (newInstance, RePlugin. getHostContext () // obtain the getString method val method1 = declaredClass. getDeclaredMethod ("getString", String: class. java, String: class. java) // call the getString method and assign the value to str method1.invoke (newInstance, "first", "default") // obtain the getStr method val method2 = declaredClass. getDeclaredMethod ("getStr") // call the getStr method to obtain the str value val get = method2.invoke (newInstance) // The Toast prompt is displayed. makeText (this, get. toString (), Toast. LENGTH_SHORT ). show ()

Use Case 3: The plug-in calls methods in the host class

In fact, this should be regarded as a communication method. It can be implemented through reflection in scenario 2, and some methods can be exposed in the host project through the communication method in the aidl process for the plug-in to call.

Take storage in the host and use the aidl value in the plug-in as an example.

The host project creates a package under main and the aidl file under the package. The structure is as follows:

To create an ISpImpl class that inherits ISp. Stub. The Code is as follows:

/*** Created by qby on 0024. * AIDL implementation class */class ISpImpl: ISp. stub () {@ Throws (RemoteException: class) override fun getString (key: String, defValue: String): String? {Return SpUtil. builder (). getSp (SampleApplication. getContext (). getString (key, defValue). str }}

Expose the getString method, register the global IBinder in SampleApplication, and register it elsewhere, but register it before you call aidl:

// Register global IBinder. host is the custom registered name RePlugin. registerGlobalBinder ("host", ISpImpl ())

A new aidl with the same name as the package under the host project is created in the plug-in project. The structure is as follows:

Call in code:

// Obtain the registered IBinderval globalBinder = RePlugin according to the name. getGlobalBinder ("host") // gets the ISp Implementation class val asInterface = ISp. stub. asInterface (globalBinder) try {// call method to obtain the return value val string = asInterface. getString ("first", "try") // The Toast prompt is displayed. makeText (this, string. toString (), Toast. LENGTH_SHORT ). show ()} catch (e: RemoteException) {// catch exception e. printStackTrace ()}

There are a lot of code written in the place where reflection is used. When it is actually used, We need to extract it into the tool class and optimize the code.

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.