gradle中有一个功能叫做变体「productflavors」,这是来为APP设置不同的打包配置,以实现多渠道打包的一种方案。
android {
productFlavors {
baidu {
}
_360 {
}
yingyongbao {
}
}
gradle中打包编译类型「buildTypes」,主要区分:debug和release类型。
android {
buildTypes {
debug {
}
release {
}
}
(1)这样的话最后打包的时候就可以生成9种包:
· baiduDebug
· baiduRelease
· _360Debug
· _360Release
· yingyongbaoDebug
· yingyongbaoRelease
(2)在Android Studio左下角可以找到并在每次build的时候选择不同种类的包:
(1)在gradle中有一个功能叫「buildConfigField」,可以在系统的buildconfig中设置一个值。如下:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "org.guan.demo";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "baidu";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";
}
(3)使用
Logger.DEBUG = BuildConfig.DEBUG;
public static final String flavor = BuildConfig.FLAVOR;
(1)很多第三方sdk喜欢在manifest中配置appkey等,可以在gradle中使用:
manifestPlaceholders = [UMENG_CHANNEL: "0",UMENG_APPKEY : "123456789"]
(2)然后在manifest中配置:
(3)配置包名
在gradle中包名用applicationId代表。
defaultConfig {
applicationId "com.guan.demo"
}
yingyongbao {
applicationId "com.guan.demo.majia"
}
(4)配置版本号
yingyongbao {
versionCode 1
versionName "1.0.0"
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
dexOptions { javaMaxHeapSize "4g" }
sourceSets {
main {
jniLibs.srcDirs = ['libs'] //打包so库
}
}
defaultConfig {
minSdkVersion 15
applicationId "com.guan.demo"
multiDexEnabled true
manifestPlaceholders = [CHANNEL_NAME: "baidu", EXECUTE_FLAG: "true", EXECUTE_ADV_FLAG: "true"]
dependencies {
debugCompile files('libs/AppOffer_2.4.3_newsensenetworks.jar')
debugCompile files('libs/Baidu_MobAds_SDK.jar')
}
ndk {
// 设置支持的 SO 库构架
abiFilters 'armeabi', 'armeabi-v7a', 'x86'// , 'arm64-v8a', 'x86_64', 'mips', 'mips64'
}
}
buildTypes {
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"
}
release {
buildConfigField "boolean", "LOG_DEBUG", "false"
}
}
productFlavors {
baidu {
buildConfigField "boolean", "yingyongbao", "false"
manifestPlaceholders = [CHANNEL_NAME: "umeng", UMENG_KEY:"1",UMENG_SECRET:"123456", EXECUTE_FLAG: "true"]
dependencies {
baiduCompile files('libs/AppOffer_2.4.3_newsensenetworks.jar')
baiduCompile files('libs/Baidu_MobAds_SDK.jar')
}
}
_360 {
buildConfigField "boolean", "yingyongbao", "false"
manifestPlaceholders = [CHANNEL_NAME: "umeng", UMENG_KEY:"2",UMENG_SECRET:"123456", EXECUTE_FLAG: "false"]
dependencies {
provided files('libs/AppOffer_2.4.3_newsensenetworks.jar')
provided files('libs/Baidu_MobAds_SDK.jar')
}
}
yingyongbao {
buildConfigField "boolean", "yingyongbao", "true"
versionCode 100
versionName "1.0.0"
applicationId "com.guan.demo.majia"
manifestPlaceholders = [CHANNEL_NAME: "umeng", UMENG_KEY:"2",UMENG_SECRET:"123456", EXECUTE_FLAG: "false"]
dependencies {
provided files('libs/AppOffer_2.4.3_newsensenetworks.jar')
provided files('libs/Baidu_MobAds_SDK.jar')
}
}
}
}
dependencies {
compile project(':Downloader')
compile files('libs/umeng_social_sdk.jar')
}
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
}
(1)切换到Project模式的目录
(2)在src目录下新建vest包:
(3)再新建以下包和文件:
(4)调试时点击 Build Variant 选择自己需要的版本即可正常引用:
在马甲渠道包下,在application里的标签里加入tools:replace="android:name"替换了主包渠道的Application。
参考链接:将依赖库中清单文件,引用的依赖库的清单文件中的,冲突部分删除掉
Android 通过配置 productFlavors 实现多版本差异化打包
彻底弄明白Android开发Gradle相关配置