国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

淺析Android中build.gradle的實用技巧

2019-10-21 21:31:06
字體:
來源:轉載
供稿:網友

1.替換符的使用

(1)在 app-android-defaultConfig (或者多渠道打包)下面可以這樣使用

android {  defaultConfig {  manifestPlaceholders = [        //高德地圖key        GDKEY: "123456789",    ]   }}

(2)在 AndroidManifest.xml 文件的 application 標簽下面這樣引用

<!-- 高德地圖 -->    <meta-data      android:name="com.amap.api.v2.apikey"      android:value="${GDKEY}" />

2.打包設置appname(啟動圖標類似,res下面的都可以這樣使用)

android {  defaultConfig {    //在string.xml中不能出現app_name這個字段,否則生成報錯    resValue "string", "app_name", "app名稱"      }}

3.生成BuildConfig.java字段

在build.gradle中

android {  defaultConfig {      //生成一個boolea類型的變量      buildConfigField "boolean", "IS_TEST_URL", "true"      //生成一個字符串變量      buildConfigField "String", "APP_UPDATE_TIME", "/"${System.currentTimeMillis().toString()}/""   }}

在java代碼

public final class BuildConfig { // Fields from product flavor: 渠道名 public static final String APP_UPDATE_TIME = "1551754973086"; public static final boolean IS_TEST_URL = false;}

4.多渠道打包(注意在defaultConfig下面添加flavorDimensions "versionCode")

android {  compileSdkVersion 28  defaultConfig {    minSdkVersion 19    targetSdkVersion 28    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    flavorDimensions "versionCode"   productFlavors {    name1 {      applicationId "com.test.name"      versionName "0.1.4"      versionCode 5      resValue "string", "app_name", "app名字"         buildConfigField "boolean", "IS_TEST_URL", "false"      buildConfigField "String", "APP_UPDATE_TIME", "/"${System.currentTimeMillis().toString()}/""    }    }

5.設置簽名

android{ signingConfigs {    release {      keyAlias ''      keyPassword ''      storeFile file('./sign.jks')      storePassword ''      v2SigningEnabled false    }  }  buildTypes {    release {      debuggable false      minifyEnabled true      shrinkResources true      useProguard true      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'      signingConfig signingConfigs.release    }    debug {      debuggable true      minifyEnabled false      shrinkResources false      useProguard false      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'      signingConfig signingConfigs.release    }  }}

6.更改打包的apk名

android{ android.applicationVariants.all { variant ->    variant.outputs.all {      Date nowTime = new Date()      SimpleDateFormat time = new SimpleDateFormat("MM月dd日HH時mm分")      outputFileName = "${variant.flavorName}(${variant.versionCode}_${variant.versionName})(${time.format(nowTime)}).apk"    }  }}

7.引入第三方庫的時候,剔除某些不需要的包或者重復的包

1.直接在configuration中排除 

configurations {  compile.exclude module: 'commons'  all*.exclude group: 'org.gradle.test.excludes', module: 'reports'}

2.在具體的某個dependency中排除

dependencies {  implementation("com.github.bumptech.glide:glide:4.8.0") {    exclude module: 'appcompat-v7'  }}

PS:Android Studio開發Build.gradle小技巧

引用版本統一規范

Android開發存在著眾多版本的不同,比如compileSdkVersion、minSdkVersion、targetSdkVersion以及項目中依賴第三方庫的版本,不同的module及不同的開發人員都有不同的版本,所以需要一個統一版本規范的文件,現在我就來介紹一種方式。

在項目根目錄,也就是跟app同一目錄下的build.gradle文件,如下圖所示

Android,build.gradle

在其最后添加如下groovy代碼。

ext {  compileSdkVersion = 25  buildToolsVersion = "25.0.0"  minSdkVersion = 19  targetSdkVersion = 19   supportVersion = '25.3.1'  rxjavaVersion = '1.1.8'  rxandroidVersion = '1.2.1'  glideVersion = '3.6.1'  junitVersion = '4.12'   deps = [      appcompatv7  : "com.android.support:appcompat-v7:$supportVersion",      supportv4   : "com.android.support:support-v4:$supportVersion",      recyclerviewv7: "com.android.support:recyclerview-v7:$supportVersion",      rxjava    : "io.reactivex:rxjava:$rxjavaVersion",      rxandroid   : "io.reactivex:rxandroid:$rxandroidVersion",      glide     : "com.github.bumptech.glide:glide:$glideVersion",      junit     : "junit:junit:$junitVersion"  ]}

有了這個規范,那么我們在app下的build.gradle文件就可以這樣來引用了

android {  compileSdkVersion rootProject.compileSdkVersion  buildToolsVersion rootProject.buildToolsVersion  defaultConfig {    applicationId "com.ecarx.thememanager"    minSdkVersion rootProject.minSdkVersion    targetSdkVersion rootProject.targetSdkVersion    versionCode 1    versionName "1.0"  }  ...} dependencies {  compile fileTree(include: ['*.jar'], dir: 'libs')  compile deps.supportv4  compile deps.appcompatv7  compile deps.recyclerviewv7  compile deps.rxjava  compile deps.rxandroid  compile deps.glide   testCompile deps.junit}

是不是一勞永逸了,今后修改版本只需要修根目錄下的build.gradle文件即可把所有依賴版本都修改

對資源進行分包

我們可以對每個頁面的資源都進行具體分類,不只是layout,還有drawable及value,是不是心動了,趕緊照著如下配置試一試吧,別再讓資源文件們“混為一潭”了。

方法很簡單,配置我們的app文件夾下的build.gradle文件,比如我的

android {  ...  sourceSets {    main {      res.srcDirs =          [              'src/main/res',              'src/main/res/layouts',              'src/main/res/layouts/home',              'src/main/res/layouts/hot_sale',              'src/main/res/layouts/amuse',              'src/main/res/layouts/delicacy',              'src/main/res/layouts/food_management',              'src/main/res/layouts/settings',          ]    }  }}

新建相關文件夾,配置完之后,sync project一下就成功了

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万宁市| 平果县| 光泽县| 新巴尔虎左旗| 报价| 汉中市| 瑞金市| 天水市| 修文县| 正镶白旗| 蓝田县| 灵武市| 罗城| 行唐县| 盘山县| 玉山县| 黄山市| 栾川县| 北京市| 南充市| 宝坻区| 崇义县| 汾阳市| 定安县| 蒙自县| 科尔| 乌兰浩特市| 同心县| 牙克石市| 武冈市| 丹江口市| 平山县| 盈江县| 太白县| 罗甸县| 湛江市| 鹤庆县| 高雄市| 莆田市| 云梦县| 新余市|