3. About me
› 4+ years of Android development
› Mobile game-dev experience
› At Yandex:
1. Mobile Yandex.Metrica
2. Continuous Integration
4. The image cannot be displayed. Your computer may not have
enough memory to open the image, or the image may have
been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the
image and then insert it again.
Intro
Why do I need this?
5. What can be done?
› Additional resources/code/manifest processing
› Output processing (apk, aar, jar)
› Other things
6. Flavors
2 Flavors!
Story
Prod / test serversLogs on/offTest / prod analyticsAds on/off
Unique build number!...
I want to configure it
myself!
3 Flavors?Hmm…
7. Story
Prod / test
servers
Flavors!
Logs on/offTest / prod
analyticsAds on/offUnique build
number!...
I want to
configure it
myself!
2 Flavors!3
Flavors?Hmm...
Android devManager
8. How should it work
Java code build.gradle Teamcity
Actual value:
"https://my.server.com"
CI server can do it
Our job
10. Use BuildConfig.java from Java
public class SomeJavaClass {
// ...
public static final String SERVER_URL = "https://my.server.com";
public static final String SERVER_URL = BuildConfig.URL;
// ...
}
public static final String SERVER_URL = "https://my.server.com";
13. Table of contents
› Gradle basics
› New Build System workflow
› Hello, Gradle plugin!
› Extending Android New Build System
14. The image cannot be displayed. Your computer may not have
enough memory to open the image, or the image may have
been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the
image and then insert it again.
Gradle basics
Tools we will use
23. The image cannot be displayed. Your computer may not have
enough memory to open the image, or the image may have
been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the
image and then insert it again.
The New Build System
workflow
What's so special?
24. What tasks will be launched?
build
check assemble
assembleDebug assembleRelease
assemble<VariantName>Guaranteed
26. Tasks we will need
assemble<VariantName>
generate<VariantName>BuildConfig
compile<VariantName>Java
....
....
....
27. Variant API
Source code
is your documentation!
› Access to most variant's tasks
› Variant output related properties
› Different for apps & libraries
28. The image cannot be displayed. Your computer may not have
enough memory to open the image, or the image may have
been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the
image and then insert it again.
Hello, Gradle plugin!
The first steps
29. The very basic one
src/main/groovy/com/example/gradle/PlaceholderPlugin.gradle
public class PlaceholderPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.task('hello') << {
println "Hello Gradle plugin!"
}
}
}
30. Bind plugin class to plugin name
src/main/resources/META-INF/gradle-plugins/placeholder.properties
implementation-class=com.example.gradle.PlaceholderPlugin
35. The image cannot be displayed. Your computer may not have
enough memory to open the image, or the image may have
been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the
image and then insert it again.
Extending
The New Build System
Let's do it!
36. Check for New Build System
// PlaceholderPlugin.groovy
@Override
void apply(Project project) {
if (project.hasProperty("android")) {
PlaceholderExtension extension = project.extensions.create(
"placeholder", PlaceholderExtension
);
def android = project.android
// all code goes here
}
}
37. Let the New Build System do its job
// PlaceholderPlugin.apply()
if (project.hasProperty("android")) {
PlaceholderExtension extension = project.extensions.create(
"placeholder", PlaceholderExtension
);
def android = project.android
project.afterEvaluate {
// at this point we have all
// tasks from New Build System
}
}
38. Process every variant
// PlaceholderPlugin.apply()
project.afterEvaluate {
if (android.hasProperty('applicationVariants')) {
android.applicationVariants.all { variant ->
addActions(project, variant, extension)
}
} else if (android.hasProperty('libraryVariants')) {
android.libraryVariants.all { variant ->
addActions(project, variant, extension)
}
}
}
42. Does it really work?
>> gradle assembleDebug
:app:preBuild
...
:app:generateDebugBuildConfig
...
:app:processDebugPlaceholders
I will replace debug!
:app:compileDebugJava
...
49. The image cannot be displayed. Your computer may not have
enough memory to open the image, or the image may have
been corrupted. Restart your computer, and then open the file
again. If the red x still appears, you may have to delete the
image and then insert it again.
To summarize
50. Key steps
› Create Gradle plugin
› Inside afterEvaluate { }
› Process every variant
› Add action to generateBuildConfig
› Handle inputs / outputs
51. What's next?
› Default values support
› Errors handling
› Publish ( jcenter / mavenCentral / other )
52. Links
Gradle
http://www.gradle.org/
The New Build System
http://tools.android.com/tech-docs/new-build-system
http://tools.android.com/tech-docs/new-build-system/build-workflow
Github sample
https://github.com/roottony/android-placeholder-plugin
53. Thank you for your attention!
anton.rutkevich@gmail.com
Anton Rutkevich
Senior software engineer
antonrut@yandex-team.ru