App多渠道签名打包
背景
需要获取设备的系统权限,由于厂商较多,不同的app需要的系统签名文件不一致,每次打包都得对应不同的文件,为了方便管理维护和app能够获取到自身的打包渠道以实现相应的功能,笔者基于Gradle实现分渠道打包,并记录整理如下。文中如有不到之处,欢迎指导交流。
QQ 2903368986
配置 productFlavors
productFlavors
中文翻译产品风味
,也就是用于定义产品的特性或者说不同的地方,通过配置里面的参数我们就可以用一套代码编译出多个渠道的产品。
flavorDimensions "channel"
productFlavors {
/***
* 渠道1
*/
flavor1 {
//自定义属性
buildConfigField "String", "key", '"value1"'
//重定义appid
applicationId "xxx.xxx.xxx"
//重定义versionName
versionName "1.0.0"
//重定义版本
versionCode 1
//重定义value资源
resValue "string", "app_name", "***"
...
}
/**
* 渠道2
*/
flavor2 {
buildConfigField "String", "key", '"value2"'
...
}
}
定义签名文件路径
ExtraPropertiesExtension
是Gradle领域对象的一个属性,我们可以将自定义的属性添加到它的ext
扩展名上
关于ExtraPropertiesExtension的详细文档
ext {
path1 = '你的文件路径/***1.jks'
path2 = '你的文件路径/***2.jks'
}
配置 signingConfigs
从源码中可以看到sinningConfigs
的配置被保存在用于配置签名配置的DSL对象sinningConfigs中。
/**
* Encapsulates signing configurations that you can apply to {@link
* com.android.build.gradle.internal.dsl.BuildType} and {@link ProductFlavor} configurations.
*
* <p>For more information about the properties you can configure in this block, see {@link
* SigningConfig}
*/
public void signingConfigs(Action<? super NamedDomainObjectContainer<SigningConfig>> action) {
checkWritability();
action.execute(signingConfigs);
}
继续往下看我们可以配置哪些参数
/**
* Creates a SigningConfig with a given name.
*
* @param name the name of the signingConfig.
*/
@Inject
public SigningConfig(@NonNull String name) {
super(name);
}
public SigningConfig initWith(com.android.builder.model.SigningConfig that) {
//签名文件
setStoreFile(that.getStoreFile());
//密码
setStorePassword(that.getStorePassword());
//用户名
setKeyAlias(that.getKeyAlias());
//密码
setKeyPassword(that.getKeyPassword());
//是否打包v1
setV1SigningEnabled(that.isV1SigningEnabled());
//是否打包v2
setV2SigningEnabled(that.isV2SigningEnabled());
//打包类型
setStoreType(that.getStoreType());
return this;
}
很明显storefile就是我们想要的,具体配置如下
signingConfigs {
flavor1 {
storeFile file(path1)
storePassword 'xxx'
keyAlias 'xxx'
keyPassword 'xxx'
storeType 'release'
...
}
flavor2 {
storeFile file(path2)
...
}
}
结尾
到此整个配置就结束了,可以愉快的code了,如果对你有帮助的话,给我点个👍吧!