Android 进阶之路Android开发经验谈Android开发

VirtualAPK插件框架简单使用

2017-11-12  本文已影响244人  闲庭

APP插件化/组件化框架分析

一、宿主程序集成

1、在宿主工程根目录的build.gradle添加依赖

dependencies {
 classpath 'com.didi.virtualapk:gradle:0.9.0'
}

dependencies {
 classpath 'com.didi.virtualapk:gradle:0.9.0'
}

2、在App的工程模块的build.gradle添加使用gradle插件

apply plugin: 'com.didi.virtualapk.host'

3、在App的工程模块的build.gradle添加VirtualAPK SDK compile依赖

dependencies {
 compile 'com.didi.virtualapk:core:0.9.0'
}

4、在App的工程模块proguard-rules.pro文件添加混淆规则

-keep class com.didi.virtualapk.internal.VAInstrumentation { *; }
-keep class com.didi.virtualapk.internal.PluginContentResolver { *; }
-dontwarn com.didi.virtualapk.**
-dontwarn android.content.pm.**
-keep class android.** { *; }

5、MyApplication类是继承了Application,覆写attachBaseContext函数,进行插件SDK初始化工作

@Override
protected void attachBaseContext(Context base)  {
   super.attachBaseContext(base);
   PluginManager.getInstance(base).init();
}

6、在使用插件之前加载插件,可以根据具体业务场景选择合适时机加载

private void loadPlugin(Context base) {
        PluginManager pluginManager = PluginManager.getInstance(base);
        File apk = new File(getExternalStorageDirectory(), "plugin.apk");
        if (apk.exists()) {
            try {
                pluginManager.loadPlugin(apk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "SDcard根目录未检测到plugin.apk插件", Toast.LENGTH_SHORT).show();
        }
    }

经过上述6步后,VirtualAPK插件功能就集成到宿主中了,宿主打包和运行方式没有任何改变。接下来看下插件工程如何集成和构建的。

二、插件程序集成

1. 在插件工程根目录的build.gradle添加依赖

   dependencies {
    classpath 'com.didi.virtualapk:gradle:0.9.0'
   }

2. 在App的工程模块的build.gradle添加使用gradle插件和插件配置信息,信息需要放在文件最下面

   apply plugin: 'com.didi.virtualapk.plugin'
   ...
   ...
   // 插件配置信息,放在文件最下面
   virtualApk {
    packageId = 0x6f             // 插件资源id,避免资源id冲突
    targetHost='../host/app'      // 宿主工程的路径
    applyHostMapping = true      // 插件编译时是否启用应用宿主的apply mapping
   }

3. 最后一步生成插件,需要使用Gradle命令

   gradlew clean assemblePlugin
   或者
   ./gradlew clean assemblePlugin

成功后会在app ->build ->outputs -> plugin ->release 中生成一个apk文件,文件名以包名+时间的形式,改成你想要的名称,比如说pluginA.apk,我这里放到手机根目录(模拟从远程下载的插件apk)。

三、宿主程序加载插件apk

1.在宿主程序中初始化PluginManager

public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        PluginManager.getInstance(base).init();
    }
}

2.在宿主程序中调用插件。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public static final String IMG1_URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510290258030&di=e851b9a55dca58e848010c91ccbabadc&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F838ba61ea8d3fd1f571a04d03a4e251f94ca5fcc.jpg";
    public static final String IMG2_URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510290264866&di=b9616f5a0c5b7d73d0cceb9a32cf53d5&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20161102%2F93a9cfc021ed44b28af7a143be6ba017_th.jpeg";
    public static final String PLUGIN_NAME = "pluginA.apk";
    public static final String PLUGIN_PKG_NAME = "com.liujc.plugin";
    public static final String PLUGIN_CLASS_NAME = "com.liujc.plugin.PluginMainActivity";
    private TextView showPluginInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        loadPlugin(this);

    }

    private void initView() {
        ImageView homeIconIv1 = (ImageView) findViewById(R.id.iv_home_icon1);
        ImageView homeIconIv2 = (ImageView) findViewById(R.id.iv_home_icon2);
        Button imageBrowserBtn = (Button) findViewById(R.id.btn_image_browser);
        imageBrowserBtn.setOnClickListener(this);
        Glide.with(this).load(IMG1_URL).into(homeIconIv1);
        Glide.with(this).load(IMG2_URL).into(homeIconIv2);
        showPluginInfo = (TextView) findViewById(R.id.show_plugin_info);
    }

    @Override
    public void onClick(View v) {
        if (PluginManager.getInstance(this).getLoadedPlugin(PLUGIN_PKG_NAME) == null) {
            Toast.makeText(getApplicationContext(),
                    "插件未加载,请尝试重启APP", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent();
        intent.setClassName(PLUGIN_PKG_NAME, PLUGIN_CLASS_NAME);
        startActivity(intent);
    }

    private void loadPlugin(Context base) {
        PluginManager pluginManager = PluginManager.getInstance(base);
        File apk = new File(getExternalStorageDirectory(),PLUGIN_NAME);
        if (apk.exists()) {
            try {
                pluginManager.loadPlugin(apk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "SDcard根目录未检测到plugin.apk插件", Toast.LENGTH_SHORT).show();
        }
        LoadedPlugin loadedPlugin = pluginManager.getLoadedPlugin(PLUGIN_PKG_NAME);
        if (loadedPlugin != null){
            String versionInfo = "Host版本信息:"+getAppVersionName(MainActivity.this)+"\n"+
                    "插件版本信息:"+getAppVersionName(loadedPlugin.getPluginContext());

            showPluginInfo.setText(versionInfo);
        }
    }

    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        int versioncode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            versioncode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return "V"+versionName+" | "+versioncode;
    }
}

这里分别显示了宿主和插件的版本号,可便于后期插件更新用,稍后整理下上传demo,下一篇讲述阿里Atlas组件化框架的使用方法。

源码奉上,自行参考

上一篇下一篇

猜你喜欢

热点阅读