Refreshed the understanding of gradle.properties in Android Studio

I usually don’t pay much attention to this file. It’s just that the boss put the signature information here before and it worked, so I learned to do it like this. I didn’t know the principle or anything. I accidentally saw the buildToolsVersion and gradle of AndroidStudio localization configuration . In this article of gradleBuildTools , I understand why this can be done.
The following content comes from AndroidStudio localization configuration gradle's buildToolsVersion and gradleBuildTools

gradle.properties has two very useful properties :

  • 1. The constants in gradle.properties can be read in any build.gradle file in the Android project, regardless of whether the build.gradle is a component or the build.gradle of the entire project;
  • 2. The data types in gradle.properties are all String types. If you use other data types, you need to convert them yourself;

Problems that gradle.properties can solve
: Using the properties of gradle.properties can solve the problem of frequent changes to online projects due to inconsistent Android development environments of development team members (the development environment refers to Android SDK and AndroidStudio) when multiple people are collaboratively developing Android projects. The build.gradle configuration.

  • 1. There is an attribute in the build.gradle of each Android component: buildToolsVersion, which indicates the version number of the build tool. This attribute value corresponds to the Android SDK Build-tools in the AndroidSDK. Under normal circumstances, the buildToolsVersion in build.gradle is the same as your computer. The latest version of Android SDK Build-tools is consistent. For example, the latest version of Android SDK Build-tools is now: 25.0.3, then the buildToolsVersion version number in build.gradle in my Android project is also 25.0.3, but Once an Android project is developed by several people at the same time, everyone's development environment and Android SDK Build-tools will always be different. Not everyone will frequently upgrade and update the Android SDK, and the code is saved to the online environment. (for example, using tools such as SVN/Git), the buildToolsVersion in build.gradle in the online Android project will also be continuously changed after a developer submits the code.
  • 2. Another reason is that the build.gradle in the root directory of the Android project declares the Android Gradle build tool, and this tool also has a version number, and the version number of Gradle Build Tools is consistent with the AndroidStudio version number, but some developers Basically, people will not upgrade their AndroidStudio version for a long time, resulting in the version number of Gradle Build Tools of each developer in the team being inconsistent.
  • If the version numbers of the two tools are changed each time the code is synchronized, developers can manually change them back by themselves, and do not submit the code that changes the tool version numbers to the online environment, so that they can barely continue development; but many companies Continuous integration tools (such as Jenkins) will be used for continuous software version release, and Android packaging requires the cooperation of Android SDK Build-tools and Gradle Build Tools. Once submitted to the online version, it will be linked to the Android on which the continuous integration tool depends. Inconsistent version numbers of environment build tools will cause Android packaging to fail.

In order to solve the above problem, it is necessary to isolate the buildToolsVersion and GradleBuildTools version numbers in build.gradle in the Android project from the online code to ensure that the buildToolsVersion and Gradle Build Tools version numbers of the online code will not be artificially changed.

Configuration strategy
1. First define two constants in the gradle.properties of the Android project: localBuildToolsVersion and localGradlePluginVersion, which represent the version numbers of buildToolsVersion and Gradle Build Tools respectively:

# 为自动化出包配置(因为每个开发的电脑坏境不一致)
localBuildToolsVersion=25.0.3
# 这个值一般跟你的AndroidStudio版本号一致
localGradlePluginVersion=2.3.2

2. Then reference localGradlePluginVersion in build.gradle of the Android project:

dependencies {
        //classpath "com.android.tools.build:gradle:$localGradlePluginVersion"
        //$localGradlePluginVersion是gradle.properties中的数据
        classpath "com.android.tools.build:gradle:$localGradlePluginVersion"
    }

3. Reference localBuildToolsVersion in the component’s build.gradle:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    //localBuildToolsVersion是gradle.properties中的数据
    buildToolsVersion localBuildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
    }
}

4. Finally, ignore gradle.properties from the version control tool (Git, SVN); then send a copy of the configured gradle.properties to each developer.

Guess you like

Origin blog.csdn.net/fengyulinde/article/details/106803725