今天说一说android studio中gradle多渠道打包。其实也不光是多渠道,就是一次打多种包。首先这篇文章是给对gradle有一定了解的读者写的,要不然你读着可能一头雾水。大家可以想一想,在公司中我们一般有多个服务器地址,有平时我们测试用的,有发布到市场上用的,首先这个包的keystore是不同的吧,一个是debug类型的,一个是签名的,再者Url应该不同吧,大家再想在我们使用推送test版本和release版本的app key应该不同吧,还有如果你打多渠道包也是一个道理,那我们是不是打不同的包时还要每次都修改java文件或者XML文件呢,no,进入正题,
说之前大家看看这张图:
这是在打签名包的时候的一个视图选择,大家可以看到供我们选择的也就两个地方,那也就是说要想一次打多个包就从这两个地方入手就得了,第一个叫做Build Type,第二个叫做Flavors,那这两个东西是干什么用的,又有什么用呢。
先说第一个Build Type:
大家可以看到我们在Build Type里可以设置这么多选择,当然有很多我也不用过,常用的比如,minify enabled:是否混淆;proguard file混淆文件;Application ID suffix:app id后缀(设置后可以在同一台手机上运行同一个app的多个版本)version name suffix:版本名后缀等等。
然后再看Flavors,这才是我们的重点,首先我们在使用百度地图时一般有两个app key(如果使用到了地图界面),怎么在打包的时候不用手动修改呢:首先在manifest文件中:
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="${BAIDU_API_KEY_VALUE}" />
其中BAIDU_API_KEY_VALUE是我随便写的,然后在gradle文件中:
productFlavors {
//debug
myDebug {
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your key1"]
}
//线上正打包式发布
online{
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your key2"]
}
}
说明一下:productFlavors中我随便写的两个类型,一个是myDebug,一个是online,这样就解决了,那如果manifest文件又有一个类似需要修改的地方呢怎么办:
productFlavors {
//debug
myDebug {
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your key1",YOUR_CUSTOM_KRY:"your value1"]
}
//线上正打包式发布
online{
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your key2",,YOUR_CUSTOM_KRY:"your value2"]
}
}
明白了吧,知道了这些多渠道打包一个道理。
然后说一下怎么不用在打不同的包时手动修改java文件,首先先在gradle文件中:
defaultConfig {
buildConfigField "boolean", "IS_SIGN", "false"
}
productFlavors {
myDebug {
}
//线上正打包式发布
online{
buildConfigField "boolean", "IS_SIGN", "true"
}
//线下测打包试
offline{
}
}
关键代码:
buildConfigField "boolean", "IS_SIGN", "false"
我在默认配置中设置IS_SIGN(我随便写的一个变量名)为false,然后在正式发包中又设置了它为ture,然后在java文件中:
public static String BASE_URL;
static {
if (BuildConfig.IS_SIGN){
BASE_URL = "your base url1";
}else{
BASE_URL = "your base url2";
}
}
上面是我在项目中的一个用的地方,是不是就解决了手动修改java文件的麻烦。
下面再送给大家一个绝对的干货,我的项目中的gradle文件源码:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':calendlar')
compile project(':library')
}
android {
signingConfigs {
debug {
storeFile file('keystore/debug.keystore')
}
release {
storeFile file('keystore/keystore')
storePassword "your storePassword"
keyAlias "your keyAlias"
keyPassword "your keyPassword"
}
}
compileSdkVersion 19
buildToolsVersion "23.0.2"
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
defaultConfig {
versionCode 33
minSdkVersion 15
targetSdkVersion 17
applicationId 'your applicationId'
buildConfigField "boolean", "IS_SIGN", "false"
}
productFlavors {
/**
* 说明:
*1.自己测试的时候用debug(默认设置在 Build Variants,需手动)
*2.让测试人员测试时使用offline
*3.发布到市场时使用online
*/
//debug
edebug {
versionName '2.0.2(d)'
signingConfig signingConfigs.debug
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your velue",HUANXIN_APP_KEY_VALUE: "your value"]
}
//线上正打包式发布
online{
signingConfig signingConfigs.release
versionName '2.0.2(p)'
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your velue", HUANXIN_APP_KEY_VALUE: "your velue"]
buildConfigField "boolean", "IS_SIGN", "true"
}
//线下测打包试
offline{
signingConfig signingConfigs.release
versionName '2.0.2(t)'
manifestPlaceholders = [BAIDU_API_KEY_VALUE: "your velue", HUANXIN_APP_KEY_VALUE: "your velue"]
}
}
}
欢迎补充and纠正