Android grade語法,多渠道打包
文章目錄
- Android grade語法,多渠道打包
- 一、groovy語法
- 二、自動打包切換測驗正式環境
- 三、gradle多渠道打包
- 總結
一、groovy語法
以下代碼需要 在build.grade檔案中操作,具體列印日志在build中查看
//字串拼接
task StringText{
def str1 = "woshishuangyinhao"
def str2 = "woshidanyinhao"
println str1+str2
}
//集合
task list{
def list = [1,2,3,4,5]
println list[0]
list.each {
println it
}
for (int i in 1..5){
println list[i]
}
}
task map{
def map = ["name":"jeck","age":19]
println(map["name"])
map.each {
println("key:${it.key},value:${it.value}")
}
}
//定義一個方法 計算兩數之和
task method{
println(methodA(2,3))
println(methodBean())
}
def methodA(int a,int b){
a+b
}
def methodBean(){
//groovy默認只回傳最后一行的結果
Student student = new Student()
student.name = "hahahh"
student.age = 1215415
}
//定義一個物件 ,在groovy沒有私有一說,都能被呼叫
class Student{
String name
int age
String getName() {
return name
}
void setName(String name) {
this.name = name
}
int getAge() {
return age
}
void setAge(int age) {
this.age = age
}
}
//測驗閉包
task closure{
mEach{
println(it)
}
}
//閉包--》回呼或者 鉤子 在groovy只對{}里面的有效,Closure
def mEach(closure){
for (int i in 1..5){
closure(i)
}
}
列印如下

二、自動打包切換測驗正式環境
1.配置baseurl
在對應檔案中分別配置url:
baseUrl = “https://www.debug.com”
baseUrl = “https://www.release.com”

2.在gradle中撰寫讀取檔案方法:
//用groovy讀取組態檔
def getServerUrl(String str){
def url;
Properties properties = new Properties();
def proFile = file("src/main/filters/"+str+"/config.properties")
if (proFile!= null && proFile.canRead()){
properties.load(new FileInputStream(proFile))
if (properties!= null){
url = properties['baseUrl'];
}
}
url
}
列印測驗:
buildTypes {
debug{
println getServerUrl("debug")
}
release {
println getServerUrl("release")
}
}

3.配置讀取配置方法
buildTypes {
debug {
//三個引數 1 資料型別 2 變數名 3 變數值
buildConfigField 'String','url',getServerUrl('debug')
}
release {
buildConfigField 'String','url',getServerUrl('release')
}
}
然后clean +rebuild project專案,然后會自動生成一個如下的類:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.rb.renbin.agradledemo";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "huawei";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Field from product flavor: huawei
public static final String PLATE_FROM = "huawei";
// Field from build type: debug
public static final String url = "https://www.debug.com";
}
4.然后獲取里面的url配置到請求base就行了,這樣打包和編譯運行就會自動切換正式環境和測驗環境了
String baseurl = BuildConfig.url;
三、gradle多渠道打包
1.先創建自己的簽名檔案
2.配置簽名
//簽名配置
signingConfigs{
release{
keyAlias 'arouter'
keyPassword '123456'
storeFile file('app/arouter.jks')
storePassword '123456'
}
}
//自動打包切換域名 用groovy讀取組態檔
buildTypes {
debug {
//三個引數 1 資料型別 2 變數名 3 變數值
buildConfigField 'String','url',getServerUrl('debug')
}
release {
buildConfigField 'String','url',getServerUrl('release')
//混淆
minifyEnabled true
// 配置簽名
signingConfig signingConfigs.release
//包的輸出
}
}
3.在defaultConfig配置渠道code
defaultConfig {
applicationId "com.rb.renbin.agradledemo"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//多渠道包code
flavorDimensions "versionCode"
}
4.配置渠道號
productFlavors{
xiaomi{
buildConfigField 'String','PLATE_FROM','xiaomi'
}
yingyongbao{
buildConfigField 'String','PLATE_FROM','yingyongbao'
}
huawei{
buildConfigField 'String','PLATE_FROM','huawei'
}
}
或者
productFlavors{
xiaomi{}
yingyongbao{}
huawei{}
}
productFlavors.all{
flavor ->
buildConfigField 'String','PLATE_FROM',"\"${name}\""
}
5.輸出包,配置打包輸出方法
release {
buildConfigField 'String','url',getServerUrl('release')
//混淆
minifyEnabled true
// 配置簽名
signingConfig signingConfigs.release
//包的輸出 渠道+時間+版本號
android.applicationVariants.all { variant ->
variant.outputs.all { output -> // each 改為 all
def fileName = "${getCurrentTime()}_V${defaultConfig.versionName}_release.apk"
def outFile = output.outputFile
if (outFile != null && outFile.name.endsWith('.apk')) {
outputFileName = fileName // output.outputFile 改為 outputFileName
}
}
}
}
def getCurrentTime(){
return new Date().format("yyyy-MM-dd",TimeZone.getTimeZone("UTC"))
}
6.gradle多渠道打包
在terminal下執行 gradlew assemble 打包指令

或者在這里選擇自己需要的渠道打包也可以

打包如下:

總結
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/294973.html
標籤:其他
