在實際的項目開發過程中,我們經常會遇到與各種第三方應用的對接,在實際的產品中,我們也開發了SDK來方便第三方的二次開發,那接下來大家一起去看看詳解Android插件化-RePlugin項目集成與使用吧!
1.什么是RePlugin?
在Android開發領域,有關插件化的討論一直熱度不減。目前市面上的插件化方案雖然很多,但多數只能實現某些功能的插件化,距離開發者的預期尚有相當差距。對此,在近期GMTC全球移動技術大會上,360手機衛士主程序架構負責人張炅軒宣布,360的插件化框架RePlugin已經可以實現“全面插件化”,同時具有出色的穩定性和靈活性,可適用于各種類型的應用上。
“RePlugin預計7月份開源,這將是我們獻給安卓世界最好的禮物?!?60如是說。
2.RePlugin有什么用?
RePlugin是一套完整的、穩定的、適合全面使用的,占坑類插件化方案,由360手機衛士的RePlugin Team研發,也是業內首個提出”全面插件化“(全面特性、全面兼容、全面使用)的方案。
3.RePlugin官方介紹
其主要優勢有:
一、集成主工程
1、在項目根目錄的 build.gradle 下添加 RePlugin Host Gradle 依賴:
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' // 1、添加RePlugin Host Gradle依賴 classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.1' }}2、在 app/build.gradle 下添加 RePlugin Host Library 依賴(為了更清晰的表示出代碼添加的位置,將原有代碼也一并貼出):
apply plugin: 'com.android.application'
android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "cn.codingblock.repluginstudy" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}apply plugin: 'replugin-host-gradle'// 集成 RePlugin 添加的配置// 集成 RePlugin 添加的配置repluginHostConfig { useAppCompat = true // 如果項目需要支持 AppComat,則需要將此配置置為 true // 如果應用需要個性化配置坑位數量,則需要添加以下代碼進行配置// countNotTranslucentStandard = 6// countNotTranslucentSingleTop = 2// countNotTranslucentSingleTask = 3// countNotTranslucentSingleInstance = 2}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.qihoo360.replugin:replugin-host-lib:2.2.1' // 集成 RePlugin 添加的配置 testCompile 'junit:junit:4.12'}以上代碼有三點需要注意:
countNotTranslucentStandard = 6countNotTranslucentSingleTop = 2countNotTranslucentSingleTask = 3countNotTranslucentSingleInstance = 2
3、讓工程的 Application 直接繼承自 RePluginApplication:
public class MyApplication extends RePluginApplication { } 當然,同時不要忘了在 AndroidManifest 對 MyApplication 的相關配置。
說明:有時候由于項目原有結構的需要,我們可能不能直接使用繼承 RePluginApplication 的方式,這個問題看來 RePlugin 開發者也想到了,所以還特地多了一種選擇,下面是項目的 Application 不繼承 RePluginApplication 的方式:
public class MyApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); RePlugin.App.attachBaseContext(this); } @Override public void onCreate() { super.onCreate(); RePlugin.App.onCreate(); } @Override public void onLowMemory() { super.onLowMemory(); RePlugin.App.onLowMemory(); } @Override public void onTrimMemory(int level) { super.onTrimMemory(level); RePlugin.App.onTrimMemory(level); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); RePlugin.App.onConfigurationChanged(newConfig); }}二、集成插件
新建一個工程做為插件APP,這里為了方便起見,直接在主工程中新建了一個 Module。
1、同集成主工程類似,在根目錄的 build.gradle 添加 RePlugin Plugin Gradle 依賴(若是單獨創建插件工程,則不需要添加注釋1下面的代碼):
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' // 1、添加RePlugin Host Gradle依賴(主工程用) classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.1' // 2、添加RePlugin Plugin Gradle依賴(插件工程用) classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.1' }}2、在 app/build.gradle 中添加 replugin-plugin-gradle 插件和 replugin-plugin-lib 依賴:
apply plugin: 'com.android.application'android { ...}apply plugin: 'replugin-plugin-gradle' // 集成 RePlugin 添加的配置dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.1' // 集成 RePlugin 添加的配置 testCompile 'junit:junit:4.12'}三、管理插件
RePlugin 對插件定義兩種方式一種是外置插件、一種是內置插件。
(一)外置插件的安裝(升級)、啟動、卸載
安裝插件:
PluginInfo pluginInfo = RePlugin.install(Environment.getExternalStorageDirectory().getPath().toString() + "/plugin1.apk");System.out.println(pluginInfo);
同時別忘了添加文件讀寫的權限。 輸出日下:
?
?
?
?
?
?
?
?
?
?
""""
?
?
""""
?
?
""""""
?
?
?
?
?
?
""
?
?
?
<1>"""""""""""""""""""""""""">
?
“”
?
?
?
?
以上就是武林技術頻道小編給大家介紹的詳解Android插件化-RePlugin項目集成與使用,看完這篇文章,大家趕快去操作看看,有任何問題及時反饋給小編哦。
新聞熱點
疑難解答