Notes for upgrading gradle tool to 3.0: gradle3.0

Source: Internet
Author: User

Notes for upgrading gradle tool to 3.0: gradle3.0
Gradle Version Upgrade

In fact, after the AS upgrade to 3.0, Gradle Plugin and Gradle can continue to be used without upgrading. However, many new features include: new functions such as Java 8 support, New dependency matching mechanism, and AAPT2 cannot be used normally.

Upgrade Gradle Plugin to 3.0.0 or later, and modify the project/build. gradle file:

Modify global. gradle (gradle managed by custom lib) tools = [gradleTools: 'com. android. tools. build: gradle: 3.0.1 '] modify project/build. gradle file buildscript {repositories {google ()} apply from: 'Global. gradle 'def tools = rootProject. ext. tools dependencies {classpath tools. gradleTools }}

Upgrade Gradle to 4.1 or above and modify the project/gradle/gradle-wrapper.properties File

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
Generate APK file name attribute outputFile to read-only

Code for changing the apk name before modifying the build. gradle file in app module

variant.outputs.each { output ->    def outputFile = output.outputFile    if (outputFile != null && outputFile.name.endsWith('.apk')) {        def fileName = "host_${variant.buildType.name}_${variant.productFlavors[0].name}_${mApplicationId}_${defaultConfig.versionCode}_v${mVersionName}.apk"        output.outputFile = new File(outputFile.parent, fileName)    }}

As the outputFile attribute changes to read-only, you need to make the following changes and directly assign values to the outputFileName attribute:

variant.outputs.all {    outputFileName = "host_${variant.buildType.name}_${variant.productFlavors[0].name}_${mApplicationId}_${defaultConfig.versionCode}_v${mVersionName}.apk"}
Changes in dependency keywords
  • Api: corresponds to the previous compile keywords, with identical functions. Dependencies are passed, causing gradle to traverse the entire dependency tree during compilation.
  • Implementation: corresponds to the previous compile, similar to the api. The key difference is that there is no dependency transfer.
  • CompileOnly: corresponds to the previous provided dependency, which is only used for compilation and will not be packaged into the final apk.
  • RuntimeOnly: corresponds to the previous 'apk ', which is opposite to compileOnly

The difference between implementation and api mainly lies in whether dependencies are transmitted. For example, A depends on B and B on C. If api is used, A can reference C, but implementation cannot.

Implementation is recommended here. First, references are not indirectly exposed to clearly understand the dependencies of the current project. Second, the dependency tree search speed during compilation can be improved to increase the compilation speed. The channel must declare flavor dimensions

At the beginning of Sync, an error should be reported:
Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

That is, each flavor channel must belong to a dimension. If there is only one dimension, the dimension attribute can be left empty in the channel, which is assigned to this dimension by default. Simply add a default dimension, for example, flavorDimensions "dimension"

The solution is to add flavorDimensions in app. gradle.

defaultConfig {    flavorDimensions  getVersion("VERSION_CODE")}

You can also set multiple dimensions as in the official document.

// Specifies two flavor dimensions.flavorDimensions "mode", "minApi"productFlavors {    free {        // Assigns this product flavor to the "tier" flavor dimension. Specifying        // this property is optional if you are using only one dimension.        dimension "mode"        ...    }    paid {        dimension "mode"        ...    }    minApi23 {        dimension "minApi"        ...    }    minApi18 {        dimension "minApi"        ...    }}
Multi-variant dependency Modification

After Gradle plugin 3.0.0 +, a new automatic variant matching mechanism is introduced. That is to say, the flavorDebug of the app automatically matches the flavorDebug variant of the library.

Let's review the old method. If the app needs to depend on the corresponding library type in a variant, declare the dependency as follows:

dependencies {    hytestCompile project(path: ':main', configuration: 'hytestRelease')    productionCompile project(path: ':main', configuration: 'productionRelease')}

In the new method, gradle automatically detects and matches the corresponding variant (provided that the app and library have the corresponding variant type ):

dependencies {    implementation project(':main')}
Trap Filling

1. style attribute '@ android: attr/windowEnterAnimation' not found. This is caused by aapt2. 3.0 aapt2 is enabled by default. Solution: add the code to close aapt2 at the end of gradle. properties under the root directory of the project:

android.enableAapt2=false

2. gradle tool 3.0 uses the lowest SDK buildTool 26.0.2

3. If aapt is used, remove it. Use annotationProcessor instead.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.