在 插件化知識梳理(1) - Small 框架之如何引入應用插件 中,咱們簡要地介紹瞭如何使用Small
框架來經過插件實現一個Activity
的跳轉,也就是app.main
,這裏的app.main
咱們稱爲應用插件,除了應用插件外,還有一種稱爲公共庫插件。android
對於這兩種插件的職責,Small
官方建議的基本原則爲git
- 公共庫插件
lib
插件模塊,包括統計、地圖、網絡、圖片等庫。utils
分離出來封裝成一個lib.utils
插件lib.style
插件
- 應用插件
app
模塊,他們能夠依賴lib
模塊,顯示調用lib
中的各個API
app.main
主插件總結下來就是:github
在項目當中,許多個模塊有可能用到一些共有的東西,例如圖片加載、網絡請求、公共控件等,那麼咱們就有必要將這些東西封裝成共同庫讓各個模塊去調用,通常來講,能夠分爲如下兩種:json
對於上面這兩類,咱們通常稱爲公共庫插件,它的命名方式爲lib.xxx
。公共庫插件模塊分爲兩個階段:bash
compile project(':lib_module_name')
讓應用插件模塊來引用它,以調用它模塊中所定義的方法或使用主題樣式。下面,咱們就分這兩個方面,這介紹一下使用創建公共庫插件。網絡
(a) 新建插件庫 Android Library app
(b) 注意包名的定義,要分爲兩個部分 新建完畢以後,咱們的項目結構變爲下面這樣: (c) 編寫工具類代碼在lib.utils
模塊中,引入第三方庫Glide
,用於圖片的加載:框架
dependencies {
//引入第三方庫Glide。
compile 'com.github.bumptech.glide:glide:3.7.0'
}
複製代碼
新建一個簡單的圖片加載類ImageLoader
:ide
public class ImageLoader {
public static void loadImage(Context context, String imgUrl, ImageView img) {
Glide.with(context).load(imgUrl).into(img);
}
}
複製代碼
(a) 新建插件庫 Android Library 工具
(b) 在 res 目錄下新建 styles.xml 文件在styles.xml
文件中,咱們定義一些公共的樣式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="StyleTextViewTitle">
<item name="android:textSize">45px</item>
<item name="android:textColor">#333333</item>
<item name="android:padding">12dp</item>
</style>
<style name="StyleTextViewSubTitle">
<item name="android:textSize">27px</item>
<item name="android:textColor">#888888</item>
</style>
</resources>
複製代碼
** (a) 引入 lib.utils 和 lib.style **
dependencies {
//...
compile project(':lib.utils')
compile project(':lib.style')
}
複製代碼
(b) 在佈局文件中,使用 lib.style 的公共樣式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.demo.small.app.main.PlugActivity">
<ImageView
android:id="@+id/iv_header"
android:layout_gravity="center_horizontal"
android:layout_width="200dp"
android:layout_height="200dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Android"
android:layout_gravity="center_horizontal"
style="@style/StyleTextViewTitle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small 插件化方案適用於將一個APK拆分爲多個公共庫插件、業務模塊插件的場景。"
style="@style/StyleTextViewSubTitle"/>
</LinearLayout>
複製代碼
** (c) 在代碼中,使用 lib.utils 定義的接口**
public class PlugActivity extends AppCompatActivity {
private static final String IMG_URL = "http://i6.hexun.com/2017-06-02/189461191.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plug);
ImageView imageView = (ImageView) findViewById(R.id.iv_header);
//調用 lib.utils 中定義的接口。
ImageLoader.loadImage(this, IMG_URL, imageView);
}
}
複製代碼
{
"version": "1.0.0",
"bundles": [
{
"uri": "lib.utils",
"pkg": "com.demo.small.lib.utils"
},
{
"uri": "lib.style",
"pkg": "com.demo.small.lib.style"
},
{
"uri": "main",
"pkg": "com.demo.small.app.main"
}
]
}
複製代碼