android presentation - gradle ++
TRANSCRIPT
![Page 1: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/1.jpg)
Gradle
![Page 2: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/2.jpg)
1.What is this?General purpose build system made in groovy that allows you to execute the needed commands to build software.
Manages:- Build process- Dependencies- Project files
![Page 3: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/3.jpg)
1.What is this?Similar products:- Ant- Maven- Gant- Ivy- Rake- Buildr- ...
![Page 4: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/4.jpg)
1.What is this?Big winIt is a full language itself, not a configuration file.
Gradle just provides a lifecycle and some classes to help you
![Page 5: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/5.jpg)
2. How it works?Manages with some classes:- Build process- Dependencies- Project files
USING- Tasks- Projects- Repositories- Dependencies
![Page 6: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/6.jpg)
Key concepts:- BuildScript- Project- Repositories- Tasks- Plugin
2. How it works?
![Page 7: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/7.jpg)
BuildScript
2. How it works?
Setup script to add dependencies on the building process. Ex. plugin dependencies
![Page 8: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/8.jpg)
Project
2. How it works?
Element that represents the current project and is considered the top level item. Contains all the important information, plugins, files and source directories.
![Page 9: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/9.jpg)
Repositories
2. How it works?
Defines which are the sources of the possible dependencies. Ex. MavenCentral, jCenter (bintray)
![Page 10: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/10.jpg)
Tasks
2. How it works?
Pieces of code that are able to perform some action defined. Tasks can be dependent from each other and they can receive actions before or after.
![Page 11: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/11.jpg)
Plugins
2. How it works?
Programs that evaluates during the build process and are able to add behaviour and tasks to the current project.
![Page 12: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/12.jpg)
DSL based on Groovy but...
- You can do whatever you can do in the Groovy language
- Use the DSL to make it look like a configuration file
3. DSL Syntax
![Page 13: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/13.jpg)
Class DSL
3. DSL Syntax
![Page 14: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/14.jpg)
Class DSL
3. DSL Syntax
![Page 15: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/15.jpg)
Class DSL
3. DSL Syntax
![Page 16: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/16.jpg)
4. Build process lifecycle- Compile build in .gradle cache- Execute buildscript- First round to configure dependencies and source directories- afterEvaluate execution to generate tasks based on first step- Execute whatever task you want
![Page 17: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/17.jpg)
5. TaskDeclare a task
![Page 18: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/18.jpg)
5. TaskAdd dependency between tasks
![Page 19: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/19.jpg)
5. Task- taks.doFirst- task.doLast (<<)- project.findBy..name, path...- extend tasks
![Page 20: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/20.jpg)
5. TaskExecute
./gradlew hellohello
![Page 21: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/21.jpg)
6. Pluginsapply plugin: "name"
where "name" is declared in META-INF in the plugin project.
Same as: project.apply(MyPluginClass)
![Page 22: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/22.jpg)
6. Pluginspublic class MyPlugin extends Plugin<Project> {
@Overridepublic void apply(Project project){}
}
![Page 23: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/23.jpg)
7. Android plugin- Android DSL- Build types- Flavors- Dimensions- Default configuration properties
![Page 24: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/24.jpg)
7. Android pluginBuild type
- Application build configuration- Default build types are debug and release
DSL: http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.BuildType.html
![Page 25: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/25.jpg)
7. Android pluginFlavor
- Specific files for the build- Version code, variables, dimensions- No needed/No default
DSL: http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html
![Page 26: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/26.jpg)
7. Android pluginDimension
- Mix different flavors in a single one- Descriptor for a flavor
![Page 27: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/27.jpg)
7. Android plugin
Is a = Flavor(s) + BuildType
Variant
ExampleBuild type: debugFlavor: free
Tasks: installFreeDebug, assembleFreeDebug...
![Page 28: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/28.jpg)
8. Show me the code
Default flavor without name
Tell the compiler where to find the tools in the ANDROID_HOME
Plugin that defines the dsl (library and app)
![Page 29: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/29.jpg)
8. Show me the code
- Searches on those repos defined in repositories (jcenter, mavenCentral)
- Takes the group:artifact:version- Allows to define dependencies inside the
project with plain jars
![Page 30: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/30.jpg)
Black Magic with gradle
When integration with third parties sucks
![Page 31: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/31.jpg)
Compiling libs per flavordependencies {
mppNotProductionCompile fileTree(dir: 'mppNonProd', include: '*.jar')
mppProductionCompile fileTree(dir: 'mppProd', include: '*.jar')
}
When third parties give several SDK per platform. As it is a dependency it’s an unique, so values cannot override the others, you just need to choose what to compile
![Page 32: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/32.jpg)
Use resources per flavor
Usually you can define resources per flavor just making a folder matching the flavor. In case you need to reuse in a different way you can iterate between flavors in the sourceSets. For example:
[qaUat, qaApplauseTestersqa, qaMppQaAlpha].each { item ->
item.res.srcDirs = ['res_qa'] }
[prod, prodMppInt, prodMppPreprod, prodMppQa].each { item ->
item.res.srcDirs = ['res_prod'] }
![Page 33: Android presentation - Gradle ++](https://reader036.vdocuments.site/reader036/viewer/2022082212/58ed74861a28ab191b8b46c5/html5/thumbnails/33.jpg)
Flavours to the infinity
The examples before were using 1 flavor to define free/paid and one for buildType.
You can define as much as you want: flavorDimensions 'apptype', 'environment','mpayments'
This allows you combinations of:
apptype+environment+mpayments+buildType. BuildType is always the last.
The more flavorDimensions you have, the more number of variants you will be able to create.