解决:多模块化下配置信息混淆、冲突bug,使用config.gradle解决

一开始项目的全部代码都放在APP下面,随着业务的逐渐增加,共同开发的人员增加,为了更好的规范项目、提高开发效率等,需要把项目改成模块化开发,根据业务的不同进行分开在不同模块,不同开发人员负责不一样的模块,互不干扰。模块化带来那么多的优点,这时会隐藏着一个问题,没有规范好第三方及项目基本配置信息,会导致:配置信息混淆bug。同一个第三方SDK,好几个模块用到都要写一次,还可能用不同版本,这样子在运行时,gradle会合在一起,这样子就不知道你具体要哪个版本…………这些问题改起来特别麻烦,这时需要统一一个gradle,配置信息全部来自它,方便统一管理项目基本信息配置、第三方SDK.

 

前提:

建立好一个Android项目(我这边是AndroidX)和几个module(注:gradle下com.android.application改成com.android.library,删除applicationId)如下图所示:

需要讲解一下这些模块(是按照我的理解来的):

1.module_common:

  • 通过api引用公共的第三方sdk,设置颜色、大小等属性,就是多个业务模块都用到的。这个也叫公共模块,模块名随便取。
  • 业务模块implementation project(':module_common')即可,不需要再重复引用第三方SDK(若只有本模块使用,则不用加到module_common,直接加到自己模块即可)。
  • 通过api引入工具、数据库、网络等单独抽离出来的模块(api project(':module_utils'),那么引用common模块的可以使用utils模块。eg:module_login引用(implementation project(':module_common'))(api、implementation 的区别,我个人解读是:A模块中api MM,A模块和引用A模块的都可以使用MM;换成implementation MM就只有A模块可以使用MM了)

2.module_home、module_login:业务模块,根据业务分离出来implementation project(':module_common')

3.module_utils、module_database:公共用的,被common 给引入

 

步骤:

一.创建config.gradle(跟app同级,命名是config.gradle,记得有后缀)

def appcompatVersion = '1.0.2'
def constraintLayoutVersion = '1.1.3'

ext {
    //Module:app属性使用这个
    app = [
            applicationId    : 'com.example.mm',
            minSdkVersion    : 15,
            targetSdkVersion : 29,
            versionCode      : 1,
            versionName      : '1.0.0',
            compileSdkVersion: 29,
            buildToolsVersion: '29.0.2',
    ]

    //模块的属性使用这个
    module = [
            minSdkVersion    : 15,
            targetSdkVersion : 29,
            compileSdkVersion: 29,
            buildToolsVersion: '29.0.2',
            sourceCompatibility: JavaVersion.VERSION_1_8,
            targetCompatibility: JavaVersion.VERSION_1_8
    ]

    //第三方平台的id、key,该在app下配置的照常配,只是id、key从这里拿,方便统一管理,eg:极光推送(app下的gradle也有配置)
    jpush = [
            appKey: '*********'
    ]

    dependencies = [
            appcompat       : "androidx.appcompat:appcompat:${appcompatVersion}",
            constraintlayout: "androidx.constraintlayout:constraintlayout:${constraintLayoutVersion}"

    ]

    testImplementation = [
            junit: "junit:junit:4.12"
    ]

    androidTestImplementation = [
            junit        : 'androidx.test.ext:junit:1.1.0',
            espresso_core: 'androidx.test.espresso:espresso-core:3.1.1'
    ]
}

 

二.build.gradle(Project:MM):加上如下代码

apply from: "config.gradle"

 

三.app下的gradle属性(必须有2,不然使用不了config.gradle里面的内容)

提问:为什么只implementation业务模块、公共模块呢?

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.app.compileSdkVersion
    buildToolsVersion rootProject.ext.app.buildToolsVersion
    defaultConfig {
        applicationId rootProject.ext.app.applicationId
        minSdkVersion rootProject.ext.app.minSdkVersion
        targetSdkVersion rootProject.ext.app.targetSdkVersion
        versionCode rootProject.ext.app.versionCode
        versionName rootProject.ext.app.versionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        manifestPlaceholders = [
                JPUSH_PKGNAME: rootProject.ext.app.applicationId,
                JPUSH_APPKEY : rootProject.ext.jpush.appKey, //从config.gradle拿
                JPUSH_CHANNEL: "developer-default"
        ]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    testImplementation rootProject.ext.testImplementation.junit
    androidTestImplementation  rootProject.ext.androidTestImplementation.junit
    androidTestImplementation  rootProject.ext.androidTestImplementation.espresso_core

    implementation project(':module_common')
    implementation project(':module_home')
    implementation project(':module_login')
}

 

四.module_common下的gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.module.compileSdkVersion
    buildToolsVersion rootProject.ext.module.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.module.minSdkVersion
        targetSdkVersion rootProject.ext.module.targetSdkVersion
        versionCode rootProject.ext.module.versionCode
        versionName rootProject.ext.module.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility rootProject.ext.module.sourceCompatibility
        targetCompatibility rootProject.ext.module.targetCompatibility
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    api rootProject.ext.dependencies.appcompat
    api rootProject.ext.dependencies.constraintlayout

    testImplementation rootProject.ext.testImplementation.junit
    androidTestImplementation  rootProject.ext.androidTestImplementation.junit
    androidTestImplementation  rootProject.ext.androidTestImplementation.espresso_core

    api project(':module_database')
    api project(':module_utils')

}

 

五.业务模块以module_login下的gradle为例:

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.module.compileSdkVersion
    buildToolsVersion rootProject.ext.module.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.module.minSdkVersion
        targetSdkVersion rootProject.ext.module.targetSdkVersion
        versionCode rootProject.ext.module.versionCode
        versionName rootProject.ext.module.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility rootProject.ext.module.sourceCompatibility
        targetCompatibility rootProject.ext.module.targetCompatibility
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //若是某些第三方控件只有本模块使用,那么不用写在module_common里,直接写来这里,一些jar也是类似的
    implementation project(':module_common')
}

 

六:其它模块,以module_utils为例:

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.module.compileSdkVersion
    buildToolsVersion rootProject.ext.module.buildToolsVersion


    defaultConfig {
        minSdkVersion rootProject.ext.module.minSdkVersion
        targetSdkVersion rootProject.ext.module.targetSdkVersion
        versionCode rootProject.ext.module.versionCode
        versionName rootProject.ext.module.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation rootProject.ext.dependencies.appcompat
    implementation rootProject.ext.dependencies.constraintlayout

    testImplementation rootProject.ext.testImplementation.junit
    androidTestImplementation  rootProject.ext.androidTestImplementation.junit
    androidTestImplementation  rootProject.ext.androidTestImplementation.espresso_core
}

 

本篇涉及到的知识(暂时想到的):模块化,组件化,implementation和api的区别,config.gradle的创建使用。

 

这个知识点,网上已经有很多成熟的文章,自己还写,只是为了记录一下,以自己的理解、认识来写。最近换工作换住所电脑坏了,好久没有写文章了,也没有找到想写的题,想到之前用过这个知识,突然间想用又有点模糊,所以就写了。不足之处,敬请谅解!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值