因为项目需要,需要做到有多个渠道的前提下,针对debug 版要求每个版本的id不同,以便于安装于一部手机进行测试,但是release版的id 需要相同(即为默认的id)。因此,对于能自动控制就坚决不手动处理的我来说,自然是想通过Gradle 这一工具去实现。
由于Gradle 比较深入的使用方法比较少,这中间走了不少坑,不过最后功夫不负有心人,还是顺利解决了,因此,记下来,以免忘记。
整个清单文件如下:
apply plugin: 'com.android.application'
//apply plugin: 'android-apt'
apply plugin: 'com.dodola.rocoofix'
apply plugin: 'com.antfortune.freeline'
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.google.test"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0.0." + getSvnRevisionNumber()
multiDexEnabled true
ndk {
abiFilters "armeabi", "armeabi-v7a"
}
}
flavorDimensions "versionCode"
productFlavors {
ChangSha {
versionCode android.defaultConfig.versionCode
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "ChangSha"]
resValue("string", "app_name", "长沙")
}
SanMenXia {
versionCode android.defaultConfig.versionCode
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "SanMenXia"]
resValue("string", "app_name", "三门峡")
}
XinYang {
versionCode android.defaultConfig.versionCode
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "XinYang"]
resValue("string", "app_name", "信阳")
}
LuAn {
versionCode android.defaultConfig.versionCode
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "LuAn"]
resValue("string", "app_name", "六安")
}
}
applicationVariants.all { variant ->
def mergedFlavor = variant.mergedFlavor
def buildType = variant.buildType.name
if (buildType == "debug") {
switch (variant.flavorName) {
case "ChangSha":
mergedFlavor.setApplicationId("com.google.changsha")
break
case "SanMenXia":
mergedFlavor.setApplicationId("com.google.sanmenxia")
break
case "XinYang":
mergedFlavor.setApplicationId("com.google.xinyang")
break
case "LuAn":
mergedFlavor.setApplicationId("com.google.luan")
break
}
}
}
buildTypes {
release {
//混淆开启
minifyEnabled false
//Zipalign优化
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources false
resValue("string", "app_name", "测试城市")
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField("String", "WEBSERVICE_URL", '"http://www.baidu.com/"')
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFileName
if (outputFile != null && outputFileName.endsWith('.apk')) {
def fileName = "TEST_v${defaultConfig.versionName}_" +
// ".${getSvnRevisionNumber()}_" +
"${defaultConfig.versionCode}_" +
// "${defaultConfig.manifestPlaceholders.UMENG_CHANNEL_VALUE}_" +
"${variant.productFlavors[0].name}_" +
"${releaseTime()}_${variant.buildType.name}.apk"
output.outputFileName = new File(fileName)
}
}
}
}
debug {
//混淆开启
minifyEnabled false
//Zipalign优化
zipAlignEnabled false
// 移除无用的resource文件
shrinkResources false
buildConfigField("String", "WEBSERVICE_URL", '"http://www.baidu.com/"')
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
// jniLibs.srcDirs = ['libs']
res.srcDirs =
[
'src/main/res/layouts/question',
'src/main/res/layouts/signup',
'src/main/res/layouts/business',
'src/main/res/layouts/mine',
'src/main/res/layouts',
'src/main/res'
]
}
}
dexOptions {
// incremental true
javaMaxHeapSize "4g"
preDexLibraries false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
productFlavors {
}
}
repositories {
flatDir {
dirs 'libs'
}
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:'
compile 'com.alibaba:fastjson:1.2.12'
compile 'com.android.support:multidex:1.0.1'
compile 'com.loopeer.android.thirdparty.pulltorefresh:Android-PullToRefresh:2.1.1'
compile 'com.android.support:recyclerview-v7:'
//https://github.com/JakeWharton/butterknife
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'org.greenrobot:eventbus:3.1.1'
compile 'com.dodola:rocoo:1.1'
compile files('libs/sun.misc.BASE64Decoder.jar')
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'fr.baloomba:viewpagerindicator:2.4.2'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
//添加友盟库
compile 'com.umeng.analytics:analytics:latest.integration'
compile 'com.squareup.wire:wire-runtime:2.1.2'
// 对话框https://github.com/afollestad/material-dialogs
compile 'com.afollestad.material-dialogs:core:0.9.4.5'
}
def getSvnRevisionNumber() {
return 'svn info --show-item revision'.execute().text.trim()
}
以上就是所有的实现,希望能帮助到有需要的人,如果看代码还有疑问的可以在评论区留言,看到了我会进行回复。
网友评论