看這篇文章的相信都提前對android插件化技術(shù)了解一二,最近有需求要使用360的開源RePlugin實(shí)現(xiàn)APK插件,說起這個的集成是簡單的,但了解這個的原理,其背后涉及的是一系列的知識點(diǎn),面廣,且復(fù)雜。陸續(xù)會一個一個整理。今天先集成replugin 并實(shí)現(xiàn)在宿主APP中直接打開插件APK。
RePlugin---GitHub地址
一.宿主app接入
1.添加 RePlugin Host Gradle 依賴
在項(xiàng)目根目錄的 build.gradle(注意:不是 app/build.gradle) 中添加 replugin-host-gradle 依賴:
buildscript {
dependencies {
classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'
...
}
}
2.添加 RePlugin Host Library 依賴
在 app/build.gradle 中需要應(yīng)用 replugin-host-gradle 插件,并添加 replugin-host-lib 依賴:
apply plugin: 'replugin-host-gradle'
repluginHostConfig {
/**
* 是否使用 AppCompat 庫 現(xiàn)在一般項(xiàng)目都會使用這個庫吧。。
* 不需要個性化配置時,無需添加
*/
useAppCompat = true
}
dependencies {
compile 'com.qihoo360.replugin:replugin-host-lib:2.2.4'
...
}
3.配置 Application 類
繼承RePluginApplication,別忘了在AndroidManifest.xml中配置
public class MainApplication extends RePluginApplication {
}
二、外置插件Apk 接入
什么是外置插件apk,要做到既可以“安裝到設(shè)備”,又可以“作為插件”使用。要注意是可以獨(dú)立安裝到手機(jī)上的,本身就是一個完整的應(yīng)用。插件也分為外置插件和內(nèi)置插件。我們文章示例的是外置插件。詳細(xì)看插件的管理
1.添加 RePlugin Plugin Gradle 依賴
在項(xiàng)目根目錄的 build.gradle(注意:不是 app/build.gradle) 中添加 replugin-plugin-gradle 依賴:
buildscript {
dependencies {
classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.4'
...
}
}
2.添加 RePlugin Plugin Library 依賴
在 app/build.gradle 中應(yīng)用 replugin-plugin-gradle 插件,并添加 replugin-plugin-lib 依賴
apply plugin: 'replugin-plugin-gradle'
dependencies {
compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'
...
}
三、外置插件準(zhǔn)備工作
通常我們下發(fā)插件是把插件放在服務(wù)器上通過網(wǎng)絡(luò)請求下發(fā),開啟服務(wù)下載apk到sdcard。但我們自己測試練習(xí)的時候沒有服務(wù)端支持,可以將apk直接放到本地存儲。(我就是直接在電腦端qq發(fā)送給手機(jī),在手機(jī)端查看apk地址為【/storage/emulated/0/tencent/qqfile_recv/xxx.apk】)
四、主程序調(diào)起插件
1.sd卡權(quán)限
如果插件APK放到了SD卡上,則請務(wù)必確保主程序中擁有SD卡權(quán)限(主程序Manifest要聲明,且ROM允許),否則會出現(xiàn)權(quán)限問題,當(dāng)然,放入應(yīng)用的files目錄則不受影響。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 在Application中配置簽名信息
沒有簽名的apk可能會攜帶病毒,所以說,一旦不做校驗(yàn),則不排除惡意人會劫持DNS或網(wǎng)絡(luò),并通過網(wǎng)絡(luò)來下發(fā)惡意插件,對您的應(yīng)用造成很不好的影響。所以首先我們要打開簽名校驗(yàn)setVerifySign(true) 即可,對于debug包的不進(jìn)行簽名校驗(yàn),最后把需要release包的簽名加入白名單.
@Override
public class MyApplication extends RePluginApplication
{
@Override
public void onCreate() {
super.onCreate();
//不會自動將“主程序簽名”加入進(jìn)來。如有需要,建議您自行加入。
RePlugin.addCertSignature("379C790B7B726B51AC58E8FCBCFE4567");
}
@Override
protected RePluginConfig createConfig() {
RePluginConfig c = new RePluginConfig();
//若為Debug環(huán)境下則無需校驗(yàn)簽名,只有Release才會校驗(yàn)
//打開簽名校驗(yàn)
c.setVerifySign(!BuildConfig.DEBUG);
return c;
}
}
3.調(diào)起方法

/**
* 模擬安裝或升級(覆蓋安裝)外置插件
* 注意:本demo將外置插件放置到/storage/emulated/0/tencent/qqfile_recv/xxx.apk目錄下
*/
private void simulateInstallExternalPlugin() {
//sd卡路徑
//獲取外部存儲的路徑返回絕對路徑的,就是你的設(shè)備SD卡的文件路徑
String pluginName = "plugin_test.apk";
String externalPluginPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tencent/qqfile_recv/" + pluginName;
String internalPluginPath = getFilesDir() + "/" + pluginName;
File externalPluginFile = new File(externalPluginPath);
File internalPluginFile = new File(internalPluginPath);
if (externalPluginFile.exists()) {
if (internalPluginFile.exists()) {
FileUtils.deleteQuietly(internalPluginFile);
}
//復(fù)制文件 更新操作
copyFile(externalPluginPath, internalPluginPath);
PluginInfo info = RePlugin.install(externalPluginPath);
if (info != null) {
RePlugin.startActivity(this, RePlugin.createIntent(info.getName(), "com.plugin.demo.MainActivity"));
} else {
Toast.makeText(this, "加載插件失敗", Toast.LENGTH_SHORT).show();
}
} else {
if (internalPluginFile.exists()) {
PluginInfo info = null;
if (internalPluginFile.exists()) {
info = RePlugin.install(internalPluginPath);
}
if (info != null) {
RePlugin.startActivity(this, RePlugin.createIntent(info.getName(), "com.plugin.demo.MainActivity"));
} else {
Toast.makeText(this, "加載插件失敗", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "沒有插件", Toast.LENGTH_SHORT).show();
}
}
}
/**
* 復(fù)制單個文件
*
* @param oldPath String 原文件路徑
* @param newPath String 復(fù)制后路徑
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字節(jié)數(shù) 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("復(fù)制單個文件操作出錯");
e.printStackTrace();
}
}
到這里我們就已經(jīng)實(shí)現(xiàn)了在一個app中直接打開一個插件apk了,文章不長,集成replugin并不復(fù)雜,主要在于學(xué)習(xí)深層的原理,現(xiàn)在大家可以動手配置測試一下了。