赞
踩
是滴滴2017年6月3号开源,框架功能完备,支持 Android 四大组件,良好的兼容性,且入侵性较低,作为加载耦合插件方案是较好选择
主要分为宿主程序 以及插件,下面是集成的步骤
首先创建宿主程序:(主要的apk)
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.didi.virtualapk:gradle:0.9.4'
· }
apply plugin: 'com.didi.virtualapk.host' android { compileSdkVersion 26 buildToolsVersion '25.0.2' defaultConfig { ....... } buildTypes { .... } } dependencies { ...... compile 'com.didi.virtualapk:core:0.9.1' }
说明
gridle版本建议应用2.3.3 太高了有问题;
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
PluginManager.getInstance(base).init();
}
class MainActivity : AppCompatActivity(), View.OnClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) loading.setOnClickListener(this) var cpuArch: String if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { cpuArch = Build.SUPPORTED_ABIS[0] } else { cpuArch = Build.CPU_ABI } Log.d("ryg", "onCreate cpu arch is " + cpuArch) loadPlugin(this) } //加载插件 override fun onClick(v: View?) { val pkg = "com.lishu.pluginproject" if (PluginManager.getInstance(this).getLoadedPlugin(pkg) == null) { Toast.makeText(this, "plugin ${pkg} not loaded", Toast.LENGTH_SHORT).show() return } // test Activity and Service val intent = Intent() intent.setClassName(this, "com.lishu.pluginproject.SecondActivity") startActivity(intent) } fun loadPlugin(base: Context) { val pluginManager = PluginManager.getInstance(base) val apk = File(Environment.getExternalStorageDirectory(), "my.apk") if (apk.exists()) { try { pluginManager.loadPlugin(apk) } catch (e: Exception) { e.printStackTrace() } } } }
到此,关于宿主程序的简单初始化,就结束了
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.didi.virtualapk:gradle:0.9.4'
}
apply plugin: 'com.didi.virtualapk.plugin'
到这一步,插件工程也初步的集成成功;
A problem occurred configuring project ':app'.
> Can't find ..\MyVirtualApk2\app\build\VAHost\versions.txt, please check up your host application
need apply com.didi.virtualapk.host in build.gradle of host application
有可能会遇到这样的问题,这个时候,我们要先编译下 宿主程序 ,然后再运行打包插件的命令
首先找插件app的build 目录:
\outputs\apk\app-release-unsigned.apk
实例命令: adb push app-release-unsigned.apk /sdcard/my.apk
注意:这个命令是进入到apk 目录后直接执行的;
然后启动宿主程序,就可以了;
//现在有着兼容性的问题bug;;;;
E:\demo\PluginProject\app\build\generated\source\r\release\android\support\design\R.java:1459: 错误: 不兼容的类型: <空值>无法转换为int
public static final int[] AppBarLayout = { null, null, null, 0x7f010048, 0x6f010011 };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。