为什么要有多渠道打包?
功能相似但是为了满足不同使用者我们不想重新写一个版本,我们就会利用多渠道来打成不同的APP来满足市场需求。
什么是多渠道打包?
多渠道打包能够指定生成的app中是那种资源文件或者java字节码,这样免去了手工打包的繁琐。
android studio具体配置实现:
一、普通项目,即就是apply plugin: 'com.android.application'
这种渠道配置:资源文件和main文件夹同级位于src下面,可以是大写也可以是小写,然后在build.gradle中配置渠道信息即可
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "banwxf.com.test"
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//配置不同渠道的app名字
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "nma_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
debug{
}
}
//配置不同渠道的信息
productFlavors {
demo{
//当然这里面还可以配置其他信息,不做阐述
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(path: ':commenUI')
compile project(path: ':mail')
compile project(':contact')
}
注意:注意配置的位置,渠道文件夹可以是大写也可以是小写,但一定要一致。
二、android libary多渠道打包实现apply plugin:'com.android.library'
给库设置多渠道有点不一样:即就是需要多在productFlavors中多一个publishNonDefault true这样的配置,而且在libary中渠道文件夹只能是小写,大写将无法引用java资源,而且为了能够支持switch语法JDK最好保持1.7及以上。
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
}
}
//配置不同渠道的信息
productFlavors {
publishNonDefault true //唯一的不同
demo{
//当然这里面还可以配置其他信息,不做阐述
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(path: ':commenUI')
compile project(path: ':commenUI')
}
另外多渠道libary怎么配置依赖关系呢?
首先,application项目中药配置所有渠道,以及要自定义编译关键词,configurations,通过自定义的关键词指定不同渠道的libary编译。
配置多渠道libary依赖格式:path值得是:‘:libaryName’,这个当然是配置在调用的module上注意:是和dependencies是同级的。而且自定义关键词是蓝色的,是不允许大写字母开头的,数字我就没试。//自定义编译关键词 configurations { demoReleaseCompile demoDebugCompile } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile project(path: ':commenUI') // compile project(path: ':mail') compile project(':contact') //配置多渠道libary依赖格式:path值得是:‘:libaryName’ demoReleaseCompile project(path: ':mail', configuration: 'demoRelease') demoDebugCompile project(path: ':mail', configuration: 'demoDebug') }
详细的代码地址:http://download.csdn.net/download/chen364567628/9996388