- 首先在主工程目錄下的
gradle.properties建立變量,控制當(dāng)前moudle是單獨運行還是moudle形式
image.png
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# 注意不要帶有標(biāo)點符號,比如xx=true; 會被轉(zhuǎn)換為false
isSampleDebug=true
-
然后在對應(yīng)moudle目錄下建立一個用于測試的清單文件,清單文件需要設(shè)置默認(rèn)啟動的activity,還有一些調(diào)試時候需要用到res也要設(shè)置下。
image.png
Debug模式下的清單文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lexinfintech.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity android:name=".activity.ComponentListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
** module模式下的清單文件**
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lexinfintech.sample">
<application>
<activity android:name=".activity.ComponentListActivity" />
</application>
</manifest>
- 在對應(yīng)moudle目錄下的gradle里面做如下配置
1、獨立工程需要apply plugin: 'com.android.application'
2、獨立工程需要設(shè)置applicationID
3、獨立工程需要都默認(rèn)啟動的主activity,這里需要設(shè)置不同的清單文件和資源引用位置
//單獨工程和lib的聲明不同
if (isSampleDebug.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
android {
//限制資源名稱
resourcePrefix project.getName() + "_"
defaultConfig {
//作為單獨工程,需要設(shè)置applicationID
if (isSampleDebug.toBoolean()) {
applicationId "com.lexinfintech.sample"
}
}
sourceSets {
main {
// 單獨調(diào)試與集成調(diào)試時使用不同的 AndroidManifest.xml 文件
if (isSampleDebug.toBoolean()) {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
//多增加一個資源調(diào)試路徑 src/main/debug/res
res.srcDirs = ['src/main/res', 'src/main/debug/res']
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'
java {
//module 模式下要 排除src/test/文件夾下的所有文件
exclude 'src/debug/'
}
}
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api project(':base')
}
- 最后還需要在主工程設(shè)置一下引用關(guān)系
//主工程在組件作為單獨工程運行的時候,要過濾到此組件,因為application是不能直接引用application的
if(!isSampleDebug.toBoolean()){
runtimeOnly project(':sample')
}
- 這樣moudle就可以作為獨立工程跑起來了

