VirtualApk 插件入门小试

时间:2021-04-21 08:51:14

1 官方资料

  滴滴开源Android插件方案:VirtualAPK

2 宿主App集成方法

  (1)在整个工程的build.gradle中添加依赖

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.didi.virtualapk:gradle:0.9.8.4'      // VirtualApk

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
}

   (2)在app模块(宿主App模块名,可改名)build.gradle添加插件引用

apply plugin: 'com.didi.virtualapk.host'    // VirtualApk  build.gradle文件顶部添加

   (3)在app模块build.gradle中添加依赖

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    compile 'com.didi.virtualapk:core:0.9.6'     // VirtualApk
}

   (4)在Application的attachBaseContext中添加PluginManager初始化代码

import com.didi.virtualapk.PluginManager;

public class TestApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        long start = System.currentTimeMillis();
        PluginManager.getInstance(base).init();    // 主要是这句
        Log.d("KLH", "use time:" + (System.currentTimeMillis() - start));
    }
}

   (5)在MainActivity的OnCreate中自动加载插件(这一步的时机可选,也可以是后续再加载),请注意这里加载插件的路径和apk名称,要约定好。

    private void loadPlugin() {
        PluginManager pm = PluginManager.getInstance(this);
        File apk = new File(Environment.getExternalStorageDirectory(), "test_plugin.apk");
        if (apk.exists()) {
            try {
                pm.loadPlugin(apk);
                showToast("插件加载成功!!!");
            } catch (Exception e) {
                e.printStackTrace();
                showToast("插件加载异常了。。。");
            }
        }
    }

  (6)启动插件Apk(这里以启动插件中的一个Activity为例)

Intent intent = new Intent();
intent.setClassName("com.klh.testplugin", "com.klh.testplugin.MainActivity");
startActivity(intent);

3 插件App集成方法

  插件工程是一个独立的App工程,只不过添加了VirtualAPK相关依赖,并且在编译时需要关联宿主工程目录(为了做一些信息同步与共享库删减)

  (1)在整个工程的build.gradle中添加依赖

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.didi.virtualapk:gradle:0.9.8.4'      // VirtualApk

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
}

  (2)在app模块的build.gradle中添加依赖

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    compile 'com.didi.virtualapk:core:0.9.6'     // VirtualApk
}

   (3)在app模块的build.gradle文件末尾中添加插件配置

// VirtualApk
apply plugin: 'com.didi.virtualapk.plugin'

virtualApk {
    // 要指定一个不重复的ID来区分插件资源,有效值范围:[0x02, 0x7E]
    packageId = 0x6a                    // the package id of Resources.

    // 宿主App的Application模块(绝对/相对)路径,插件的构建需要依赖此路径
    targetHost = '../testAndroid/app'   // the path of application module in host project.

    // 如果插件有引用宿主的类,那么此选项可以确保插件与宿主保持混淆一致
    applyHostMapping = true             //optional, default value: true.
}

   (4)编译方法:gradlew clean assemblePlugin

4 安装测试

  (1)安装宿主App

  (2)将编译好的插件改名与宿主约定一致,推送到指定目录。例如本文的约定路径是:/sdcard/test_plugin.apk

E:\VirtualShare\gitLab\research\05.VirtualApk\VATestPlugin>adb push app\build\outputs\apk\release\app-release-unsigned.apk /sdcard/test_plugin.apk
app\build\outputs\apk\release\app-release-unsigned.apk: 1 file pushed. 0.9 MB/s (27437 bytes in 0.030s)

5 其他

  插件不能直接访问宿主App的接口,可采用与宿主App集成相同的aar包来实现数据交互。