- 插件化与组件化的区别
插件化:插件化是把整个APP拆分成多个模块,这些模块有一个宿主和多个插件,每个模块都是一个APK,最终打包的适合将宿主APK和模块APK合并或者分开打包。插件化有助于减少宿主APP项目功能并减少宿主APK文件过大的问题。
组件化 :组件化是把整个APP拆分成多个模块,每个模块都是一个组件(Module),开发的过程中我们可以让这些组件相互依赖或者单独调试部分组件等,但是最终发布的时候是将这些组件合并统一成一个apk,这就是组件化开发。
- 插件化的好处
1.宿主和插件分开编译
编译时只需要编译宿主app,插件app是在编译好后下发到宿主app里的。
并发开发
2.宿主app什么时候发布版本跟插件app什么时候开发完没有关系,宿主app只要开发完并且为插件app提供一个入口就可以了。
动态更新插件
3.插件app在开发完后下发到宿主app里,点击相应的入口就可以跳转到最新版的插件app了。
4.按需下载模块
5.解决方法数或变量数爆棚(65536)
- 技术角度
插件化主要是解决如何启动未安装的APK里面的类(主要是四大组件),主要问题涉及如何加载类、如何加载资源、如何管理组件生命周期。
1.类加载
Android对于外部的dex文件,主要通过DexClassLoader类加载,因此,只需要给定插件的路径,就可以构造对应的类加载器:
private DexClassLoader createDexClassLoader(String apkPath) {
File dexOutputDir = mContext.getDir("dex", Context.MODE_PRIVATE);
DexClassLoader loader = new DexClassLoader(apkPath, dexOutputDir.getAbsolutePath(),
null, mContext.getClassLoader());
return loader;
}
2.资源加载
Android系统通过Resource对象加载资源,因此只需要添加资源(即apk文件)所在路径到AssetManager中,即可实现对插件资源的访问。
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod(addAssetPathMethod, String.class);
addAssetPath.invoke(assetManager, apkPath);
Resources pluginRes = new Resources(assetManager,
mContext.getResources().getDisplayMetrics(),
mContext.getResources().getConfiguration());
pluginApk = new PluginApk(pluginRes);
pluginApk.classLoader = createDexClassLoader(apkPath);
3.调用插件Activity
通过在宿主APP中添加一个空壳Activity作为代理(Proxy),系统对该Activity的回调都会映射到插件Activity,如此便可以实现通过系统来管理插件的生命周期。这种方式十分直观,但是需要所有的插件Activity都继承这个用作代理的PluginActivity(Demo中的命名)
3.1.1 代理实现
首先建立一个PluginManager类来实现插件的加载:
public class PluginManager {
static class PluginMgrHolder {
static PluginManager sManager = new PluginManager();
}
private static Context mContext;
Map sMap = new HashMap<>();
public static PluginManager getInstance() {
return PluginMgrHolder.sManager;
}
public PluginApk getPluginApk(String packageName) {
return sMap.get(packageName);
}
public static void init(Context context) {
mContext = context.getApplicationContext();
}
public final void loadApk(String apkPath) {
PackageInfo packageInfo = queryPackageInfo(apkPath);
if (packageInfo == null || TextUtils.isEmpty(packageInfo.packageName)) {
return;
}
// check cache
PluginApk pluginApk = sMap.get(packageInfo.packageName);
if (pluginApk == null) {
pluginApk = createApk(apkPath);
if (pluginApk != null) {
pluginApk.packageInfo = packageInfo;
sMap.put(packageInfo.packageName, pluginApk);
} else {
throw new NullPointerException("PluginApk is null");
}
}
}
private PluginApk createApk(String apkPath) {
String addAssetPathMethod = "addAssetPath";
PluginApk pluginApk = null;
try {
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod(addAssetPathMethod, String.class);
addAssetPath.invoke(assetManager, apkPath);
Resources pluginRes = new Resources(assetManager,
mContext.getResources().getDisplayMetrics(),
mContext.getResources().getConfiguration());
pluginApk = new PluginApk(pluginRes);
pluginApk.classLoader = createDexClassLoader(apkPath);
} catch (IllegalAccessException
| InstantiationException
| NoSuchMethodException
| InvocationTargetException e) {
e.printStackTrace();
}
return pluginApk;
}
private PackageInfo queryPackageInfo(String apkPath) {
PackageInfo packageInfo = mContext.getPackageManager().getPackageArchiveInfo(apkPath,
PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
if (packageInfo == null) {
return null;
}
return packageInfo;
}
private DexClassLoader createDexClassLoader(String apkPath) {
File dexOutputDir = mContext.getDir("dex", Context.MODE_PRIVATE);
DexClassLoader loader = new DexClassLoader(apkPath, dexOutputDir.getAbsolutePath(),
null, mContext.getClassLoader());
return loader;
}
public void startActivity(Intent intent) {
Intent pluginIntent = new Intent(mContext, ProxyActivity.class);
Bundle extra = intent.getExtras();
// complicate if statement
if (extra == null || !extra.containsKey(Constants.PLUGIN_CLASS_NAME) && !extra.containsKey(Constants.PACKAGE_NAME)) {
try {
throw new IllegalAccessException("lack class of plugin and package name");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
pluginIntent.putExtras(intent);
pluginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(pluginIntent);
}
}
表示一个Apk文件:
public class PluginApk {
public PackageInfo packageInfo;
public DexClassLoader classLoader;
public Resources pluginRes;
public PluginApk(Resources pluginRes) {
this.pluginRes = pluginRes;
}
}
所有插件Activity都要继承一个父类:
public abstract class PluginActivity extends Activity implements Pluginable, Attachable {
public final static String TAG = PluginActivity.class.getSimpleName();
protected Activity mProxyActivity;
private Resources mResources;
PluginApk mPluginApk;
@Override
public void attach(Activity proxy, PluginApk apk) {
mProxyActivity = proxy;
mPluginApk = apk;
mResources = apk.pluginRes;
}
@Override
public void setContentView(int layoutResID) {
mProxyActivity.setContentView(layoutResID);
}
@Override
public void setContentView(View view) {
mProxyActivity.setContentView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
mProxyActivity.setContentView(view, params);
}
@Override
public View findViewById(int id) {
return mProxyActivity.findViewById(id);
}
@Override
public Resources getResources() {
return mResources;
}
@Override
public WindowManager getWindowManager() {
return mProxyActivity.getWindowManager();
}
@Override
public ClassLoader getClassLoader() {
return mProxyActivity.getClassLoader();
}
@Override
public Context getApplicationContext() {
return mProxyActivity.getApplicationContext();
}
@Override
public MenuInflater getMenuInflater() {
return mProxyActivity.getMenuInflater();
}
@Override
public Window getWindow() {
return mProxyActivity.getWindow();
}
@Override
public Intent getIntent() {
return mProxyActivity.getIntent();
}
@Override
public LayoutInflater getLayoutInflater() {
return mProxyActivity.getLayoutInflater();
}
@Override
public String getPackageName() {
return mPluginApk.packageInfo.packageName;
}
@Override
public void onCreate(Bundle bundle) {
// DO NOT CALL super.onCreate(bundle)
// following same
VLog.log(TAG + ": onCreate");
}
@Override
public void onStart() {
}
@Override
public void onResume() {
}
@Override
public void onStop() {
}
@Override
public void onPause() {
}
@Override
public void onDestroy() {
}
}
这个类只是一个壳,系统会通过触发对应的方法的具体实现:
public class ProxyActivity extends Activity {
LifeCircleController mPluginController = new LifeCircleController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPluginController.onCreate(getIntent().getExtras());
}
@Override
public Resources getResources() {
// construct when loading apk
Resources resources = mPluginController.getResources();
return resources == null ? super.getResources() : resources;
}
@Override
public Resources.Theme getTheme() {
Resources.Theme theme = mPluginController.getTheme();
return theme == null ? super.getTheme() : theme;
}
@Override
public AssetManager getAssets() {
return mPluginController.getAssets();
}
@Override
protected void onStart() {
super.onStart();
mPluginController.onStart();
}
@Override
protected void onResume() {
super.onResume();
mPluginController.onResume();
}
@Override
protected void onStop() {
super.onStop();
mPluginController.onStop();
}
@Override
protected void onPause() {
super.onPause();
mPluginController.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mPluginController.onDestroy();
}
}
这个类是系统实际启动的类,其主要逻辑由LifeCircleController负责:
public class LifeCircleController implements Pluginable {
Activity mProxy;
PluginActivity mPlugin;
Resources mResources;
Resources.Theme mTheme;
PluginApk mPluginApk;
String mPluginClazz;
public LifeCircleController(Activity activity) {
mProxy = activity;
}
public void onCreate(Bundle bundle) {
mPluginClazz = bundle.getString(Constants.PLUGIN_CLASS_NAME);
String packageName = bundle.getString(Constants.PACKAGE_NAME);
mPluginApk = PluginManager.getInstance().getPluginApk(packageName);
try {
mPlugin = (PluginActivity) loadPluginable(mPluginApk.classLoader, mPluginClazz);
mPlugin.attach(mProxy, mPluginApk);
mResources = mPluginApk.pluginRes;
mPlugin.onCreate(bundle);
} catch (Exception e) {
VLog.log("Fail in LifeCircleController onCreate");
VLog.log(e.getMessage());
e.printStackTrace();
}
}
private Object loadPluginable(ClassLoader classLoader, String pluginActivityClass)
throws Exception {
Class> pluginClz = classLoader.loadClass(pluginActivityClass);
Constructor> constructor = pluginClz.getConstructor(new Class[] {});
constructor.setAccessible(true);
return constructor.newInstance(new Object[] {});
}
@Override
public void onStart() {
if (mPlugin != null) {
mPlugin.onStart();
}
}
@Override
public void onResume() {
if (mPlugin != null) {
mPlugin.onResume();
}
}
@Override
public void onStop() {
mPlugin.onStop();
}
@Override
public void onPause() {
mPlugin.onPause();
}
@Override
public void onDestroy() {
mPlugin.onDestroy();
}
public Resources getResources() {
return mResources;
}
public Resources.Theme getTheme() {
return mTheme;
}
public AssetManager getAssets() {
return mResources.getAssets();
}
}
有点像Activity源码的外观模式,内部的分工和职责划分对于使用者是不可见的。
最后在主工程启动插件:
Intent intent = new Intent();
intent.putExtra(Constants.PACKAGE_NAME, PLUGIN_PACKAGE_NAME);
intent.putExtra(Constants.PLUGIN_CLASS_NAME, PLUGIN_CLAZZ_NAME);
mPluginManager.startActivity(intent);
插件Activity如下:
public class MainActivity extends PluginActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Plugin App");
((ImageView) findViewById(R.id.iv_logo)).setImageResource(R.drawable.android);
}
}