Contents 1, Preface 2, Brief analysis of sourceSets structure 3, The attribute of sourceSets
3.1 java
3.2 assets
3.3 aidl
3.4 jni
3.5 jniLibs
3.6 manifest
3.7 renderscript
3.8 res
3.9 resources
4. SourceSets method
4.1 setRoot (String path)
Write at the end
One, foreword
In Android projects, code is placed by convention. This is due to gradle’s philosophy of “convention over configuration”, which reduces the need to write configurations.
But conventions are not rules, so configuration changes can be made, which is one of gradle’s great advantages. In Android, we configure through sourceSets.
2. Brief analysis of sourceSets structure
As usual, let’s look at the existing structure of sourceSets
android{
sourceSets{
main{
}
// The hello flavor is superimposed on the main instead of being replaced
hello{
}
}
flavorDimensions('product')
productFlavors{
hello{
dimension 'product'}}}Copy the code
There are three things we can learn from the above code
- SourceSets can be customized for different flavors, such as Hello Flavors, which has its own resource catalog.
- Main is the main trunk, other flavors are set as branches, and branch Settings are merged into the trunk, such as the [Hello flavor] configuration here.
- SourceSets can be loaded with multiple sourceSets, such as main and Hello.
So according to the formula, we’ll look at sourceSet inheritance structures, the manifestation of sourceSet class for the com. Android. Build. Gradle. API. AndroidSourceSet is an interface, no other class inheritance.
The attributes of sourceSets
3.1 java
- Type: AndroidSourceDirectorySet
- Description: Sets the location where Java code is stored
- Usage:
main {
java {
srcDirs = [
'src/main/java'.'src/main/patternFilterableDemo/exclude'
]
excludes = ['zincStudy/*.java']
includes = ["com/zinc/gradlestudy/MainActivity.java"]}}Copy the code
AndroidSourceDirectorySet has three attributes can be set, we parse one by one:
3.1.1 srcDirs
Description:An existing Java code pathFor example, above we set up two foldersjava
和 exclude
3.1.2 includes
Description: Set which classes to compile and package for the “srcDirs” folder path we set.
The default value for this property is **/*.java (see the official documentation for PatternFilterable for the rules), which contains all folders in the directory and all files under it.
Our example here is set"com/zinc/gradlestudy/MainActivity.java"
, the final effect of apK package is shown in the figure below
3.1.3 excludes
Description: Set which classes will not be compiled and packaged for the “srcDirs” folder path we set.
Here’s an example:
main {
java {
srcDirs = [
'src/main/java'.'src/main/patternFilterableDemo/exclude'
]
excludes = ['zincStudy/*.java']}}Copy the code
We only useexcludes
, the final code structure of APK is as follows, which is no longer visiblezincStudy
The code of
Note: The value of excludes and excludes is set to the folder path set by srcDirs.
3.2 assets
- Type: AndroidSourceDirectorySet
- Description: Set the location where assets are stored
- Usage:
assets.srcDirs 'src/main/assets'.'src/main/zincAssets'
Copy the code
3.3 aidl
- Type: AndroidSourceDirectorySet
- Description: Set the location where aiDL is stored
- Usage:
aidl.srcDirs 'src/main/aidl'.'src/main/zincAidl'
Copy the code
3.4 the jni
- Type: AndroidSourceDirectorySet
- Description: Set the location where jNI is stored
- Usage:
jni.srcDirs Directory '1'.Directory '2'
Copy the code
3.5 jniLibs
- Type: AndroidSourceDirectorySet
- Description: Set the storage location of jniLibs
- Usage:
jniLibs.srcDirs Directory '1'.Directory '2'
Copy the code
3.6 the manifest
- Type: AndroidSourceFile
- Description: Set the location of the manifest
- Usage:
manifest.srcFile 'src/main/ZincManifest.xml'
Copy the code
3.7 renderscript
- Type: AndroidSourceDirectorySet
- Description: Set the location where renderScript is stored
- Usage:
renderscript.srcDirs 'xxx'.'xxx'
Copy the code
3.8 res
- Type: AndroidSourceDirectorySet
- Description: Set the location where the RES is stored
- Usage:
res.srcDirs 'src/main/res'.'src/main/zinc-res'
Copy the code
3.9 the resources
- Type: AndroidSourceDirectorySet
- Description: Sets the location where Java resources are stored
- Usage:
resources.srcDir 'xxx'.'xxx'
Copy the code
4. SourceSets method
4.1 setRoot (String path)
-
Description: This method invalidates the properties we shared above. Once we have setRoot set up, Gradle will look for resources in the same directory we set up. (See the following examples)
-
Usage:
zinc {
// If our code is all in the same directory, we can use setRoot to set it,
// With setRoot set, gradle only looks for resources in the same directory.
// For example, only find Java code in SRC /zincPower/ Java
// The path set by java.srcdirs is ignored
setRoot 'src/zincPower'
// This will be ignored
java.srcDirs 'zzz'
}
Copy the code
Write at the end
SourceSets is relatively simple, but very useful, because in a project, to manage the code structure is a very important task, and to manage the structure of the code of a task is to use a different folder to different function code in the reasonable position, the advantage is easy to remove (premise is decoupling is enough, a bit digress).
If you like, please give me a like and follow me. If there is anything wrong in the article, please discuss it with me in the comments section and make progress together.