上個星期,公司裏有一個小的講座,對插件化進行了簡單的介紹,所以決定開始研究一下這方面的知識。php
在網上查了一些相關的資料,發現了Small
這個開源的插件化框架,所以打算從它入手,經過它的內部實現,學習一下插件化的相關原理。這篇文章是個開篇,先從一個簡單的例子開始,把環境給搭建好。先給你們分享一些這幾天查閱的資料,若是你們有比較好的文章也能夠留言或者私信我:android
Small Github 官網 Small Issues Small 快速入門github
Android Small 源碼分析 (一) 啓動流程 Android Small 源碼分析 (二) 插件加載過程json
Android Small 插件化框架 -- 啓動插件 Activity 源碼解析(上) Android Small 插件化框架 -- 啓動插件 Activity 源碼解析(下) Android Small 插件化框架 -- Android 應用類加載機制 Android Small 插件化框架 -- 類加載實現解析app
對於Small
來講,一個最簡單的框架分爲三個部分:框架
bundle.json
,用於宿主和插件之間的路由。本文所用的是一個最簡單的例子,所以在代碼上基本不會有什麼問題,主要是環境上的區別,遇到編譯不過的問題能夠多多百度,下面是我所採用的環境:maven
Android Studio
版本:Android Studio 3.0
Gradle
版本:gradle-3.5-all.zip
compileSdkVersion
:24
buildToolsVersion
:24.0.2
完整的例子能夠查看 github.com/imZeJun/Sma…ide
整個具體的實現分爲五步:
build.gradle
文件,引入Small
插件這裏比較關鍵的一點,是須要在新建工程/宿主模塊的時候,將包名修正爲com.demo.small
,這是爲了和之後的lib/app
模塊造成統一:
對於項目的build.gradle
,修改包含如下三個部分:
dependencies
節點中引入遠程依賴。apply plugin
應用插件。Small
代碼庫版本。buildscript {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
//1.引入Small依賴,必選。
classpath 'net.wequick.tools.build:gradle-small:1.2.0-alpha3'
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//2.應用插件,必選。
apply plugin: 'net.wequick.small'
//3.配置Small的代碼庫版本,須要放在第2步的下面,不然會報錯,可選。
small {
aarVersion = '1.2.0-alpha3'
}
複製代碼
這裏用到的插件模塊很簡單,就是位於另外一個模塊中的Activity
,選擇File -> New -> New Module
:
Phone & Tablet Module
:那麼要以app.xxx
結尾Android Library
,那麼要以lib.xxx
結尾這裏,咱們先演示第一種:
PlugActivity
,它的佈局爲:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.demo.small.app.main.PlugActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/black"
android:text="PlugActivity started success"/>
</FrameLayout>
複製代碼
(a) 配置路由協議
接下來要在宿主模塊中進行路由配置,咱們在宿主模塊上單擊右鍵,新建一個assets
文件夾,以後在assets
文件夾中,新建一個路由文件,bundle.json
文件,注意assets
文件夾從Project
視圖上所處的位置以下圖所示,千萬不要放錯地方了:
bundle.json
中,咱們聲明插件模塊:
{
"version": "1.0.0",
"bundles": [
{
"uri": "main",
"pkg": "com.demo.small.app.main"
}
]
}
複製代碼
** (b) 在宿主模塊的自定義 Application 中進行預加載**
public class SmallHostApp extends Application {
public SmallHostApp() {
//Small初始化。
Small.preSetUp(this);
}
}
複製代碼
** (c) 將自定義的 Application 配置到宿主模塊的 AndroidManifest.xml 中**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.demo.small">
<application
android:name=".app.SmallHostApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LaunchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
複製代碼
** (d) 在啓動 Activity 的 onCreate() 方法中加載插件,點擊按鈕後跳轉到插件的Activity**
public class LaunchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
setUp();
}
private void setUp() {
Small.setUp(this, new net.wequick.small.Small.OnCompleteListener() {
@Override
public void onComplete() {
Log.d("LaunchActivity", "onComplete");
}
});
}
public void startPlugActivity(View view) {
Small.openUri("main", LaunchActivity.this);
}
}
複製代碼
最後一步,就是進行編譯和安裝,編譯時:
./gradlew buildLib -q && ./gradlew buildBundle -q
複製代碼
./gradlew assembleDebug && adb install -r app/build/outputs/apk/app-debug.apk
複製代碼
./gradlew cleanLib -q && ./gradlew cleanBundle -q
複製代碼