插件化知識梳理(2) Small 框架之如何引入公共庫插件

1、前言

插件化知識梳理(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')讓應用插件模塊來引用它,以調用它模塊中所定義的方法或使用主題樣式。
  • 在編譯階段,共同庫插件會被打包成爲一個可獨立更新的插件。

下面,咱們就分這兩個方面,這介紹一下使用創建公共庫插件。網絡

2、公共庫

2.1 新建 lib.utils 模塊做爲工具類共同庫

(a) 新建插件庫 Android Library app

(b) 注意包名的定義,要分爲兩個部分
新建完畢以後,咱們的項目結構變爲下面這樣:
(c) 編寫工具類代碼

lib.utils模塊中,引入第三方庫Glide,用於圖片的加載:框架

dependencies {
    //引入第三方庫Glide。
    compile 'com.github.bumptech.glide:glide:3.7.0'
}
複製代碼

新建一個簡單的圖片加載類ImageLoaderide

public class ImageLoader {

    public static void loadImage(Context context, String imgUrl, ImageView img) {
        Glide.with(context).load(imgUrl).into(img);
    }
}
複製代碼

2.2 新建 lib.style 做爲資源類共同庫

(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>
複製代碼

2.3 修改插件模塊 app.main

** (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);
    }
}
複製代碼

2.4 修改宿主模塊的 bundle.json 文件:

{
  "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"
    }
  ]
}
複製代碼

2.6 從新編譯

2.7 最終效果


更多文章,歡迎訪問個人 Android 知識梳理系列:

相關文章
相關標籤/搜索