According to the official Demo, try to access RePlugin.



Host program access

  1. In the root directorybuild.gradleAdd plug-in dependencies for the RePlugin host to the file

    classpath 'com. Qihoo360. Replugin: replugin - host - gradle: 2.3.3'Copy the code

    Note:replugin-host-gradle

  2. inhostApplication of appbuild.gradleTo add RePlugin’s core dependency libraries and applicationsreplugin-host-gradleThe plug-in

    apply plugin: 'com.android.application'
    
    android {
       ...
    }
    
    dependencies {
        ...
        implementation 'com. Qihoo360. Replugin: replugin - host - lib: 2.3.3'
    }
    
    apply plugin: 'replugin-host-gradle'/** * All configuration items are optional. No need to add by default * More optional configuration items see Replugin-host-gradle's RepluginConfig class * Modifiable configuration items See Automatically Generating RepluginhostConfig. Java */ RePluginHostConfig { // Whether to use the AppCompat library. In addition to adding appcomat-v7 dependencies to the host app, useAppCompat = should also be configuredtrue
    }Copy the code

    It should be noted that a. the core library introduced by the host isreplugin-host-libB. RePlugin plug-in needs to be placed after the Android configuration block, otherwise the applicationId cannot be found and an error is reported

  3. Configure the Application, using the RePluginApplication method inherited, or you can choose to inherit the custom Application and then call Replugin.app.xxx in each lifecycle. Please refer to the official documentation for details

    public class SampleApplication extends RePluginApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); RePlugin.enableDebugger(base, BuildConfig.DEBUG); } /** * RePlugin allows you to provide a variety of "custom" behaviors that allow you to implement functionality "without modifying the source code" */ @override protected RePluginConfigcreateConfig() {
            RePluginConfig c = new RePluginConfig();
            returnc; }}Copy the code

    Some parameters can be customized in application, as follows

    // Whether to enable the debugger. You are advised to enable it in the Debug phase and disable it in the Release phase. The debugger is disabled by default
    RePlugin.enableDebugger(base, BuildConfig.DEBUG);
    
    / / customize RePlugin behavior of the class, must be in RePlugin. App. AttachBaseContext is passed
    // Override createConfig() if inherited from RePluginApplication
    RePluginConfig c = new RePluginConfig ()
    
    // Set the plug-in callback method to customize the callback behavior of the plug-in framework
    c.setCallbacks(RePluginCallbacks callbacks)
    
    // Set the event callback method of the plug-in framework. The caller can customize the event callback behavior of the plug-in framework
    c.setEventCallbacks(RePluginEventCallbacks eventCallbacks)
    
    Is the host class allowed to be used when the plug-in does not specify a class? True, when no class is specified in the plug-in, the host's is used by default.
    c.setUseHostClassIfNotFound(boolean useHostClassIfNotFound)
    
    // Set whether to enable signature verification for the plug-in. The default is false. However, it is strongly recommended that this switch be turned on when releasing.
    / / this switch will have to and RePlugin addCertSignature cooperate to use
    c.setVerifySign(boolean verifySign)
    
    // Do you "move" the files to the app_p_A directory during plug-in installation? The default is True
    c.setMoveFileWhenInstalling(boolean moveFileWhenInstalling)
    
    // Whether to optimize the speed of the first plug-in load on Art. Default is false
    c.setOptimizeArtLoadDex(boolean optimizeArtLoadDex)Copy the code

    More configurations can be viewedRePluginConfigClass. See a class with two callbacks at configuration timeRePluginCallbacksandRePluginEventCallbacks                                          RePluginCallbacks:You can customize the ClassLoader for the host and plug-in or trigger a callback when the plug-in is not foundRePluginEventCallbacks:This class is called back when the plug-in installation fails or the activity in the plug-in is started and destroyed

Plug-in access

  1. In the root directorybuild.gradleAdd the plug-in dependency for the RePlugin plug-in to the file

    classpath 'com. Qihoo360. Replugin: replugin - plugin - gradle: 2.3.3'
    Copy the code

    Note:replugin-plugin-gradle

  2. inThe plug-inOf the programbuild.gradleTo add RePlugin’s core dependency libraries and applicationsreplugin-plugin-gradleThe plug-in

    apply plugin: 'com.android.application'
    
    android {
       ...
    }
    
    dependencies {
        ...
        implementation 'com. Qihoo360. Replugin: replugin - plugin - lib: 2.3.3'
    }
    
    apply plugin: 'replugin-plugin-gradle'* See replugin-plugin-gradle's RepluginConfig class */ repluginPluginConfig{// Plug-in name pluginName = for more optional items see replugininconfig class */ repluginPluginConfig{// Plug-in name pluginName ="demo1"// hostApplicationId hostApplicationId ="com.zh.replugindemo"// The full name of the host's startup page hostAppLauncherActivity ="com.zh.replugindemo.MainActivity"
    }Copy the code

    It should be noted that a. the core library introduced by the host isreplugin-plugin-libB. RePlugin plug-in needs to be placed after the Android configuration block, otherwise the applicationId cannot be found and an error is reported

Plug-in installation

  • External plug-in

External plug-ins are those that can be installed and run by “downloading”, “putting on an SD card”, etc. Such plug-ins can be installed directly via replugin. install(” APK path “). The same goes for upgrading plug-ins

  • Built-in plug-in
      A built-in plug-in is a plug-in that can be delivered “with the release of the main program”, usually in Assets of the main program. The install method is called out of order and the Dex is automatically loaded when the built-in plug-in is used. Built-in plugins can also be upgraded but will be equivalent to external plugins.

Adding built-in plug-ins:

    1. Rename the generated APK file to: “plug-in name “.jar
    2. Put it in assets/plugins of the main program

In this way, when the main program is compiled, the “dynamic compilation scheme” in RePlugin will automatically generate a “plugins-builtin.json” file in assets, which records the main information of its built-in plug-in and is easy to obtain directly at runtime.

preload

Neither external plug-in installation nor internal plug-in installation will release optimized resources in advance, which saves memory space, because not all plug-ins will use them, and resources will only be loaded when they are used, which will cause significant lag because the process is synchronous and time-consuming.

We can call replugin. preLoad(” plug-in name “) at an opportune time to preLoad the plug-in. It will “release the dex in advance” of the plug-in, and the DEX cache in memory, so that when the plug-in is started next time, there is no need to go through the dex2OAT process, the speed will be much faster. Preloading needs to be performed in child threads to avoid ANR.

Commonly used API

  • Plug-in management

    Install or upgrade the APK plug-in in the specified path
    RePlugin.install(String pluginPath);
    
    // Uninstall the plug-in with the specified name
    RePlugin.uninstall(String pluginName);
    
    // Preloads the plug-in with the specified name
    RePlugin.preload(String pluginName);
    
    // Check whether the plugin is installed (but not necessarily used, such as Dex, Native library may not be released)
    RePlugin.isPluginInstalled(String pluginName);
    
    // Check whether the current plug-in has released Dex and Native library
    RePlugin.isPluginDexExtracted(String pluginName);
    
    /* * Determine whether the plugin has been used before. As long as Dex and Native were released, they were regarded as the difference between "used" * and isPluginDexExtracted: the plug-in would delete the old Dex after the upgrade was completed. * Its isPluginDexExtracted is false, while isPluginUsed remains true */
    RePlugin.isPluginUsed(String pluginName);Copy the code
  • Component calls in plug-ins

1. Open the Activity

The host starts the plug-in’s Activity

     // The first way
     Intent intent = new Intent();
     intent.setComponent(new Component("Plug-in Name"."The Activity full name"));
     context.startActivity(intent);

    // The second way
    Intent intent = RePlugin.createIntent("Plug-in Name"."The Activity full name");
    context.startActivity(intent);

    // The third way
    RePlugin.startActivity(context, intent)
    RePlugin.startActivity(context, intent, "Plug-in Name"."The Activity full name")
    RePlugin.startActivityForResult(context, intent, requestCode)Copy the code

The plug-in starts the host Activity in much the same way that the host starts the plug-in, except that it passes the host package name

    Intent intent = new Intent() 
    intent.setComponent(new Component("Host package name"."The Activity full name")); 
    context.startActivity(intent);Copy the code

Replugin.gethostcontext () can be used to retrieve the host’s Context, which can be used to retrieve information such as the host’s package name

2. The host starts the Service in the plug-in, which is called through the PluginServiceClient class, similar to the arguments passed in the development unit

    // Start the service
    PluginServiceClient.startService(context, intent)
    // Stop the servicePluginServiceClient. StopService (context, intent)// Bind the service
    PluginServiceClient.bindService(context, intent, serviceConnection, flag)
    // Unbind
    PluginServiceClient.unbindService(context, intent, serviceConnection, flag)Copy the code

3. Communicate with Binder in the plug-in

First need to in the Binder in the concrete implementation of plug-in modules in the Application in advance call RePlugin. RegisterPluginBinder () need to register IBinder name (whatever) and the Binder interface implementation class.

    / / plugindemo2 in Application
    public class MainApp extends Application { 
        @Override 
        public void onCreate(a) { 
            super.onCreate();
             RePlugin.registerPluginBinder("demo2test".newDemo2Impl()); }}}Copy the code

The IBinder object is then retrieved from Replugin. fetchBinder(” plug-in name “, “IBinder name at registration “) where cross-plug-in calls are needed. The internal Binder class of the implementation class of IInterface generated by AIDL protocol is invoked to obtain the Binder proxy, and the communication between the plug-ins is realized

    // Plugindemo1 and plugindemo2 communicate with binder
    IBinder b = RePlugin.fetchBinder("demo2"."demo2test");
    if (b == null) {
        return;
    }
    IDemo2 demo2 = IDemo2.Stub.asInterface(b);
    try {
        demo2.hello("helloooooooooooo");
    } catch (RemoteException e) {
        e.printStackTrace();
    }Copy the code

4. FetchXXX method in RePlugin

    // Load the plug-in in the current process and get the IBinder defined by the plug-in through the Plugin class
    RePlugin.fetchBinder("Plug-in name"."Module to load")
    
    // Obtain the ClassLoader of the specified plug-in, through which the class file can be loaded
    RePlugin.fetchClassLoader("Plug-in name")

    // Get the Context of the specified plug-in
    RePlugin.fetchContext("Plug-in name")

    // Get the resource Id of the specified plug-in
    RePlugin.fetchResourceIdByName("Plug-in name"."Resource type + Resource name to obtain")/ / such as "layout/activity_main"
    
    // Get the Resource object for the specified plug-in
    RePlugin.fetchResource("Plug-in name")

    // Get the View object for the specified plug-in
    RePlugin.fetchViewByLayoutName(""."Layout name",rootView)
Copy the code