一、将项目(有多个module)中相同的配置信息抽取出单独用一个配置文件来设置
common.gradle
// Groovy语法
// 扩展块
ext {
// 建立Map存储,key和value都是自定义的
androidID = [
compileSdkVersion : 29,
applicationId : "com.example.moduleproject",
minSdkVersion : 26,
targetSdkVersion : 29,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner"
]
appID = [
app: "com.example.moduleproject"
]
dependenciesID = [
"appcompat" : "androidx.appcompat:appcompat:1.1.0",
"constraintlayout": "androidx.constraintlayout:constraintlayout:1.1.3"
]
}
在项目根目录下的build.gradle引入common.gradle
build.gradle(Project)
// 引入根目录下的common.gradle
apply from : 'common.gradle'
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
之后在各个module中更改相关的配置信息,以app目录下build.gradle为例
其中,定义def androidID = rootProject.ext.androidID 是出于性能考虑,相对于定义了一个缓存,可以从缓存中读取
build.gradle(app)
apply plugin: 'com.android.application'
println "app build gradle"
// 完整的方式 相当于定义了一个缓存, 出于性能考虑
def androidID = rootProject.ext.androidID
android {
compileSdkVersion androidID.compileSdkVersion
defaultConfig {
//applicationId "com.example.moduleproject"
//applicationId androidID.applicationId
applicationId appID.app
minSdkVersion androidID.minSdkVersion
targetSdkVersion androidID.targetSdkVersion
versionCode androidID.versionCode
versionName androidID.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 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'*/
// 循环读取
dependenciesID.each {k,v -> implementation v}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
二、区分正式环境和测试环境
在common.gradle公共配置文件中添加一个变量 isRelease 来区分正式环境(isRelease = true)和测试环境(isRelease = false),并在公共配置文件中配置正式环境和测试环境的url
如下:
common.gradle
// 扩展块
ext {
// 正式环境(true) 和 测试环境(false)
isRelease = false
// 正式环境 和 测试环境 服务器 URL配置
url = [
"debug" : "https://***.***.***.***/debug",
"release": "http://***.***.***.***/release"
]
}
如何让打包签名的时候动态部署执行正式或测试环境下的配置的服务器URL配置信息,需要在app壳中的build.gradle进行如下配置:
build.gradle(:app)
defaultConfig {
...
// Java代码也可以用
// 给Java代码暴露,标记,正式环境 和 测试环境 的标识
// 组件化 和 集成化 的时候需要
buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
}
buildTypes {
debug {
buildConfigField("String", "debug", "\"${url.debug}\"")
}
release {
buildConfigField("String", "release", "\"${url.release}\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
同步编译之后,就能够在代码中有BuildConfig类的相关信息,即在代码中可以使用Buildconfig.debug变量和BuildConfig.isRelease
BuildConfig.java
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.moduleproject";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug
public static final String debug = "https://192.188.22.99/debug";
// Fields from default config.
public static final boolean isRelease = false;
}
三、版本发布
将各个module模块的build.gradle进行配置,例如有login和register两个子模块,需要配置两个地方
build.gradle(:login)
//apply plugin: 'com.android.application'
if (isRelease) { //如果是发布版本时,各个模块都不能独立运行
apply plugin: 'com.android.library' // 正式环境 libray不能独立运行
} else {
apply plugin: 'com.android.application' // 测试环境 application独立运行
}
android {
compileSdkVersion androidID.compileSdkVersion
defaultConfig {
if (!isRelease) { // 能够独立运行, 必须要有appID
applicationId appID.login// 组件化模式能独立运行才能有applicationId
}
...
}
...
}
在app壳中添加相关的配置
build.gradle(:app)
dependencies {
... ...
if (isRelease) { //正式环境
// 依附app壳
implementation project(':login')
}
四、区分正式环境和测试环境运行不同的代码首页(只是一种思路)
在Login Module下的main目录下新建一个debug文件夹,该文件夹中新建一个AndroidManifest.xml清单文件
debug/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.login">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".debug.Login_DebugActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
该清单文件跟main目录下的AndroidManifest.xml清单文件类似,只是启动加载的activity不同。
在包目录下创建一个debug文件夹,该文件夹下存放一个debug环境下启动的页面Login_DebugActivity.java
Login_DebugActivity.java
package com.example.login.debug;
import android.os.Bundle;
import com.example.login.R;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class Login_DebugActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity_debug);
}
}
在login的build.gradle配置文件中进行如下环境设置:
build.gradle(:login)
//apply plugin: 'com.android.application'
if (isRelease) { //如果是发布版本时,各个模块不能独立运行
apply plugin : "com.android.library" //正式环境 library不能独立运行
} else {
apply plugin : "com.android.application" //测试环境 application独立运行
}
android {
compileSdkVersion 29
defaultConfig {
//applicationId "com.example.login"
if (!isRelease) {
applicationId appID.login
}
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
if (!isRelease) {
// 如果是组件化模式,需要单独运行时
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"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
五、备注
common.gradle
// Groovy语法
// 扩展块
ext {
// 正式环境(true) 和 测试环境(false)
isRelease = true
// 正式环境 和 测试环境 服务器 URL配置
url = [
"debug" : "https://***.***.***.***/debug",
"release": "https://***.***.***.***/release"
]
// 建立Map存储,key和value都是自定义的
androidID = [
compileSdkVersion : 29,
applicationId : "com.example.moduleproject",
minSdkVersion : 26,
targetSdkVersion : 29,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner"
]
appID = [
app : "com.example.moduleproject",
login : "com.example.login",
]
dependenciesID = [
"appcompat" : "androidx.appcompat:appcompat:1.1.0",
"constraintlayout": "androidx.constraintlayout:constraintlayout:1.1.3"
]
}
build.gradle(:app)
apply plugin: 'com.android.application'
println "app build gradle"
// 完整的方式 相当于定义了一个缓存, 出于性能考虑
def androidID = rootProject.ext.androidID
android {
compileSdkVersion androidID.compileSdkVersion
defaultConfig {
//applicationId "com.example.moduleproject"
//applicationId androidID.applicationId
applicationId appID.app
minSdkVersion androidID.minSdkVersion
targetSdkVersion androidID.targetSdkVersion
versionCode androidID.versionCode
versionName androidID.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// Java代码也可以用
// 给Java代码暴露,标记,正式环境 和 测试环境 的标识
// 组件化 和 集成化 的时候需要
buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
}
buildTypes {
debug {
buildConfigField("String", "debug", "\"${url.debug}\"")
}
release {
buildConfigField("String", "release", "\"${url.release}\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
/*implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'*/
dependenciesID.each {k,v -> implementation v}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
if (isRelease) { //正式环境
// 依附app壳
implementation project(':login')
}
}
build.gradle(:login)
//apply plugin: 'com.android.application'
if (isRelease) { //如果是发布版本时,各个模块不能独立运行
apply plugin : "com.android.library" //正式环境 library不能独立运行
} else {
apply plugin : "com.android.application" //测试环境 application独立运行
}
android {
compileSdkVersion 29
defaultConfig {
//applicationId "com.example.login"
if (!isRelease) {
applicationId appID.login
}
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
if (!isRelease) {
// 如果是组件化模式,需要单独运行时
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"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
build.gradle(Project)
// 引入根目录下的common.gradle
apply from : 'common.gradle'
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}