溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

超實(shí)用!打包、配置項(xiàng)目必備的Gradle實(shí)戰(zhàn)技巧!

發(fā)布時(shí)間:2020-07-02 14:57:19 來源:網(wǎng)絡(luò) 閱讀:1034 作者:星恒Android 欄目:移動(dòng)開發(fā)

官方參考

https://developer.android.com/studio/build/gradle-tips.html

以下都是經(jīng)過實(shí)戰(zhàn)總結(jié)、多次調(diào)整的gradle配置方案,歡迎采用,提意見

一 自動(dòng)命名打包的不同版本

 

applicationVariants.all { variant ->
variant.outputs.each { output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
if ("debug" == variant.buildType.name) {
def apkFile = new File(
output.outputFile.getParent(),
"XXX_App_${variant.versionName}_${buildTime()}_debug.apk")
output.outputFile = apkFile
} else if ("release" == variant.buildType.name) {
def apkFile = new File(
output.outputFile.getParent(),
"XXX_App_${variant.versionName}_release.apk")
output.outputFile = apkFile
}
}
}

if (variant.buildType.name == "debug") {
variant.mergedFlavor.versionName = variant.mergedFlavor.versionName + "." + buildTime();
}

}
def buildTime() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMdd')
    return formattedDate
}

二 統(tǒng)一全module的配置

在build.gradle中改寫引用方式   

 compileSdkVersion rootProject.ext.android["compileSdkVersion"]

在config.gradle中配置如下

 

ext {
android = [
compileSdkVersion: 23,
buildToolsVersion: "25.0.0",
minSdkVersion : 15,
targetSdkVersion : 22,
minifyEnabled : false,
minifyFile : rootProject.file('proguard-rules.pro'),
]

version = [
"supportVersion" : "25.0.0"
]

dependencies = [
"support_v4": "com.android.support:support-v4:${version["supportVersion"]}",
"appcompat-v7" : "com.android.support:appcompat-v7:${version["supportVersion"]}",
"recycleview" : "com.android.support:recyclerview-v7:${version["supportVersion"]}",
"multidex" : "com.android.support:multidex:1.0.1",
"junit" : "junit:junit:4.12",

//Baidu location
"baidu" : "libs/BaiduLBS_Android.jar"
] }

三 混合使用productFlavors 和 meta

manifestPlaceholders 可以在清單文件中配置好,格式如下

 

    <meta-data
            android:name="appid"
            android:value="12334"/>

同時(shí)在build.gradle中引用

 

productFlavors {

        normal {
            applicationId "com.xxx"
            manifestPlaceholders = [xxx: "0", appid: "xxx" , host:"xxx"];
        }

        productone {
 applicationId "com.xxx"
            manifestPlaceholders = [xxx: "0", appid: "xxx" , host:"xxx"];
        }

        producttwo {
            
 applicationId "com.xxx"
            manifestPlaceholders = [xxx: "0", appid: "xxx" , host:"xxx"];
        }

    }

配置不同product的資源和jni來源

 

sourceSets{
        normal {
            jniLibs.srcDirs = ['src/normalIn/jniLibsIn/']
            assets.srcDirs = ['src/normalIn/assetsIn']
        }

        productone {
            jniLibs.srcDirs = ['src/normalIn/jniLibsIn/']
            assets.srcDirs = ['src/normalIn/assetsIn']
        }
    }

代碼中調(diào)用方法:

 

  try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (info != null && info.metaData != null) {
                meta = info.metaData.getInt("appid");
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


四 flavor版本判斷

 

// build.gradle in application module

android {
    productFlavors {
        googlePlay {
        }
        wandoujia {
        }
    }
}
if (getGradle().getStartParameter().getTaskRequests().toString().contains("GooglePlay")) {
    // Google Play 版本才應(yīng)用該插件
    apply plugin: 'com.google.gms.google-services'
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI