1.替换符的使用
(1)在app-android-defaultConfig(或者多渠道打包)下面可以这样使用
android {
defaultConfig {
manifestPlaceholders = [
//高德地图key
GDKEY: "123456789",
]
}
}
(2)在AndroidManifest.xml文件的application标签下面这样引用
2.打包时设置appname(启动图标类似,res下面的都可以这样使用)
android {
defaultConfig {
//在string.xml中不能出现app_name这个字段,否则生成报错
resValue "string", "app_name", "app名称"
}
}
3.生成BuildConfig.java字段
多渠道打包可以根据某个变量很方便区分测试环境和正式环境
在build.gradle中
android {
defaultConfig {
//生成一个boolea类型的变量
buildConfigField "boolean", "IS_TEST_URL", "true"
//生成一个字符串变量
buildConfigField "String", "APP_UPDATE_TIME", "\"${System.currentTimeMillis().toString()}\""
}
}
在java代码
public final class BuildConfig {
// Fields from product flavor: 渠道名
public static final String APP_UPDATE_TIME = "1551754973086";
public static final boolean IS_TEST_URL = false;
}
4.多渠道打包(注意在defaultConfig下面添加flavorDimensions "versionCode")
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
flavorDimensions "versionCode"
productFlavors {
name1 {
applicationId "com.test.name"
versionName "0.1.4"
versionCode 5
resValue "string", "app_name", "app名字"
buildConfigField "boolean", "IS_TEST_URL", "false"
buildConfigField "String", "APP_UPDATE_TIME", "\"${System.currentTimeMillis().toString()}\""
}
}
5.设置签名
android{
signingConfigs {
release {
keyAlias ''
keyPassword ''
storeFile file('./sign.jks')
storePassword ''
v2SigningEnabled false
}
}
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
debuggable true
minifyEnabled false
shrinkResources false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}
6.更改打包的apk名
android{
android.applicationVariants.all { variant ->
variant.outputs.all {
Date nowTime = new Date()
SimpleDateFormat time = new SimpleDateFormat("MM月dd日HH时mm分")
outputFileName = "${variant.flavorName}(${variant.versionCode}_${variant.versionName})(${time.format(nowTime)}).apk"
}
}
}
7.引入第三方库的时候,剔除某些不需要的包或者重复的包
1.直接在configuration中排除
configurations {
compile.exclude module: 'commons'
all*.exclude group: 'org.gradle.test.excludes', module: 'reports'
}
2.在具体的某个dependency中排除
dependencies {
implementation("com.github.bumptech.glide:glide:4.8.0") {
exclude module: 'appcompat-v7'
}
}
8.引入本地的其他路径的module,在settins.gradle文件中代码如下
include ':demo1app', ':demo2app'
project(':demo1app').projectDir = new File("Demo1\\demo1app") //对应Module的路径
project(':demo2app').projectDir = new File("Demo2\\demo2app")