In addition to the two core modules of Project and Task, Gradle also has some important modules.
Knowledge graph
One, the Settings class
Two, resources, source code management-SourceSet
This class corresponds to the sourceSets closure, which is mainly used to manage the location of Android resources, java source code, third-party libraries, jar files and so files.
1. Chestnut-modify the default reading position of so
The default reading location on Android studio is the main-jinLibs folder
sourceSets{
// .so 默认 main/jniLibs 文件件下
main{
jniLibs.srcDir = "libs" // .so 文件位置修改
// jniLibs.srcDirs= ["libs","xxx"]//这些文件夹中读取 so
}
}
1. In this way, the so is loaded from the app’s libs folder
by default. 2. To modify other default locations in the system, just find it through the sourceSets closure default closure folder (mian above), and then modify it.
3. You can also modify multiple folders.
4. As above, if you get the main file, you can modify all the default locations under the main folder.
2. Demand res resource subcontracting management
Requirements: Subcontract management of resource files like java files. Chestnut: The advertisement resource is loaded under the res-ad folder, and the player resource is loaded under the res-player folder.
(1) Create a folder
In fact, we will also find that what we have created is a normal folder, without the small icon above the res folder.
(2) Modify SourceSet and synchronize the project
sourceSets {
main {
res.srcDirs = ["src/main/res", "src/main/res-ad", "src/main/res-player"]
}
}
You can see that our newly created folder can also put resources in the same function as res
3. Summary
1. sourceSets can be called multiple times, just call under the android closure. Or other places called android.sourceSets
2, the above two examples are commonly used in actual development
Three, custom Plugin
Fourth, andriod's extension to gradle
A simple gradle code in the Android project can use the Android plug-in! As follows, after creating an Android project, the system will generate it by default.
After introducing the simple code as above, we can use the attributes or tasks provided by the Android plugin under app/build.gradle.
1. All configurable properties in Android
After the introduction of the Android plugin, What are the configurations available for android{} in app/build.gradle under the Android project? What methods can be called? In fact, just look at the BaseExtension class, which defines what Android closures can configure . The class is as follows:
ps: Check the corresponding class for the configured sub-configuration.
2. Task introduced by Android plugin
1. The Task and attributes that can be used by the Android plug-in are defined in the BaseVariant class.
2. There are several common forms of variants in Android1. Application type: application type variant
2. Library type: Android library project type variant
3. Test type: rarely usedps: For details, please refer to the translation document on github
3. Chestnuts:By variantManipulate the properties of the Android plugin configuration
// 遍历所有变体(通过all闭包)
this.android.applicationVariants.all {
variant ->
def baseName = variant.baseName
def name = variant.name
println "baseName:$baseName"
println "Name:$name"
}
log:
> Configure project :app
baseName:debug
Name:debug
baseName:release
Name:release
1. Get the collection of variants in Android: android.applicationVariants
2. By default, Android has a default channel, which has two variants:1、debug
2、release3. Users can configure multiple channels, each channel will have debug and release variants
4. Chestnuts:By variantOperate Task in Android plugin
this.afterEvaluate {
// 遍历所有变体
this.android.applicationVariants.all {
variant ->
Task task = variant.checkManifest
println(task.name)
}
}
Simply get the tasks in the application
Five, ant, maven migrate to gradle
1. Two ways
- Migration: The compiler installs the gradle plugin and exports it as a gradle project.
Core: Why can different project structures run after modification? : Gradle modified the sourceSet file of build.gradle.
Harvest: Different build projects may read different resource directories. Just use gradle's sourceSet to read the specified directory.
- Retrofit: Modify the sourceSet to read the directory by default. (For example, Android studio reads so under libs, just modify the default reading position of sourceSet (default reading position main/jniLibs))