组件化(一)——Gradle编译

三步走起组件化

组件化开发的设计图
优点:主要是为了把公共类进行封装下沉,把业务解耦,进行组件化开发。

1.创建模块必须是同一个包内,并且类规范必须携带module_xxx

2.建立项目根目录下创建公共gradle文件: config.gradle

(用于统一属性和配置组件化和集成化模式)

ext{
    //1.realse 动态切换 false:组件化 ture:集成化
    isRelease = true

    //2.使用map 存储公共属性
    androidId = [
            compileSdkVersion: 28,
            buildToolsVersion: "29.0.0",
            minSdkVersion    : 19,
            targetSdkVersion : 28,
            versionCode      : 1,
            versionName      : "1.0"
    ]
    appId = [
            "app" : "com.lg.modularization",
            "member" : "com.lg.modularization.member",
            "personal":"com.lg.modularization.personal"
    ]
    url = [
           "debug" : "https://www.baidu.com/",
            "realse" : "https://developers.google.cn/china/"
    ]
    supportLibrary = "28.0.0"
    dependencies = [
            "appcompat"      : "com.android.support:appcompat-v7:${supportLibrary}",
            "recyclerview": "com.android.support:recyclerview-v7:${supportLibrary}",
            "constraint"     : "com.android.support.constraint:constraint-layout:1.1.3",
            "okhttp3"        : "com.squareup.okhttp3:okhttp:3.10.0",
            "retrofit"       : "com.squareup.retrofit2:retrofit:2.5.0",
            "fastjson"       : "com.alibaba:fastjson:1.2.58",
    ]
}

3.配置个模块的build.gradle,依赖与config.build例如:app模块

if (isRelease) { // 如果是发布版本时,各个模块都不能独立运行
    apply plugin: 'com.android.library'
} else {
    apply plugin: 'com.android.application'
}

def rootAndroidId = rootProject.ext.androidId
def rootAppId = rootProject.ext.appId
def rootSupport = rootProject.ext.dependencies
def baseUrl = rootProject.ext.url

android {
    compileSdkVersion rootAndroidId.compileSdkVersion
    buildToolsVersion rootAndroidId.buildToolsVersion
    defaultConfig {
        // 发布版本是集成化模式,不能有applicationId
        if (!isRelease) {
            applicationId rootAppId.order // 组件化模式需要applicationId
        }
        minSdkVersion rootAndroidId.minSdkVersion
        targetSdkVersion rootAndroidId.targetSdkVersion
        versionCode rootAndroidId.versionCode
        versionName rootAndroidId.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            buildConfigField("String", "baseUrl", "\""+baseUrl.realse+"\"")

        }
        debug{
            minifyEnabled false
            buildConfigField("String", "baseUrl", "\""+baseUrl.debug+"\"")

        }
    }
    sourceSets{
        main{
            if(!release){
                // 如果是组件化模式,需要单独运行时
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            }else{
                // 集成化模式,整个项目打包apk
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    // release 时 debug 目录下文件不需要合并到主工程
                    exclude '**/debug/**'
                }
            }
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // 循环引入第三方库
    rootSupport.each { k, v -> implementation v }
    implementation project(':common') // 公共基础库
}

你可能感兴趣的:(组件化(一)——Gradle编译)