1. Top Gradle Defines Extended Variables
In build.gradle at the Project level, use apply from:'component.gradle', introducing the component.gradle configuration;
// Import the contents of the component.gradle configuration file to this location // Equivalent to introducing a header file apply from: "component.gradle"
Apply front is equivalent to introducing a header file that copies everything in the component.gradle configuration file to the location of the statement in its original state.
component.gradle is a developer-defined configuration file written in Groovy;
ext is an extension that allows you to define extended variables.
The component.gradle file reads as follows: The variables used globally are defined here;
// ext is the meaning of an extension // The {} curly bracket after the ext is a closure. ext{ // Defines an android variable of type Dictionary Map Collection // There are several sets of key-value pairs defined androidConfig = [ compileSdkVersion : 30, minSdkVersion : 18, targetSdkVersion : 30, versionCode : 1, versionName : "1.0" ] applicationId = [ "app" : "kim.hsl.component", "mylibrary" : "", "mylibrary2" : "", ] }
Variables defined in build.gradle at the Project level can be used in build.gradle at the Module level;
For example, if the component.gradle configuration is introduced in build.gradle at the Project level above, the Android variable defined in component.gradle can be invoked in build.gradle at the Modele level through rootProject.ext.android;
The variable defined in the top-level build script can be obtained by the build script in any Module;
2. Get the top Gradle object
Calling rootProject in build.gradle is equivalent to calling the getRootProject() method of the Project object, which is a grammatical sugar provided by Groovy and is similar to the Kotlin usage;This method returns a Projectobject that represents the Projectobject corresponding to the build.gradle build script at the Projectlevel in the root directory.
RootProjects correspond to top-level Projectlevel build.gradle;
Projects correspond to getRootProject method source code:
public interface Project extends Comparable<Project>, ExtensionAware, PluginAware { String DEFAULT_BUILD_FILE = "build.gradle"; String PATH_SEPARATOR = ":"; String DEFAULT_BUILD_DIR_NAME = "build"; String GRADLE_PROPERTIES = "gradle.properties"; Project getRootProject(); }
3. Use Gradle variable in Module
The above defines the extension variable in the top-level build.gradle, which can be obtained in build.gradle under Module;
Using the rootProject.ext.androidConfig code, you can get the androidConfig variable defined in the top-level component.gradle;
// Defines an android variable of type Dictionary Map Collection // There are several sets of key-value pairs defined androidConfig = [ compileSdkVersion : 30, minSdkVersion : 18, targetSdkVersion : 30, versionCode : 1, versionName : "1.0" ]
Assigning the rootProject.ext.androidConfig variable to the local def androidConfig variable, DEF is equivalent to Object in Java, def androidConfig is declaring a new variable named androidConfig;
// def is equivalent to Object in Java // Declare and assign config and appId variables def androidConfig = rootProject.ext.androidConfig def appId = rootProject.ext.applicationId
By calling androidConfig.compileSdkVersion, you can get the value of 30 for the corresponding compileSdkVersion key in the androidConfig variable defined in component.gradle;
By calling androidConfig.minSdkVersion, you can get the value of 18 for the minSdkVersion key corresponding to the androidConfig variable defined in component.gradle;
Some code examples:
// def is equivalent to Object in Java // Declare and assign config and appId variables def androidConfig = rootProject.ext.androidConfig def appId = rootProject.ext.applicationId android { compileSdkVersion androidConfig.compileSdkVersion buildToolsVersion "30.0.3" defaultConfig { applicationId appId["app"] minSdkVersion androidConfig.minSdkVersion targetSdkVersion androidConfig.targetSdkVersion versionCode androidConfig.versionCode versionName androidConfig.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } }
The advantage of this is that you can manage the configuration versions of Android applications in a unified way, without having to modify each Module independently.
IV. Printing Variable Values in Gradle
Print the output variable value in build.gradle, use the println function to print the variable in build.gradle under Modele, place the variable in'${}', and print the result to the Build panel at compile time;
println("Print Variable : rootProject.ext.android : ${rootProject.ext.android}")
Note: Do not print Chinese, chaos will occur;
5. The Gradle build script involved
Top Gradle code example:
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Import the contents of the component.gradle configuration file to this location // Equivalent to introducing a header file apply from: "component.gradle" buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.0" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
Gradle code introduced:
// ext is the meaning of an extension // The {} curly bracket after the ext is a closure. ext{ // Defines an android variable of type Dictionary Map Collection // There are several sets of key-value pairs defined androidConfig = [ compileSdkVersion : 30, minSdkVersion : 18, targetSdkVersion : 30, versionCode : 1, versionName : "1.0" ] applicationId = [ "app" : "kim.hsl.component", "mylibrary" : "", "mylibrary2" : "", ] }
Module-level Gradle code example: Component\app\build.gradle build script;
plugins { id 'com.android.application' id 'kotlin-android' } println("Print Variable : rootProject.ext.androidConfig : ${rootProject.ext.androidConfig}") // def is equivalent to Object in Java // Declare and assign config and appId variables def androidConfig = rootProject.ext.androidConfig def appId = rootProject.ext.applicationId android { compileSdkVersion androidConfig.compileSdkVersion buildToolsVersion "30.0.3" defaultConfig { applicationId appId["app"] minSdkVersion androidConfig.minSdkVersion targetSdkVersion androidConfig.targetSdkVersion versionCode androidConfig.versionCode versionName androidConfig.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
6. Blog Resources
Blog source:
- GitHub : https://github.com/han1202012/Component
- CSDN download: