Android studio模块化之模块划分简易版

news/2024/5/20 3:05:51 标签: android studio, android, gradle

模块也好,组件也罢,都是需要做好模块区分的。

一、新建module

修改你的模块名,比如你原来的包名是aaa.bbb.ccc,那么模块名就会是aaa.bbb.模块名

二、统一依赖

当我们建立好的module之后,发现主项目app和mudole各自的build.gradle文件都有一些相同的依赖,这个这个时候,需要进行管理,避免混乱。

一些相同的依赖

需要处理一下

gradle_25">处理之前的的两个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

gradle_137">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'
}

gradle_182">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’)的形式即可

gradle_206">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 {
}

这样,最简单的模块区分就可以了。

作者:J船长
链接:https://juejin.cn/post/7132783136827506724
更多Android studio学习笔记+视频资料可扫描下方二维码查看👇


http://www.niftyadmin.cn/n/1458566.html

相关文章

从零开始,全套Android架构师学习笔记,帮你打开Android世界的大门

众所周知,Android是谷歌开发的一款基于Linux的开源操作系统,从诞生至今已有10余年。一路走来,Android在用户体验、性能、功耗、安全、隐私等方面都取得了很大的进步。 Android系统庞大且复杂,需要具备全面的技术栈。首先&#xf…

Android Framework底层原理解密(AMS、PMS、Activity、Binder...)

我想点击此文的各位应该已对 Framework 系统架构有一定的了解,Android系统架构的分层,一共5层,从上到下依次是应用层、应用框架层、系统运行库层、硬件抽象层和Linux内核层。其中,应用框架层(Java API Framework&#…

Android零基础学习指南,从入门到精通一战通关

Android程序员想要入门进阶,升职拿高薪,一般需要做出以下努力: 明确的学习进阶规划,系统化掌握更深层次的技术弄清目标岗位的能力需求,针对性提升技术能力;增加实战经验,高效率提高解决实际问题的能力。 针…

全网最全Android compose开发应用指南

Jetpack Compose 是一款基于Kotlin API,重新定义Android布局的一套框架。它可简化并加快 Android 上的界面开发。使用更少的代码、强大的工具和直观的 Kotlin API,快速让应用生动而精彩。对于开发者而言最直观的就是节省开发时长,减少包体积&…

浅谈Android高级架构师进阶之路

无论是在Android开发还是其他领域,高级程序员一定是勤奋的,可以快速地掌握大量的新技术、新框架,不仅懂得原理,还能把新的技术落地到公司的产品中去。这部分高级程序员的进阶之路通常分为以下六个阶段: 第一阶段&…

Android面试174道题,助你高效学习、备战秋招

程序员求职/跳槽前,闭关复习必不可少,但毕竟Android涉及的知识面较广,知识点多且杂,要系统复习绝对是一个大工程,对于本就身心承受着很大压力的程序员而言更加困难。 许多粉丝在领取资料后给我反馈,之前分…

2022阿里巴巴Android开发岗面试题目,附大厂最新面试题合集+解析

转眼就要到国庆节了,离2022结束就剩下2个多月了,相信此时仍有很多人在跳槽的路上。这里分享一下今年阿里巴巴Android开发的面试题给大家。阿里作为国内首屈一指的互联网大厂之一,可以说是行业的风向标,其面试题目的倾向性与侧重点…

最强Android入门开发指南,帮你打通Android的任督二脉

Android 新手想要入门,很容易会遇到各类困难和学习瓶颈。没有一个好学的学习方向,学习规划,学习教程,这都是新手会面临的问题。 很多人会在百度上搜索,查阅相关资料。但是网上搜索的很多资料,都是断片式的学…