I. Overview
The unification of the Android development version involves the global setting in Gradle, and we set the version number in development to the global parameter by configuring Gradle, which is writing Groovy code. This allows the same version number to be accessed in the module or library for unification. The global settings in Gradle are as follows:
Engineering Build.gradle
Ext method
Gradle.properties file
-P parameter
Second, global Settings 1. Engineering Build.gradle
allprojects { Apply plugin: ' Com.android.application ' android { compilesdkversion Buildtoolsversion "22.0.1" }}
This will only work if all of your modules are Android apps. You need to add the Android plugin to access Android's tasks.
2. Ext method
ext { Compilesdkversion = buildtoolsversion = "22.0.1"}
You define some properties in the global Gradle file, and then you can use those properties in the Submodule.
Android { compilesdkversion rootProject.ext.compileSdkVersion buildtoolsversion RootProject.ext.buildToolsVersion}
3. gradle.properties file
Compile_sdk_version
"22.0.1"
Once defined in the file, you can use it directly in the submodule:
Android {
Compilesdkversion compile_sdk_version
Build_tools_version
}
4.-P parameter
This is the way the command line works, such as when we define a task in Gradle:
Task PrintProperties << { if (project.hasproperty (' cmd ')) { println cmd /Command line property }}
We then set the parameters from the command line:
$ gradlew printproperties-pcmd= ' Hello from the command line '
After performing the task output:
Hello from the command line
Third, the unified version
Into the topic, let's see how we can share our same version to achieve unity. Let's start by creating a Gradle file that puts the version number Version.gradle
Then we define it by ext:
Ext.deps = [:]//Empty map
def versions = [:]
Versions.support = "27.1.1"
Versions.constraint_layout = "1.0.2"
Versions.junit = "4.12"
Versions.test_runner = "1.0.2"
Versions.test_espresso = "3.0.2"
Versions.android_gradle_plugin = "3.1.2"
def deps = [:]
def support = [:]
Support.v7 = "COM.ANDROID.SUPPORT:APPCOMPAT-V7: $versions. Support"
Deps.support = Support
View
Deps.constraint_layout = "Com.android.support.constraint:constraint-layout: $versions. Constraint_layout"
Deps.android_gradle_plugin = "Com.android.tools.build:gradle: $versions. Android_gradle_plugin"
def test = [:]
Test.junit = "Junit:junit: $versions. JUnit"
Test.runner = "Com.android.support.test:runner: $versions. Test_runner"
Test.test_espresso = "Com.android.support.test.espresso:espresso-core: $versions. Test_espresso"
Deps.test = Test
Ext.deps = Deps
def build_version = [:]
BUILD_VERSION.MIN_SDK = 15
BUILD_VERSION.TARGET_SDK = 27
Build_version.build_tools = "27.1.1"
Ext.build_version = Build_version
def addrepos (Repositoryhandler handler) {
Handler.google ()
Handler.jcenter ()
Handler.mavencentral ()
Add the Jitpack repository
Handler.maven {URL "Https://jitpack.io"}
handler.maven{url ' http://maven.aliyun.com/nexus/content/groups/public/'}
}
Ext.addrepos = This.&addrepos
This defines our version, and the way to write it is Groovy code. Here's what we're going to apply the Version.gradle file to the project:
apply from: Indicates a reference to another configuration file
Item Build.gradle
Buildscript { "Version.gradle"}
After Sync Project, we can use it to look at the use of the submodule:
Apply plugin: ' Com.android.application '
Android {
Compilesdkversion BUILD_VERSION.TARGET_SDK
Defaultconfig {
ApplicationID "Com.sl.gradledemo"
Minsdkversion BUILD_VERSION.MIN_SDK
Targetsdkversion BUILD_VERSION.TARGET_SDK
Versioncode 1
Versionname "1.0"
Testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"
}
Signingconfigs {
Release {
StoreFile file (release_store_file)
Storepassword Release_store_password
Keyalias Release_key_alias
Keypassword Release_key_password
}
}
Buildtypes {
Release {
Minifyenabled false
Signingconfig Signingconfigs.release
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '
Buildconfigfield ("String", "Api_url", "\" Https://www.baidu.com\ "")
Buildconfigfield ("boolean", "Log_state", "false")
Resvalue ("string", "Show_name", "Release_app")
Resvalue ("string", "App_name", "Release_app")
}
Debug {
Buildconfigfield ("String", "Api_url", "\" Http://192.168.142.30\ "")
Buildconfigfield ("boolean", "Log_state", "true")
Resvalue ("string", "Show_name", "Debug_app")
Resvalue ("string", "App_name", "Dubug_app")
}
}
}
dependencies {
Implementation Filetree (dir: ' Libs ', include: [' *.jar '])
Implementation DEPS.SUPPORT.V7
Implementation Deps.constraint_layout
Testimplementation Deps.test.junit
Androidtestimplementation Deps.test.runner
Androidtestimplementation Deps.test.test_espresso
}
This allows all of our submodules to be configured with the same version as above, with only the version defined in Version.gradle. So we need to update the version when it is not convenient a lot.
Android Development Version Unified