android studio模块化之模块分别简易版

手机软件开发 2024-9-30 00:32:20 38 0 来自 中国
模块也好,组件也罢,都是须要做好模块区分的。
一、新建module

修改你的模块名,比如你原来的包名是aaa.bbb.ccc,那么模块名就会是aaa.bbb.模块名
二、同一依赖

当我们创建好的module之后,发现主项目app和mudole各自的build.gradle文件都有一些相同的依赖,这个这个时间,须要举行管理,制止杂乱。
一些相同的依赖

3.png 须要处置处罚一下
处置处罚之前的的两个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=falsedependencies.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照旧moduleif (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 {}如许,最简单的模块区分就可以了。
作者:J船长
链接:https://juejin.cn/post/7132783136827506724
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:53, Processed in 0.180798 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表