組件化中需要一個app殼工程,這個殼工程中不處理任何業務,就只是一個空殼,由它將所需要的各個組件模塊組合起來,構成一個完整的應用。
-
直接new一個module
- 接着進入app的
AndroidManifest
文件將註冊Activity的相關代碼也搬到ModuleMain
模塊的AndroidManifest
中去,只留下application
標籤。
這裏注意組件化項目中每個Module都會有自己的AndroidManifest
文件,最後打包時會將這些文件合併成一個文件,所以會出現application
標籤中的屬性重複問題,要在app的AndroidManifest
文件中添加如下兩行代碼:
//加在manifest標籤裏
xmlns:tools="http://schemas.android.com/tools"
//加在application標籤裏
tools:replace="android:name,android:label,android:icon, android:theme,android:allowBackup"
這裏的name
、label
、icon
、theme
、allowBackup
都可能會有重複,所以全部寫上之間用逗號隔開。 完整AndroidManifest
如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:name,android:label,android:icon, android:theme,android:allowBackup">
</application>
</manifest>
接着將app目錄下的build.gradle內容複製到mainmodule下的build.gradle裏面,然後將app下的res資源目錄及文件拷貝到mainmodule的res目錄下。
需要在project下的自定義的gradle文件里加入一個判斷是模塊還是應用的boolean值,如本項目在fjs.gradle裏添加一個布爾類型的isModule值
ext{
isModule = false //true:作爲Lib組件存在, false:作爲application存在
android = [
compileSdkVersion :28,
buildToolsVersion :"29.0.0",
minSdkVersion :18,
targetSdkVersion :28,
versionCode :1,
versionName :"1.0"//必須是int或者float,否則影響線上升級
]
version = [
androidSupportSdkVersion:"28.0.0",
constraintLayoutVersion:"1.1.3",
rxJavaVersion:"2.0.1",
junitVersion:"4.12",
runnerVersion:"1.0.2",
espressoCoreVersion:"3.0.2",
]
dependencies = [
"appcompat-v7":"com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}",
"constraint-layout":"com.android.support.constraint:constraint-layout:${version["constraintLayoutVersion"]}",
"rxjava":"io.reactivex.rxjava2:rxjava:${version["rxJavaVersion"]}",
"junit":"junit:junit:${version["junitVersion"]}",
"runner":"com.android.support.test:runner:${version["runnerVersion"]}",
"espresso-core":"com.android.support.test.espresso:espresso-core:${version["espressoCoreVersion"]}"
]
}
- 在mainmodule裏的build.gradle裏添加一個判斷,如下所示:
- 然後在app的build.gradle裏面的dependencies裏添加如下內容:
if (rootProject.ext.isModule){
implementation project(":mainmodule")
}
- 在app和其它模塊裏分別新建一個Application,並在各自的模塊裏的AndroidManifest.xml文件裏的application標籤下引入新建的Application。
- 然後同步即可。