android studio模块化之模块划分简易版
2022-09-01 本文已影响0人
我爱田Hebe
模块也好,组件也罢,都是需要做好模块区分的。
一、新建module
修改你的模块名,比如你原来的包名是aaa.bbb.ccc,那么模块名就会是aaa.bbb.模块名
二、统一依赖
当我们建立好的module之后,发现主项目app和mudole各自的build.gradle文件都有一些相同的依赖,这个这个时候,需要进行管理,避免混乱。
一些相同的依赖
需要处理一下
处理之前的的两个build.gradle
app下build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.ttpj.testplu"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
home下build.gradle
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
minSdk 21
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
开始处理
- 1、切换到Project,新建一个
dependencies.gradle
文件 - 2、把一些共同的依赖放到
dependencies.gradle
- 3、主工程和mudole各自build文件的处理
- 4、在
gradle.properties
下放置标记,区分模块是否独立运行。
标记 singleModule
在gradle.properties
放置标记
# 配置模块化的运行方式,singleModule=true表示每个模块都独立运行作为单独的apk,fasle标志作为module,被app模块依赖。
singleModule=false
dependencies.gradle
apply plugin: 'org.jetbrains.kotlin.android'
android {
compileSdk 32
defaultConfig {
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
app下的build.gradle
apply plugin: 'com.android.application'
apply from: '../dependencies.gradle'
android {
defaultConfig {
applicationId "com.ttpj.testplu"
}
}
dependencies {
//根据是否独立运行,将模块作为apk还是module
if (!singleModule.toBoolean()) {
//其他模块作为app运行的话,就不能依赖库作为lib用
implementation project(path: ':home')
}
// 往往这个时候,需要在这个if之外放置一个service的module,而service依赖这common这个module
}
(如果gradle7.0以上的的plugins{}去掉,然后改成apply plugin: 'xxxx')的形式即可
home下的build.gradle
//根据是否独立运行,将模块作为apk还是module
if (singleModule.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
apply from: '../dependencies.gradle'
android {
//from dependencies.gradle
defaultConfig {
//只有独立运行时候才需要applicationId
if (singleModule.toBoolean()) {
applicationId "com.ttpj.home"
}
}
}
dependencies {
}
这样,最简单的模块区分就可以了。