官方參考
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比武寧網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式武寧網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋武寧地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
https://developer.android.com/studio/build/gradle-tips.html
以下都是經(jīng)過實戰(zhàn)總結(jié)、多次調(diào)整的gradle配置方案,歡迎采用,提意見
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
}
在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"
] }
manifestPlaceholders 可以在清單文件中配置好,格式如下
android:name="appid"android:value="12334"/>同時在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 moduleandroid {productFlavors {googlePlay {}wandoujia {}}}if (getGradle().getStartParameter().getTaskRequests().toString().contains("GooglePlay")) {// Google Play 版本才應(yīng)用該插件apply plugin: 'com.google.gms.google-services'}
網(wǎng)頁名稱:超實用!打包、配置項目必備的Gradle實戰(zhàn)技巧!
網(wǎng)站路徑:http://weahome.cn/article/peshci.html