溫馨提示×

溫馨提示×

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

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

淺析Android中build.gradle的實(shí)用技巧

發(fā)布時(shí)間:2020-10-08 02:30:57 來源:腳本之家 閱讀:175 作者:謝長意 欄目:移動開發(fā)

1.替換符的使用

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

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

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

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

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

android {
  defaultConfig {
    //在string.xml中不能出現(xiàn)app_name這個(gè)字段,否則生成報(bào)錯
    resValue "string", "app_name", "app名稱"   
   }
}

3.生成BuildConfig.java字段

在build.gradle中

android {
  defaultConfig {
      //生成一個(gè)boolea類型的變量
      buildConfigField "boolean", "IS_TEST_URL", "true"
      //生成一個(gè)字符串變量
      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.設(shè)置簽名

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時(shí)mm分")
      outputFileName = "${variant.flavorName}(${variant.versionCode}_${variant.versionName})(${time.format(nowTime)}).apk"
    }
  }
}

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

1.直接在configuration中排除

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

2.在具體的某個(gè)dependency中排除

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

PS:Android Studio開發(fā)Build.gradle小技巧

引用版本統(tǒng)一規(guī)范

Android開發(fā)存在著眾多版本的不同,比如compileSdkVersion、minSdkVersion、targetSdkVersion以及項(xiàng)目中依賴第三方庫的版本,不同的module及不同的開發(fā)人員都有不同的版本,所以需要一個(gè)統(tǒng)一版本規(guī)范的文件,現(xiàn)在我就來介紹一種方式。

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

淺析Android中build.gradle的實(shí)用技巧

在其最后添加如下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"
  ]
}

有了這個(gè)規(guī)范,那么我們在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文件即可把所有依賴版本都修改

對資源進(jìn)行分包

我們可以對每個(gè)頁面的資源都進(jìn)行具體分類,不只是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',
          ]
    }
  }
}

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向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