Android 组件化

1. 1. 在项目中配置config.gradle文件

ext {
    isModule = true
    isUserSingleModule = true
。。。
}

1.2. 某一个模块的build.gradle文件中

if (rootProject.ext.isUserModule) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
defaultConfig {
        if (rootProject.ext.isUserModule) {
            applicationId rootProject.ext.android.applicationId
        }
。。。}

sourceSets {
        main {
            if (rootProject.ext.isUserModule) {
                manifest.srcFile 'src/main/module/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {//排除java/debug文件夹下的所有文件
                    exclude '*module'
                }
            }
        }
    }

在主app的build.gradle中

if(!rootProject.ext.isModule){
        implementation project(':bundle_user')
    }

2.1. (在Android项目中的任何一个build.gradle文件中都可以把gradle.properties中的常量读取出来)在gradle.properties中

然后我们在业务组件的build.gradle中读取 isModule,但是 gradle.properties 还有一个重要属性: gradle.properties 中的数据类型都是String类型,使用其他数据类型需要自行转换;也就是说我们读到 isModule 是个String类型的值,而我们需要的是Boolean值,代码如下:

# 每次更改“isModule”的值后,需要点击 "Sync Project" 按钮
isModule=false
if (isModule.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
参考: https://blog.csdn.net/guiying712/article/details/55213884


猜你喜欢

转载自blog.csdn.net/t_yoo_csdn/article/details/81034381
今日推荐