盒子
盒子
Posts List
  1. 1.UMENG_CHANNEL_VALUE
  2. 2.buildConfigField

gradle实践系列:一个常见gradle脚本例子

apply plugin: 'com.android.application'
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
signingConfigs {
debug{
}
release {
keyAlias 'cmssp'
keyPassword 'xxxxxx'
storeFile file('C:/d/github/cmssp/doc/project/cmssp.keystore.jks')
storePassword 'xxxxxx'
}
}
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.hch.beautyenjoy"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "tencent"]
}
lintOptions {
abortOnError false
}
buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
buildConfigField "boolean", "LOG_DEBUG", "false"
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "beauty_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
productFlavors {
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
anzhi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "anzhi"]
}
ali {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "ali"]
}
qihu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qihu"]
}
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
tencent {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "tencent"]
}
}
}
dependencies {
compile project(':waterFallListView')
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-v4:23.1.0'
}

1.UMENG_CHANNEL_VALUE

UMENG_CHANNEL_VALUE是一个定义在androidmanifest.xml中的渠道变量,用于给不同渠道打包,也可以用于定义一些自定义参数,这些参数可以在打不同包的时候,被替换成想要的值,然后在app中通过获取meta值来使用

<meta-data android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}">
</meta-data>

2.buildConfigField

buildConfigField可以用于生成特定的java代码变量

debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}

上面语句的作用就是在生成debug版本的apk时,在BuildConfig.java中定义常量,名字为LOG_DEBUG, 类型为boolean,值为true的常量

编译生成的BuildConfig.java内容如下

package com.hch.beautyenjoy;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.hch.beautyenjoy";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "baidu";
public static final int VERSION_CODE = 4;
public static final String VERSION_NAME = "2.2-debug";
// Fields from build type: debug
public static final boolean LOG_DEBUG = true;
}

支持一下
扫一扫,支持牛头码农