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了,如果對你有幫助的話,給我點個👍吧!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/275823.html
標籤:其他
上一篇:Android Parcelable序列化原始碼決議
下一篇:屬性影片
