VirtualAPK detailed explanation and use, really cow

Keywords: Android Design Pattern

3.1 basic principles

  • ClassLoader that combines host and plug-in
    It should be noted that classes in plug-ins cannot be duplicated with the host
  • Merge plug-in and host resources
    Reset the packageId of the plug-in resource and merge the plug-in resource and the host resource
  • Remove the reference of the plug-in package to the host
    During construction, the Gradle plug-in is used to remove the reference of the plug-in to the host code and resources

3.2 implementation principle of four components

  • Activity
    The system verification is bypassed by taking up the pit in the host manifest, and then the real activity is loaded;
  • Service
    The dynamic agent AMS intercepts service related requests and transfers them to the Service Runtime for processing. The Service Runtime will take over all operations of the system;
  • Receiver
    Re register the statically registered receiver in the plug-in;
  • ContentProvider
    The dynamic proxy IContentProvider intercepts provider related requests and transfers them to the Provider Runtime for processing. The Provider Runtime will take over all system operations.
    The overall architecture of VirtualAPK. Please read the source code for more details.

3.3 overall architecture of virtualapk

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-0qimh5da-1630587542054)( https://user-gold-cdn.xitu.io/2017/10/19/303e5cfd242442fee27eb48da71fe5a5? imageView2/0/w/1280/h/960/ignore-error/1)]

3.4 principle analysis source code

Source code analysis written by Hongyang great God (Didi's official website recommends reading written by Hongyang great God)
Analysis of VirtualApk source code of didi plug-in scheme
The following great God doesn't know who it is (recommended on Didi's official website)
Analysis of VirtualAPK resource loading mechanism

4 use

4.1 the plug-in and the host must compile the same aar

For example, the host compile s the following aar

compile 'com.didi.virtualapk:core:0.9.0'//Dependency of didi VirtualAPK
compile 'com.alibaba:fastjson:1.2.39'
compile'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

If the plug-in project needs to access classes and resources in the host sdk, you can also compile the aar of the sdk in the plug-in project, as follows:

compile 'com.didi.virtualapk:core:0.9.0'//Dependency of didi VirtualAPK
compile 'com.alibaba:fastjson:1.2.39'
compile'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

In this way, the plug-in project can normally reference the sdk. Moreover, the aar will be automatically removed from apk when the plug-in is built

4.2 host Engineering

  • Add in build.gradle under the project root directory
dependencies {
 classpath 'com.didi.virtualapk:gradle:0.9.0'
}
  • Add at the top of the App's build.gradle
 apply plugin: 'com.didi.virtualapk.host' 
  • Add compile in build.gradle of App
dependencies {
 compile 'com.didi.virtualapk:core:0.9.0'
}
  • Write MyApp inherited Application and rewrite attachBaseContext method to initialize plug-in engine (don't forget to configure Application in AndroidManifest.xml)
 @Override
    protected void attachBaseContext(Context context)  {
        super.attachBaseContext(context);
        PluginManager.getInstance(context).init();
    }
  • It is recommended that you load the plug-in when the Application starts. Otherwise, please pay attention to the loading time of the plug-in. Consider a case. If you load the plug-in at a later time and access the resources in the plug-in, please pay attention to the current Context. For example, to load the plug-in in the host Activity(MainActivity), and then access the resources (such as fragments) in the plug-in in the MainActivity, you need to make the displayed hook, otherwise some 4.x mobile phones will fail to find the resources.
 PluginManager pluginManager = PluginManager.getInstance(this);
//Here is to check whether the plug-in apk exists and load it if it exists (for example, modify the online bug and download the plug-in apk to the root directory of sdcard, named Demo.apk)
        File apk = new File(Environment.getExternalStorageDirectory(), "Demo.apk");
        if (apk.exists()) {
            try {
                pluginManager.loadPlugin(apk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • AndroidManifest.xml adds the permission to read and write SD card. If the plug-in apk is obtained from the Internet, you also need to add the Internet permission (I'm just a simple test, and directly put the plug-in apk into the SD card)
 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

4.3 plug in project

  • Add in build.gradle under the project root directory
dependencies {
 classpath 'com.didi.virtualapk:gradle:0.9.0'
}
  • Add dependencies and plug-in configuration information at the top of the App's build.gradle
 apply plugin: 'com.didi.virtualapk.plugin'//Note that this is the end of plugin, and the host ends with host 

//The plug-in configuration information is placed at the bottom of the file
virtualApk {
//For the packageId in the plug-in resource table, ensure that different plug-ins have different packageId
packageId = 0x6f

// The path of the application module of the host project. The construction of the plug-in depends on this path. My host project and the plug-in project are in the same level directory, so it is written below
targetHost = '../VirtualAPKHostDemo/app' 

//The default is true. If the plug-in has a class that references the host, this option can make the plug-in and the host consistent
applyHostMapping = true 
[]( )4.5 Gradle Version recommendation
------------------------------------------------------------

at present VirtualAPK The builder is still improving, so plug-in construction is recommended Gradle 2.14.1 Version, 3.x The version may have adaptation problems. We are trying to be compatible with various versions Gradle edition. At the same time, wait VirtualAPK After the builder is improved, its code will also be open source. Please look forward to it.
----------------------------

at present VirtualAPK The builder is still improving, so plug-in construction is recommended Gradle 2.14.1 Version, 3.x The version may have adaptation problems. We are trying to be compatible with various versions Gradle edition. At the same time, wait VirtualAPK After the builder is improved, its code will also be open source. Please look forward to it.
 

Posted by mospeed on Thu, 02 Sep 2021 18:31:29 -0700