Android sudio配置实现app多渠道打包,libary多渠道打包

为什么要有多渠道打包?

功能相似但是为了满足不同使用者我们不想重新写一个版本,我们就会利用多渠道来打成不同的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上
//自定义编译关键词
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')
}
注意:是和dependencies是同级的。而且自定义关键词是蓝色的,是不允许大写字母开头的,数字我就没试。

详细的代码地址:http://download.csdn.net/download/chen364567628/9996388






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值