1. 程式人生 > >Gradle 翻譯 tips and recipes 使用技巧 MD

Gradle 翻譯 tips and recipes 使用技巧 MD

enca left rule roc fileinput main ani ica ads

Markdown版本筆記 我的GitHub首頁 我的博客 我的微信 我的郵箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 [email protected]

目錄

目錄
Gradle 提示與訣竅
管理項目和源代碼
更改默認源集配置:sourceSets
配置項目範圍的屬性:ext
管理庫和依賴項
將依賴項配置針對特定構建:configurations
創建不同版本的應用
配置多 APK 支持:splits、density、abi
配置動態版本代碼:android.applicationVariants.all
組合多個產品風味:flavorDimensions
過濾變體:variantFilter
測試應用(不常用)
配置 lint 選項:lintOptions
配置儀器 manifest 設置:test**
更改測試構建類型:testBuildType
配置 Gradle 測試選項:testOptions
優化您的構建
壓縮代碼:ProGuard
通過 Instant Run 啟用代碼壓縮:useProguard
配置 DEX 選項:dexOptions
發布您的應用
簽署您的應用:signingConfigs
從您的項目中移除私密簽署信息:.properties配置文件
簡化應用開發
與代碼共享自定義字段和資源值:buildConfigField 和 resValue
與 manifest 共享屬性:manifestPlaceholders

Gradle 提示與訣竅

Gradle tips and recipes

GradleAndroid Plugin for Gradle 提供了一種靈活[flexible]的方式來編譯、構建和打包您的 Android 應用或庫。本頁面匯總了一些有用提示和配置,旨在幫助您充分利用每一個構建[get the most out of each build]。

管理項目和源代碼

[Manage projects and sources]

下面是一些可用於管理您的項目的模塊及其源代碼的配置。要詳細了解如何創建和管理項目與模塊,請閱讀 項目概覽。

更改默認源集配置:sourceSets

[Change default source set configurations]

您可以使用模塊級 build.gradle 文件中的 sourceSets 代碼塊更改 Gradle 希望為源集 的每個組件收集文件的位置。

簡潔版:

android {
  sourceSets {
    main {
      java.srcDirs = [‘other/java‘] //默認為【src/main/java】
      res.srcDirs = [‘other/res1‘, ‘other/res2‘] //默認為【src/main/res】
      manifest.srcFile ‘other/AndroidManifest.xml‘ //默認為【src/main/AndroidManifest.xml】
    }
    androidTest {
      setRoot ‘src/tests‘ //默認有三個根目錄【src/androidTest】【src/test】【src/main】
    }
  }
}

完整版:

android {
  sourceSets {
    main { // Encapsulates將包封 configurations for the main source set.
      java.srcDirs = [‘other/java‘] // Changes the directory for Java sources.
/** If you list multiple directories, Gradle uses all of them to collect sources. Because Gradle gives these directories equal priority, if you define the same resource in more than one directory, you get an error when merging resources. */
      res.srcDirs = [‘other/res1‘, ‘other/res2‘]

/** Note: You should avoid specifying a directory which is a 【parent】 to one or more other directories you specify. For example, avoid the following: res.srcDirs = [‘other/res1‘, ‘other/res1/layouts‘, ‘other/res1/strings‘]. You should specify either only the root ‘other/res1‘ directory, or only the nested ‘other/res1/layouts‘ and ‘other/res1/strings‘ directories. */

/** For each source set, you can specify only one Android manifest. By default, Android Studio creates a manifest for your main source set in the src/main/ directory. */
      manifest.srcFile ‘other/AndroidManifest.xml‘
    }

    androidTest {  // Create additional blocks to configure other source sets.

/** If all the files for a source set are located under a single root directory, you can specify that directory using the setRoot property. When gathering搜集 sources for the source set, Gradle looks only in locations relative to the root directory you specify. For example, after applying the configuration below for the androidTest source set, Gradle looks for Java sources only in the src/tests/java/ directory. */
      setRoot ‘src/tests‘
    }
  }
}

配置項目範圍的屬性:ext

[configure project-wide properties]

對於包含多個模塊的項目,在項目級別定義屬性,然後在所有模塊間共享這些屬性可能會非常有用。為此,您可以將 extra properties 添加到 the top-level build.gradle 文件的 ext 代碼塊中。

簡潔版

ext {
    compileSdkVersion = 26
    supportLibVersion = "27.1.1"
}

完整版

buildscript {...}

allprojects {...}

// This block encapsulates封裝 custom properties and makes them available to all modules in the project.
ext { // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 26
    // You can also create properties to specify versions for dependencies指定依賴項的版本.
    // Having consistent一致的 versions between modules can avoid conflicts with behavior.
    supportLibVersion = "27.1.1"
    //...
}

要從相同項目中的模塊訪問這些屬性,請在 module-level build.gradle 文件中使用以下語法。

android {
  compileSdkVersion rootProject.ext.compileSdkVersion //語法【rootProject.ext.property_name】
}

dependencies {
    //語法【$rootProject.ext.property_name】或【${rootProject.ext.property_name}】
    compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
}

管理庫和依賴項

[Manage libraries and dependencies]

Gradle 提供了一種穩健的機制來管理依賴項,不管它們是遠程庫還是本地庫模塊。

將依賴項配置針對特定構建:configurations

[Target specific builds with dependency configurations]

如果您希望某個依賴項僅用於特定的構建變體源集或者測試源集,則必須大寫依賴項配置名稱並在其前面加上構建變體或測試源集的名稱作為前綴。

If you want a dependency for only a specific build variant source set or testing source set, capitalize the dependency configuration name and prefix it with the name of the build variant or testing source set.

簡潔版

configurations {
    freeDebugRuntimeOnly{} //初始化配置名稱,用於指定具體是哪個構建(此名稱代表 free Debug 的構建)
    feeDebugImplementation{}
}

dependencies {
    freeDebugRuntimeOnly fileTree(dir: ‘libs‘, include: [‘*.jar‘]) //僅為指定構建添加此依賴
    feeDebugImplementation ‘com.google.firebase:firebase-ads:9.8.0‘
}

完整版

android {...}

// Creates Gradle dependency configurations to use in the dependencies block.
configurations {
  // For variants that combine a product flavor and build type, you need to intitialize a placeholder for its dependency configuration.
    freeDebugRuntimeOnly{}
  //...
}

dependencies {
    freeImplementation ‘com.google.firebase:firebase-ads:9.8.0‘  // Adds an implementation dependency only to the "free" product flavor.
    freeDebugRuntimeOnly fileTree(dir: ‘libs‘, include: [‘*.jar‘]) // Adds a runtimeOnly dependency only to the "freeDebug" build variant.
    testImplementation ‘junit:junit:4.12‘ // Adds a remote binary dependency only for local tests.
    androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2‘ // Adds a remote binary dependency only for the instrumented test APK.
}

創建不同版本的應用

[Create different versions of your app]

Gradle 和 Android 插件允許您通過配置構建變體的方式從一個模塊創建不同版本的應用。

配置多 APK 支持:splits、density、abi

[Configure multiple APK support]

利用 Android 插件,您可以構建多個 APK,讓每一個都針對不同的 ABI 或屏幕密度,並充分利用 Google Play 的多 APK 支持。

按屏幕密度配置單獨的 APK
[Configure separate APKs per screen density]

要為不同的屏幕密度創建單獨的 APK,請將 android.splits.density 代碼塊添加到您的模塊的 build.gradle 文件中。

android {
  splits {
    density { // Configures multiple APKs based on screen density.
      enable true  // The default value is false
      exclude "ldpi", "xxhdpi", "xxxhdpi" //排除。Specifies a list of screen densities Gradle should not create multiple APKs for.
      compatibleScreens ‘small‘, ‘normal‘, ‘large‘, ‘xlarge‘  //兼容的 Specifies a list of compatible screen size settings for the manifest.
    }
  }
}

按 ABI 配置單獨的 APK
[Configure separate APKs per ABI]

要為每個 ABI 創建單獨的 APK,請將 android.splits.abi 代碼塊添加到您的模塊的 build.gradle 文件中。

android {
  splits {
    abi { // Configures multiple APKs based on ABI.
      enable true  // The default value is false
      // By default all ABIs are included, so use reset() and include to specify that we only want APKs for x86 and x86_64.
      reset() // Resets the list of ABIs that Gradle should create APKs for to none.
      include "x86", "x86_64"  // Specifies a list of ABIs that Gradle should create APKs for.
      universalApk false  // Specifies that we do not want to also generate a universal APK that includes all ABIs.
    }
  }
}

配置動態版本代碼:android.applicationVariants.all

[Configure dynamic version codes]

默認情況下,在 Gradle 為您的項目生成 APK 時,每個 APK 都有相同的版本信息,此信息在模塊級 build.gradle 文件中指定。由於 Google Play 商店不允許同一個應用的多個 APK 全都具有相同的版本信息,在上傳到 Play 商店之前,您需要確保每個 APK 都有自己唯一的 versionCode

為此,您可以使用自定義構建邏輯在構建時向每個 APK 分配不同的版本代碼。例如,在為每個 ABI 創建單獨的 APK 時,自動 APK 版本控制將如下所示:

簡潔版

ext.abiCodes = [‘armeabi-v7a‘:1, x86:2, x86_64:3]

import com.android.build.OutputFile
android.applicationVariants.all { variant -> 
  variant.outputs.each { output ->
    def baseAbiVersionCode = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))
    if (baseAbiVersionCode != null) {
       output.versionCodeOverride =  baseAbiVersionCode * 1000 + variant.versionCode
    }
  }
}

完整版

ext.abiCodes = [‘armeabi-v7a‘:1, x86:2, x86_64:3]

import com.android.build.OutputFile
android.applicationVariants.all { variant -> 
  variant.outputs.each { output -> // Assigns a different version code for each output APK other than the universal APK.
    def baseAbiVersionCode = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))// Determines the ABI for this variant and returns the mapped value.

/** Because abiCodes.get() returns null for ABIs that are not mapped by ext.abiCodes, the following code does not override the version code for universal APKs. However, because we want universal APKs to have the lowest version code, this outcome is desirable令人滿意的. */
    if (baseAbiVersionCode != null) {
/** Assigns the new version code to versionCodeOverride, which changes the version code for only the output APK, not for the variant itself. Skipping this step simply causes只會導致 Gradle to use the value of variant.versionCode for the APK. */
      output.versionCodeOverride =  baseAbiVersionCode * 1000 + variant.versionCode
    }
  }
}

組合多個產品風味:flavorDimensions

[Combine multiple product flavors]

某些情況下,您可能希望組合多個產品風味中的配置。為此,您可以通過 Android Plugin for Gradle 創建產品風味組,稱為風味維度。

下面的代碼示例使用 flavorDimensions 屬性創建一個“模式”風味維度以組織“完整”和“演示”產品風味,以及一個“api”風味維度以基於 API 級別組織產品風味配置。隨後,Gradle 會將“模式”維度的產品風味與“api”維度的產品風味組合。

簡潔版

android {
  buildTypes {
    debug {...}
    release {...}
  }

  flavorDimensions "api", "mode"

  productFlavors {
    demo {
      dimension "mode"  //風味維度:mode
    }
    full {
      dimension "mode"  //風味維度:mode
    }

    minApi24 {
      dimension "api"  //風味維度:api
      minSdkVersion ‘24‘
      versionCode 30000 + android.defaultConfig.versionCode
      versionNameSuffix "-minApi24"
    }
    minApi23 {...}  //風味維度:api
    minApi21 {...}  //風味維度:api
  }
}

完整版

android {
  buildTypes {
    debug {...}
    release {...}
  }

/** Specifies指定 the flavor dimensions you want to use. The order in which you list each dimension determines決定了 its priority優先級, from highest to lowest, when Gradle merges合並 variant sources and configurations. You must assign分配給 each product flavor you configure to one of the flavor dimensions. */
  flavorDimensions "api", "mode"

  productFlavors {
    demo {
      dimension "mode"  // Assigns this product flavor to the "mode" flavor dimension.
    }

    full {
      dimension "mode"
    }

/** Configurations in the "api" product flavors override覆蓋 those in "mode" flavors and the defaultConfig {} block. Gradle determines確定 the priority between flavor dimensions based on根據 the order in which they appear next to the flavorDimensions property above--the first dimension has a higher priority than the second, and so on. */
    minApi24 {
      dimension "api"
      minSdkVersion ‘24‘
/** To ensure the target device receives the version of the app with the highest compatible API level[要確保目標設備接收具有最高兼容API級別的應用程序版本], assign分配 version codes in increasing value with API level. */
      versionCode 30000 + android.defaultConfig.versionCode
      versionNameSuffix "-minApi24"
    }

    minApi23 {
      dimension "api"
      minSdkVersion ‘23‘
      versionCode 20000  + android.defaultConfig.versionCode
      versionNameSuffix "-minApi23"
    }

    minApi21 {
      dimension "api"
      minSdkVersion ‘21‘
      versionCode 10000  + android.defaultConfig.versionCode
      versionNameSuffix "-minApi21"
    }
  }
}

過濾變體:variantFilter

[Filter variants]

您可以使用模塊的 build.gradle 文件中的 variantFilter 代碼塊過濾構建變體,將您不想要的變體過濾掉。以下示例代碼將指示 Gradle 不構建任何可以將 minApi21 與 demo 產品風味組合的變體

簡潔版

android {
  variantFilter { variant ->
      def names = variant.flavors*.name //精準匹配時使用【variant.buildType.name == "<buildType>"】
      if (names.contains("minApi21") && names.contains("demo")) {
          //具體的邏輯
      }
  }
}

完整版

android {
  buildTypes {...}
  flavorDimensions "api", "mode"
  productFlavors {
    demo {...}
    full {...}
    minApi24 {...}
    minApi23 {...}
    minApi21 {...}
  }

  variantFilter { variant ->
      def names = variant.flavors*.name
      // To check for a certain build type, use 【variant.buildType.name == "<buildType>"】
      if (names.contains("minApi21") && names.contains("demo")) {
          setIgnore(true)  // Gradle ignores any variants that satisfy滿足 the conditions above.
      }
  }
}

測試應用(不常用)

[Test your app]

要詳細了解如何運行本地和集成單元測試,請閱讀測試應用。

配置 lint 選項:lintOptions

[Configure lint options]

您可以使用模塊級 build.gradle 文件中的 lintOptions 代碼塊配置特定的 lint 選項。要詳細了解如何為您的 Android 項目使用 lint,請閱讀使用 Lint 改進您的代碼。

簡潔版

android {
  lintOptions {
    disable ‘TypographyFractions‘,‘TypographyQuotes‘
    enable ‘RtlHardcoded‘, ‘RtlCompat‘, ‘RtlEnabled‘
    check ‘NewApi‘, ‘InlinedApi‘
    quiet true
    abortOnError false
    ignoreWarnings true
  }
}

完整版

android {
  lintOptions {
    disable ‘TypographyFractions‘,‘TypographyQuotes‘  // Turns off checks for the issue IDs you specify.
    enable ‘RtlHardcoded‘, ‘RtlCompat‘, ‘RtlEnabled‘ // These checks are in addition to the default lint checks.
/** To enable checks for only a subset of issue IDs and ignore all others, list the issue IDs with the ‘check‘ property instead. This property overrides any issue IDs you enable or disable using the properties above. */
    check ‘NewApi‘, ‘InlinedApi‘
    quiet true  // If set to true, turns off analysis progress reporting by lint.
    abortOnError false // if set to true (default), stops the build if errors are found.
    ignoreWarnings true // if true, only report errors.
  }
}

配置儀器 manifest 設置:test**

[Configure instrumentation manifest settings]

在 Gradle 構建您的測試 APK 時,它會自動生成 AndroidManifest.xml 文件並為其配置 <instrumentation> 節點。您可以在 測試源集 中創建另一個 manifest 文件或者配置您的模塊級 build.gradle 文件,通過這兩種方式更改此節點的一些設置,如以下代碼示例中所示。

簡潔版

android {
  defaultConfig {
    testApplicationId "com.test.foo"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"
    testFunctionalTest true
  }
}

完整版

android {
  defaultConfig {
    testApplicationId "com.test.foo"  // Specifies the application ID for the test APK.
    testInstrumentationRunner "android.test.InstrumentationTestRunner" // fully-qualified class name
    // If set to ‘true‘, enables the instrumentation class to start and stop profiling.
    // If set to false (default), profiling occurs the entire time the instrumentation class is running.
    testHandleProfiling true
    // If set to ‘true‘, indicates that the Android system should run the instrumentation class as a functional test. 
    testFunctionalTest true //The default value is ‘false‘
  }
}

更改測試構建類型:testBuildType

[Change the test build type]

默認情況下,所有測試均針對調試構建類型運行。您可以利用模塊級 build.gradle 文件中的 testBuildType 屬性將其更改為其他構建類型。例如,如果您想針對“staging”構建類型運行測試,請按下面這段代碼中所示對該文件進行編輯。

android {
    testBuildType "staging"
}

配置 Gradle 測試選項:testOptions

[Configure Gradle test options]

要指定可以更改 Gradle 運行所有測試方式的選項,請配置模塊級 build.gradle 中的 testOptions 代碼塊。

android {
  testOptions {
    // ‘$rootDir‘ sets the path relative to the root directory of the current project.
    reportDir "$rootDir/test-reports" //默認在 project/module_name/build/outputs/reports/
    resultsDir "$rootDir/test-results" //默認在 project/module_name/build/outputs/test-results/
  }
}

要僅為本地單元測試指定選項,請配置 testOptions.unitTests 代碼塊。

android {
  testOptions {
    unitTests {
/** By default, local unit tests throw an exception any time the code you are testing tries to access Android platform APIs (unless you mock嘲笑、藐視、不尊重 Android dependencies yourself or with a testing framework like Mockito). However, you can enable the following property so that the test returns either null or zero when accessing platform APIs, rather than throwing an exception. */
      returnDefaultValues true

      all { // controlling how Gradle executes local unit tests.
        jvmArgs ‘-XX:MaxPermSize=256m‘ // Sets JVM argument(s) for the test JVM(s).
        if (it.name == ‘testDebugUnitTest‘) {  // You can also check the task name to apply options to only the tests you specify.
          systemProperty ‘debug‘, ‘true‘
        }
      }
    }
  }
}

優化您的構建

[Optimize your build]

本部分將介紹一些有助於加快您的完整構建和增量構建速度的配置。

壓縮代碼:ProGuard

[Shrink your code]

Android Studio 使用 ProGuard 來壓縮代碼。對於新項目,Android Studio 將使用 Android_SDK/tools/proguard/ 下的默認設置文件 proguard-android.txt。要想進一步壓縮代碼,請嘗試使用位於同一位置的 proguard-android-optimize.txt 文件。

android {
  buildTypes {
    release {
      minifyEnabled true //默認為false
      proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt‘), ‘proguard-rules.pro‘ //默認配置
    }
  }
}

要添加特定於每個構建變體的 ProGuard 規則,請為每個風味配置其他 proguardFiles 屬性。例如,以下示例會將 flavor2-rules.pro 添加到“flavor2”中。現在,發布版本“flavor2”使用所有三個 ProGuard 規則,因為還應用了來自 release 代碼塊的規則。

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘),‘proguard-rules.pro‘
        }
    }
    productFlavors {
        flavor1 {
        }
        flavor2 {
            proguardFile ‘flavor2-rules.pro‘
        }
    }
}

通過 Instant Run 啟用代碼壓縮:useProguard

[Enable code shrinking with Instant Run]

要通過 Instant Run 啟用代碼壓縮,只需將 useProguard 設為 false(並保持 minifyEnabled 設為 true)。這將使用實驗性代碼壓縮器,它不會對您的代碼進行混淆處理或優化(因此,您應當僅為 debug 構建類型啟用此壓縮器)。

android {
    buildTypes {
        debug {
            minifyEnabled true
            useProguard false
            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
        }
    }
}

配置 DEX 選項:dexOptions

[Configure dex Options]

在 Gradle 將您的代碼編譯到 DEX 文件中時,使用以下屬性可以縮短構建時間。

android {
  dexOptions {
    maxProcessCount 8 // Sets the maximum number of DEX processes that can be started concurrently.
    javaMaxHeapSize "2g" // Sets the maximum memory allocation pool size for the dex operation.
    preDexLibraries true // Enables Gradle to pre-dex library dependencies.
  }
}

發布您的應用

[Publish your app]

要詳細了解如何將使用的應用發布到 Google Play,請閱讀發布您的應用。

簽署您的應用:signingConfigs

[Sign your app]

盡管 Android Studio 提供了一種從界面為發布構建配置簽署的簡單方式,您仍然可以手動配置模塊的 build.gradle 文件中的 signingConfigs 代碼塊:

android {
    defaultConfig {...}
    signingConfigs {
        release {
            storeFile file("myreleasekey.keystore")
            storePassword "password"
            keyAlias "MyReleaseKey"
            keyPassword "password"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

從您的項目中移除私密簽署信息:.properties配置文件

[Remove private signing information from your project]

默認情況下,簽署配置將以純文本形式記錄到模塊的 build.gradle 文件中。如果您正在與某個團隊合作或者參與一個開放源代碼項目,可以執行以下步驟,將此敏感信息移出構建文件。

簡潔版

def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(rootProject.file("keystore.properties"))) //加載配置文件

android {
  signingConfigs {
    config {
      keyAlias keystoreProperties[‘keyAlias‘]
      keyPassword keystoreProperties[‘keyPassword‘]
      storeFile file(keystoreProperties[‘storeFile‘])
      storePassword keystoreProperties[‘storePassword‘]
    }
  }
}

1、在項目的根目錄下創建一個名為 keystore.properties 的文件,並包含以下信息:

storePassword=myStorePassword
keyPassword=myKeyPassword
keyAlias=myKeyAlias
storeFile=myStoreFileLocation

2、在您的 build.gradle 文件中,按以下步驟操作來加載 keystore.properties 文件(必須在 android 代碼塊之前):

def keystorePropertiesFile = rootProject.file("keystore.properties") // Creates and initializes a variable to the keystore.properties file.
def keystoreProperties = new Properties() // Initializes a new Properties() object
keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) // Loads the keystore.properties file into the Properties object.

android { ... }

3、輸入存儲在 keystoreProperties 對象中的簽署信息:

android {
  signingConfigs {
    config {
      keyAlias keystoreProperties[‘keyAlias‘]
      keyPassword keystoreProperties[‘keyPassword‘]
      storeFile file(keystoreProperties[‘storeFile‘])
      storePassword keystoreProperties[‘storePassword‘]
    }
  }
}

要詳細了解應用簽署,請閱讀 簽署您的應用。

簡化應用開發

[Simplify app development]

下面的提示有助於簡化您的 Android 應用開發。

與代碼共享自定義字段和資源值:buildConfigField 和 resValue

[Share custom fields and resource values with your app‘s code]

在構建時,Gradle 將生成 BuildConfig 類,以便您的應用代碼可以檢查與當前構建有關的信息。您也可以使用 buildConfigField() 函數,將自定義字段添加到 Gradle 構建配置文件的 BuildConfig 類中,然後在應用的運行時代碼中訪問這些值。同樣,您也可以使用 resValue() 添加應用資源值。

簡潔版

android {
  buildTypes {
    release {
      buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")
      resValue("string", "build_time", "${minutesSinceEpoch}")
    }
  }
}

完整版

android {
  buildTypes {
    release {
      // These values are defined only for the release build, which is typically典型的 used for 【full builds】 and 【continuous builds】.
      buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")
      resValue("string", "build_time", "${minutesSinceEpoch}")
    }
    debug {
      // Use static values for incremental builds to ensure that resource files and BuildConfig aren‘t rebuilt重建 with each run.
      // If they were dynamic, they would prevent阻止 certain某一、某些 benefits利益、福利、優勢 of Instant Run as well as Gradle UP-TO-DATE checks.
      buildConfigField("String", "BUILD_TIME", "\"0\"")
      resValue("string", "build_time", "0")
    }
  }
}

在您的應用代碼中,您可以訪問以下屬性:

Log.i(TAG, BuildConfig.BUILD_TIME);
Log.i(TAG, getString(R.string.build_time));

與 manifest 共享屬性:manifestPlaceholders

[Share properties with the manifest]

某些情況下,您可能需要同時在 manifest 和代碼中聲明相同屬性(例如,在為FileProvider聲明機構時)。如以下示例中所示,請在模塊的 build.gradle 文件中定義一個屬性並使其對 manifest 和代碼均可用,而不必在多個位置更新相同的屬性以反映更改。要了解詳情,請閱讀 將構建變量註入 Manifest。

簡潔版

android {
  defaultConfig {
      manifestPlaceholders = [
          filesAuthority: "${applicationId}.files",
          app_logo         : "@drawable/icon",
          app_channel_value: "小米應用市場",
      ]
  }
}
android:authorities="${filesAuthority}"

完整版

android {
  defaultConfig {
      def filesAuthorityValue = applicationId + ".files" // Creates a property for the FileProvider authority.

      // Creates a placeholder property to use in the manifest.
      manifestPlaceholders = [filesAuthority: filesAuthorityValue,
                                    app_logo         : "@drawable/icon",
                                    app_channel_value: "小米應用市場",
      ]

      // Adds a new field for the authority to the BuildConfig class.
      buildConfigField("String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\"")
      buildConfigField "String", "BASE_URL", ‘"http://120.com/"‘
      buildConfigField "int", "countryCode", "20094"
  }
}

在您的 manifest 中,訪問以下占位符:

<manifest>
  <application>
    <provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="${filesAuthority}"
      android:exported="false"
      android:grantUriPermissions="true"/>
  </application>
</manifest>

以上內容上次更新日期:五月 8, 2018

2019-5-12

Gradle 翻譯 tips and recipes 使用技巧 MD