android多渠道打包定义名字,AndroidStudio多渠道打包之路一-----aar包改名

前言

很早之前做了个aar的依赖库,但是给到对方开发人员都是同一个名字,导致对方觉得管理不方便,请求做个类似多渠道打包的控制,于是开始了自己的aar包修改命名之路。

如何修改打包后的aar包名字

修改apk包的名字相信大家都不陌生,因为这个需求对于Android开发来说再平常不过了,需要多渠道打包,重命名apk名字等

这里就先用给apk包修改名字为引,因为aar包改名基本一致。

gradle配置多渠道打包apk相应配置

在项目文件app下的build.gradle中的android{}模块下配置混淆压缩:

buildTypes {

debug {

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

signingConfig signingConfigs.release

}

release {

minifyEnabled false

zipAlignEnabled true

buildConfigField "boolean", "LOG_DEBUG", "false"

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

signingConfig signingConfigs.release

}

}

这里发布debug或release的相应配置。

多渠道打包需要创建和buildTypes的同级别的productFlavors来实现,如:

//配置多版本的apk

productFlavors {

Offline {

applicationId "com.bm.*****.beta"

manifestPlaceholders = [app_name: "测试粑粑", icon: "@drawable/common_app_icon"]

resValue("string", "baseUrl", "http://192.168.1.35/")

}

Online {

applicationId "com.bm.*****"

manifestPlaceholders = [app_name: "正式粑粑", icon: "@drawable/common_app_icon"]

resValue("string", "baseUrl", "http://haha.cheche.com/")

}

}

这样就配置了两个版本渠道Offline和Online,并对不同的baseUrl参数进行定义,在响应的java代码中获取该参数的方法是:

Application.getContext().getResources().getString(R.string.baseUrl);

自定义发布包的名字:

applicationVariants.all { variant ->

variant.outputs.all { output -> // each 改为 all

def fileName = "CheMiClientApp_" + buildType.name + "_V" + defaultConfig.versionName + ".apk"

def outFile = output.outputFile

if (outFile != null && outFile.name.endsWith('.apk')) {

outputFileName = fileName // output.outputFile 改为 outputFileName

}

}

}

这个是AS升级到3.0以后的配置方式,AS3.0之前的配置方式会报错。

AS3.0之前的配置:

想要生成的apk中加上版本号,或者当前的时间等信息,该如何生成呢?

以上边为例,我想要offline的版本加上版本号和当前日期,格式为app_offline_debug_v1.0-20170419.apk这样的格式!

首先我们需要在build.gradle根目录,定义得到当前日期的函数:

def releaseTime() {

return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))

}

然后在build.gradle中android{}标签下,添加:

//在apk文件后边生成版本号信息

android.applicationVariants.all {

variant ->

variant.outputs.each {

output -> output.outputFile = new File(output.outputFile.parent, "app_" + productFlavors[0].name + "_" + buildType.name + "_v" + defaultConfig.versionName + "-${releaseTime()}.apk");

}

}

①output.outputFile.parent表示生成apk的路径,这个是默认路径在项目/app/build/outpus/apk,当然我们可以自己写路径。

②productFlavors[0].name表示productFlavors标签下的名称,这里就是offline或者online。

③buildType.name指编译类型,就是debug或者release

④defaultConfig.versionName表示在defaultConfig标签下写的版本号

⑤releaseTime()表示我们定义的得到日期的函数

但是AS3.0之后这样配置会报错:

Error

Error:(56, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

Solution

outputFile变为只读,不能修改输出的名称所以报错。修改为:

applicationVariants.all { variant ->

variant.outputs.all { output -> // each 改为 all

def fileName = "${variant.versionName}_release.apk"

def outFile = output.outputFile

if (outFile != null && outFile.name.endsWith('.apk')) {

outputFileName = fileName // output.outputFile 改为 outputFileName

}

}

把each修改为all,然后通过outputFileName修改生成apk的名称。此外,AS 3.0后打包完,除了apk包文件,还会多一个 output.json 参数文件。

aar自定义名字

//自动追加版本号和版本名称

android.libraryVariants.all {

variant->variant.outputs.each {

output-> output.outputFile = new File(output.outputFile.parent,"wimiftSDKLibrary_"+defaultConfig.versionName+"_"+new Date().format("yyyy-MM-dd")+"_"+buildType.name+".aar")

}

}

亲测有效。

另外aar打包混淆发包的代码模板:

buildTypes {

release {

//签名

signingConfig signingConfigs.release

//混淆

minifyEnabled true

//对齐

zipAlignEnabled true

//移除无用的resource 文件

shrinkResources true

//加载默认混淆配置文件

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

打完收工!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值