Android- 多渠道打包配置;你了解吗?
如果您希望添加或更改特定设置,您可以将调试构建类型添加到您的配置中。以下示例为调试构建类型指定了 applicationIdSuffix,并配置了一个使用调试构建类型中的设置进行初始化的 jnidebug 构建类型。
applicationIdSuffix: 字段表示,在不改变你默认的程序 ID(包名)的情况下,为其添加后缀。比如你的包名是 com.rae.app,但你想区分测试包和正式包的情况,这个时候将 applicationIdSuffix 设置为.debug,那么你的应用程序对应的包名就变成了 com.rae.app.debug
android {...defaultConfig {...}buildTypes {release {minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}
debug {applicationIdSuffix ".debug"}
/**
- The 'initWith' property allows you to copy configurations from other build types, 
- so you don't have to configure one from the beginning. You can then configure 
- just the settings you want to change. The following line initializes 
- 'jnidebug' using the debug build type, and changes only the 
- applicationIdSuffix and versionNameSuffix settings.*/ 
jnidebug {
// This copies the debuggable attribute and debug signing configurations.initWith debug
applicationIdSuffix ".jnidebug"jniDebuggable true}}}
构建源集
名词解析: 通常源代码是放在 src/main 文件夹下的,但你想可以根据不同的构建类型(比如 debug、release)区分不同的源文件,这样对应建立的文件夹就是一个不同的构建源。打个比方,debug 的构建源为 src/debug,release 的构建源为 src/release,而在 src/main 定义的为公共资源,最后在构建的时候会进行合并操作。
Android Studio 按逻辑关系将每个模块的源代码和资源分组为源集。模块的 main/ 源集包括其所有构建变体共用的代码和资源。其他源集目录为可选项,在您配置新的构建变体时,Android Studio 不会自动为您创建这些目录。不过,创建类似于 main/ 的源集有助于让 Gradle 只应在构建特定应用版本时使用的文件和资源井然有序:
构建源的命名规则如下:
productFlavor 表示渠道包,可以看下面的多渠道打包
- src/main/ 此源集包括所有构建变体共用的代码和资源。 
- src/<buildType>/ 创建此源即可加入特定构建类型专用的代码和资源。示例:src/jnidebug 
- src/<productFlavor>/ 创建此源即可加入特定产品风味专用的代码和资源。比如百度渠道包:src/baidu 
- src/<productFlavorBuildType>/ 创建此源集可加入特定构建变体专用的代码和资源。 
例如,要生成应用的“完全调试”版本,构建系统需要合并来自以下源集的代码、设置和资源。比如:百度的开发环境包:src/baiduDebug
构建类型的依赖配置
很多时候我们会把 sdk 或者 api 接口单独做成一个库,一般会有生产环境和测试环境之分,但在依赖的时候往往我们会像这样去引用:compile project(':sdk'),这样依赖的环境就是 release,在开发调试的时候测试环境的时候就不行了。我们得换另外一种方式:
<buildType>Compile project()
这样会根据不同的构建类型去依赖不同的包,比如我们测试环境的依赖包:debugCompile project(':sdk'),再比如上面的 jnidebug:jnidebugCompile project(':sdk')
那么问题来了,我当前的构建类型怎么对应到其他的 module 去呢?比如你的 app 要依赖 sdk module 的 debug 环境,那么你可以这么做:
configuration:目标 module 的<buildType>,比如你 sdk 中<buildType>的 debug 构建类型
debugCompile project(path: ':sdk', configuration: 'debug')
 
 综合示例
1、先看 app 这边的 build.gradle 配置:
apply plugin: 'com.android.application'
android {buildTypes {release {minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}debug {applicationIdSuffix '.debug'minifyEnabled false}
// 自定义的构建类型,名字随便取,一定要有意义 raedebug {initWith debugapplicationIdSuffix '.raedebug'}}}
dependencies {// 生成环境依赖 releaseCompile project(path: ':sdk', configuration: 'release')// 测试环境依赖 debugCompile project(path: ':sdk', configuration: 'debug')// 自定义构建类型依赖 raedebugCompile project(path: ':sdk', configuration: 'uutest')}
2、sdk module 的 build.gradle 配置:
apply plugin: 'com.android.library'
android {buildTypes {debug {debuggable trueminifyEnabled false}release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}
// 注意这里,跟第一点的 raedebugCompile project 的 configuration 要匹配。uutest {initWith debug}}}
多渠道打包 productFlavors
先看看 build.gradle 配置你就懂了
android{
// 渠道包定义,默认定义的名称就是渠道名称 productFlavors {
dev {} // 测试 baidu {} // 百度手机助手 yinyongbao {} // 应用宝 m360 {} // 360 手机助手 pp {} // PP 助手 anzhi{} // 安智市场 xiaomi {} // 小米商店 letv {} // 乐视商店 huawei {} // 华为商店 lenovomm {} // 联想乐商店 other {} // 其他市场 official{} // 官方版本
}
// 批量渠道包值替换 productFlavors.all { flavor ->// 友盟、极光推送渠道包, UMENG_CHANNEL 是根据你 AndroidManifest.xml 来配置的,请看下面。flavor.manifestPlaceholders = [UMENG_CHANNEL: name, JPUSH_CHANNEL: name]}}
AndroidManifest.xml 配置:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.rae.demo">
<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme">











 
    
评论