Share
Embed
Kotlinify your Gradle
Presentation given at Android Makers Paris 2018 https://androidmakers.fr/schedule/#session-45
Alexander Gherschon
April 23, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
121 slides
-
WHO AM I? • Android Developer @ Houzz • Organiser
and Speaker @ KotlinTLV.co.il • Love to build tools! -
WHAT IS GRADLE • Build system (JVM based) • Defines
Tasks inside a Project • A Plugin is set of Tasks gathered for a purpose • Java, Kotlin, Android, iOS, .Net… -
INTERESTING EXISTING PLUGINS • Android Gradle Plugin • Kotlin Gradle
Plugin • Gradle Android Ribbonizer plugin • Gradle Play Publisher • etc… -
BUILDING A GRADLE PLUGIN • Print all “// TODO” lines
in our project • Find all source files • Find all lines starting with “// TODO” • Android projects (for each build variant) -
USING GRADLE PLUGINS buildscript { ext.kotlin_version = '1.2.21' repositories {
google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” } } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' -
buildscript { ext.kv = '1.2.21' repositories { google() jcenter() }
dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kv" } } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' USING GRADLE PLUGINS -
buildscript { ext.kv = '1.2.21' repositories { google() jcenter() }
dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kv" } } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' USING GRADLE PLUGINS -
buildscript { ext.kv = '1.2.21' repositories { google() jcenter() }
dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kv" } } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' USING GRADLE PLUGINS -
GRADLE API INTERFACE<PROJECT> •getDependencies() •getTasks() •… INTERFACE<TASK> •doFirst() •doLast() •…
INTERFACE<REPOSITORY> •mavenCentral() •… -
ADDING DEPENDENCIES ./build.gradle repositories { jcenter() google() } dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" testCompile group: 'junit', name: 'junit', version: '4.12' compile gradleApi() compileOnly 'com.android.tools.build:gradle:3.0.1' } -
ADDING DEPENDENCIES ./build.gradle repositories { jcenter() google() } dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" testCompile group: 'junit', name: 'junit', version: '4.12' compile gradleApi() compileOnly 'com.android.tools.build:gradle:3.0.1' } -
ADDING DEPENDENCIES ./build.gradle repositories { jcenter() google() } dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" testCompile group: 'junit', name: 'junit', version: '4.12' compile gradleApi() compileOnly 'com.android.tools.build:gradle:3.0.1' } -
ADDING DEPENDENCIES ./build.gradle repositories { jcenter() google() } dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" testCompile group: 'junit', name: 'junit', version: '4.12' compile gradleApi() compileOnly 'com.android.tools.build:gradle:3.0.1' } -
NAMING OUR PLUGIN ./build.gradle ext { pluginGroupId = 'il.co.galex.tools.build' pluginArtifactId
= 'todos' pluginVersion = '0.0.1' } -
DEPLOYING LOCALLY ./build.gradle apply plugin: 'maven-publish' publishing { publications {
TodosPublication(MavenPublication) { groupId = pluginGroupId artifactId = pluginArtifactId version = pluginVersion from components.java } } } -
DEPENDING & APPLYING ./build.gradle buildscript { repositories { mavenLocal() }
dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "il.co.galex.tools.build:todos:0.0.1" } } apply plugin: 'il.co.galex.todos' ./app/build.gradle -
DEPENDING & APPLYING ./build.gradle buildscript { repositories { mavenLocal() }
dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "il.co.galex.tools.build:todos:0.0.1" } } apply plugin: 'il.co.galex.todos' ./app/build.gradle -
GRADLE PLUGIN ENTRY POINT ./src/…/META-INF/gradle-plugins/il.co.galex.todos.properties implementation-class=il.co.galex.tools.todos.plugin.TodosPlugin /src/main/kotlin/…/il.co.galex.tools.todos.plugin.TodosPlugin class TodosPlugin: Plugin<Project>
{ override fun apply(project: Project) { TODO("We will implement here our plugin!") } } -
IMPLEMENTING OUR PLUGIN class TodosPlugin: Plugin<Project> { override fun apply(project:
Project?) { TODO("We will implement here our plugin!") } } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { val hasApp
= project.plugins.hasPlugin(AppPlugin::class.java) val hasLib = project.plugins.hasPlugin(LibraryPlugin::class.java) if (!hasApp && !hasLib) { throw IllegalStateException(“'some warning here”) } (…) } -
DEPENDENCIES (REMINDER) ./build.gradle repositories { jcenter() google() } dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" testCompile group: 'junit', name: 'junit', version: '4.12' compile gradleApi() compileOnly 'com.android.tools.build:gradle:3.0.1' } -
DEPENDENCIES (REMINDER) ./build.gradle repositories { jcenter() google() } dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" testCompile group: 'junit', name: 'junit', version: '4.12' compile gradleApi() compileOnly 'com.android.tools.build:gradle:3.0.1' } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { val hasApp
= project.plugins.hasPlugin(AppPlugin::class.java) val hasLib = project.plugins.hasPlugin(LibraryPlugin::class.java) if (!hasApp && !hasLib) { throw IllegalStateException(“'some warning here”) } (…) } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { val hasApp
= project.plugins.hasPlugin(AppPlugin::class.java) val hasLib = project.plugins.hasPlugin(LibraryPlugin::class.java) if (!hasApp && !hasLib) { throw IllegalStateException(“'some warning here”) } (…) } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { val hasApp
= project.plugins.hasPlugin(AppPlugin::class.java) val hasLib = project.plugins.hasPlugin(LibraryPlugin::class.java) if (!hasApp && !hasLib) { throw IllegalStateException(“'some warning here”) } (…) } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { (…) val
variants: DomainObjectSet<out BaseVariant> = if (hasApp) { project.extensions.findByType(AppExtension::class.java)!!.applicationVariants } else { project.extensions.findByType(LibraryExtension::class.java)!!.libraryVariants } (…) } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { (…) val
variants: DomainObjectSet<out BaseVariant> = if (hasApp) { project.extensions.findByType(AppExtension::class.java)!!.applicationVariants } else { project.extensions.findByType(LibraryExtension::class.java)!!.libraryVariants } (…) } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { val variants:
DomainObjectSet<out BaseVariant> = (…) variants.all { variant: BaseVariant -> variant.sourceSets.flatMap { it.javaDirectories } .forEach { println(it) } } } -
IMPLEMENTING OUR PLUGIN override fun apply(project: Project) { val variants:
DomainObjectSet<out BaseVariant> = (…) variants.all { variant: BaseVariant -> variant.sourceSets.flatMap { it.javaDirectories } .forEach { println(it) } } } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
IMPLEMENTING OUR PLUGIN variants.all { variant: BaseVariant -> val folders
= variant.sourceSets.flatMap { it.javaDirectories } val taskName = "print${variant.name.capitalize()}Todos" val task = project.tasks.create(taskName, TodosTask::class.java) { it.folders = folders } task.group = "Todos" task.description = "Find all todos in the project for variant ${variant.name}" } -
THE TODOS TASK open class TodosTask : DefaultTask() { var
folders: List<File>? = null } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
THE TODOS TASK open class TodosTask : DefaultTask() { @TaskAction
fun run() { folders?.forEach { folder -> folder.walk().filter { it.isFile }.forEach { file -> file.readLines().forEachIndexed { index, line -> if (line.contains("// TODO")) { println("$file:${index + 1} - ${line.trim()}") } } } } } } -
WRITING OUR EXTENSION open class TodosExtension { var keywords: List<String>?
= null set(value) { when { value == null -> throw IllegalArgumentException("...") value.isEmpty() -> throw IllegalArgumentException("...") value.find { it == "" } != null -> throw IllegalArgumentException("...") else -> field = value } } } -
WRITING OUR EXTENSION open class TodosExtension { var keywords: List<String>?
= null set(value) { when { value == null -> throw IllegalArgumentException("...") value.isEmpty() -> throw IllegalArgumentException("...") value.find { it == "" } != null -> throw IllegalArgumentException("...") else -> field = value } } } -
WRITING OUR EXTENSION open class TodosExtension { var keywords: List<String>?
= null set(value) { when { value == null -> throw IllegalArgumentException("...") value.isEmpty() -> throw IllegalArgumentException("...") value.find { it == "" } != null -> throw IllegalArgumentException("...") else -> field = value } } } -
USING OUR EXTENSION IN OUR PLUGIN class TodosPlugin : Plugin<Project>
{ override fun apply(project: Project) { project.extensions.create("todos", TodosExtension::class.java) } } -
USING OUR EXTENSION IN OUR PLUGIN class TodosPlugin : Plugin<Project>
{ override fun apply(project: Project) { project.extensions.create("todos", TodosExtension::class.java) } } -
USING OUR EXTENSION IN OUR TASK val extension: TodosExtension? =
project.extensions.findByType(TodosExtension::class.java) val keywords = extension?.keywords ?: listOf(“TODO") file.readLines().forEachIndexed { index, line -> if (line.trim().startsWith("//") && keywords.intersect(line.split(" ")).isNotEmpty()) { println("$file:${index + 1} - ${line.trim()}") } } -
USING OUR EXTENSION IN OUR TASK val extension: TodosExtension? =
project.extensions.findByType(TodosExtension::class.java) val keywords = extension?.keywords ?: listOf(“TODO") file.readLines().forEachIndexed { index, line -> if (line.trim().startsWith("//") && keywords.intersect(line.split(" ")).isNotEmpty()) { println("$file:${index + 1} - ${line.trim()}") } } -
USING OUR EXTENSION IN OUR TASK val extension: TodosExtension? =
project.extensions.findByType(TodosExtension::class.java) val keywords = extension?.keywords ?: listOf(“TODO") file.readLines().forEachIndexed { index, line -> if (line.trim().startsWith("//") && keywords.intersect(line.split(" ")).isNotEmpty()) { println("$file:${index + 1} - ${line.trim()}") } } -
• Built a Gradle Plugin • Created a task per
Android Build Variant • Used an extension for as plugin parameters RECAP -
WHAT IS A DSL • Domain Specific Language • General
purpose Programming Language -
SIMPLE DSL EXAMPLE enum class Color { BLUE, RED, GREEN
} data class Wheels(var number: Int? = null, var color: Color? = null) data class Vehicle(var wheels: Wheels? = null, var engine: String? = null) -
SIMPLE DSL EXAMPLE // imperative way val carImperative = Vehicle(Wheels(2,
Color.RED), "Meh") println(carImperative) // Vehicle(wheels=Wheels(number=2, color=RED), engine=Meh) -
SIMPLE DSL EXAMPLE // declarative way val carDSL = vehicle
{ engine = "V8" wheels { number = 4 color = Color.BLUE } } -
SIMPLE DSL EXAMPLE private fun vehicle(block: Vehicle.() -> Unit) =
Vehicle().apply(block) // declarative way val carDSL = vehicle { engine = "V8" wheels { number = 4 color = Color.BLUE } } SIMPLE DSL EXAMPLE -
SIMPLE DSL EXAMPLE private fun vehicle(block: Vehicle.() -> Unit) =
Vehicle().apply(block) // declarative way val carDSL = vehicle { engine = "V8" wheels { number = 4 color = Color.BLUE } } SIMPLE DSL EXAMPLE -
SIMPLE DSL EXAMPLE // declarative way val carDSL = vehicle
{ this.engine = "V8" wheels { number = 4 color = Color.BLUE } } private fun vehicle(block: Vehicle.() -> Unit) = Vehicle().apply(block) SIMPLE DSL EXAMPLE -
THE APPLY FUNCTION private fun vehicle(block: Vehicle.() -> Unit) =
Vehicle().apply(block) private fun vehicle(block: Vehicle.() -> Unit): Vehicle { val vehicle = Vehicle() vehicle.block() return vehicle } -
APPLY FUNCTION private fun vehicle(block: Vehicle.() -> Unit) = Vehicle().apply(block)
private fun vehicle(block: Vehicle.() -> Unit): Vehicle { val vehicle = Vehicle() vehicle.block() return vehicle } -
SIMPLE DSL EXAMPLE // declarative way val carDSL = vehicle
{ engine = "V8" wheels { number = 4 color = Color.BLUE } } -
SIMPLE DSL EXAMPLE // declarative way val carDSL = vehicle
{ this.engine = "V8" this.wheels { this.number = 4 this.color = Color.BLUE } } -
SIMPLE DSL EXAMPLE private fun Vehicle.wheels(block: Wheels.() -> Unit) {
this.wheels = Wheels().apply(block) } // declarative way val carDSL = vehicle { this.engine = "V8" this.wheels { this.number = 4 this.color = Color.BLUE } } -
SIMPLE DSL EXAMPLE private fun Vehicle.wheels(block: Wheels.() -> Unit) {
this.wheels = Wheels().apply(block) } // declarative way val carDSL = vehicle { this.engine = "V8" this.wheels { this.number = 4 this.color = Color.BLUE } } -
SIMPLE DSL EXAMPLE private fun Vehicle.wheels(block: Wheels.() -> Unit) {
this.wheels = Wheels().apply(block) } // declarative way val carDSL = vehicle { this.engine = "V8" this.wheels { this.number = 4 this.color = Color.BLUE } } -
SIMPLE DSL EXAMPLE private fun Vehicle.wheels((block: Wheels.() -> Unit) {
this.wheels = Wheels().apply(block) } // declarative way val carDSL = vehicle { this.engine = "V8" this.wheels { this.number = 4 this.color = Color.BLUE } } Lambda with Receiver = Lambda, with inside “this” -
GRADLE API INTERFACE<PROJECT> •getDependencies() •getTasks() •… INTERFACE<TASK> •doFirst() •doLast() •…
INTERFACE<REPOSITORY> •mavenCentral() •… -
WHAT IS THE KOTLIN-DSL • Gradle API • Set of
Extensions functions and lambdas with receiver • IntelliJ ‘extension’ which understands .kts build files -
PS: CONVERT TO K IN TWO GIT ‘PHASES’ 1.Rename file
then commit 2.Modify its content then commit and push -
PS: CONVERT TO K IN TWO GIT ‘PHASES’ 1.build.gradle ->
build.gradle.kts + commit 2.Modify build.gradle.kts + commit + push -
CONVERTING OUR PLUGIN - 1 buildscript { ext.kotlin_version = '1.2.31'
repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'java' apply plugin: 'kotlin' apply plugin: 'maven-publish' -
CONVERTING OUR PLUGIN - 1’ plugins { kotlin("jvm") version "1.2.40"
`java-gradle-plugin` `maven-publish` } -
CONVERTING OUR PLUGIN - 3 task sourceJar(type: Jar) { from
sourceSets.main.allJava } task javadocJar(type: Jar, dependsOn: javadoc) { from javadoc.destinationDir } -
CONVERTING OUR PLUGIN - 3’ val sourceJar = task<Jar>("sourceJar") {
from(java.sourceSets["main"].allSource) classifier = "sources" } val javadocJar: Jar = task<Jar>("javadocJar") { dependsOn("javadoc") from(java.docsDir) classifier = "javadoc" } -
CONVERTING OUR PLUGIN - 4 ext { pluginGroupId = 'il.co.galex.tools.build'
pluginArtifactId = 'todos' pluginVersion = '0.0.2' } -
CONVERTING OUR PLUGIN - 4’ ext { set("pluginGroupId", "il.co.galex.tools.build") set("pluginArtifactId",
"todos") set("pluginVersion", "0.0.5") } -
CONVERTING OUR PLUGIN - 5 publishing { publications { TodosPublication(MavenPublication)
{ groupId = pluginGroupId artifactId = pluginArtifactId version = pluginVersion from components.java artifact sourceJar { classifier "sources" } artifact javadocJar { classifier = 'javadoc' } } } } -
CONVERTING OUR PLUGIN - 5’ publishing { publications { create<MavenPublication>("TodosPublication")
{ groupId = ext["pluginGroupId"] as String? artifactId = ext["pluginArtifactId"] as String? version = ext["pluginVersion"] as String? from(components["java"]) artifact(sourceJar) artifact(javadocJar) } } } -
CONVERTING OUR ANDROID APP - 1 android { compileSdkVersion 26
defaultConfig { applicationId "il.co.galex.todosgradleplugintest" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } (...) } -
CONVERTING OUR ANDROID APP - 1’ android { compileSdkVersion(26) defaultConfig
{ applicationId = "il.co.galex.todosgradleplugintest" minSdkVersion(15) targetSdkVersion(26) versionCode = 1 versionName = "1.0" testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner" } (...) } -
CONVERTING OUR ANDROID APP - 2 signingConfigs { debug {
storeFile file("debug.keystore") } } buildTypes { debug { signingConfig signingConfigs.debug minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } -
CONVERTING OUR ANDROID APP - 2’ signingConfigs { getByName("debug") {
storeFile = file("debug.keystore") } } buildTypes { getByName("debug") { signingConfig = signingConfigs.getByName("debug") isMinifyEnabled = false proguardFile(getDefaultProguardFile ("proguard-android.txt")) proguardFile("proguard-rules.pro") } } -
CONVERTING OUR ANDROID APP - 3 sourceSets { main {
java.srcDirs += 'src/main/kotlin' } debug { java.srcDirs += 'src/debug/kotlin' } release { java.srcDirs += 'src/release/kotlin' } } -
CONVERTING OUR ANDROID APP - 3’ sourceSets { getByName("main") {
this.java.srcDir("src/main/kotlin") } getByName("debug") { this.java.srcDir("src/debug/kotlin") } getByName("release") { this.java.srcDir("src/release/kotlin") } } -
CONVERTING OUR ANDROID APP - 4 dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' } -
CONVERTING OUR ANDROID APP - 4’ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") implementation("com.android.support:appcompat-v7:26.1.0")
implementation("com.android.support.constraint:constraint-layout:1.0.2") testImplementation("junit:junit:4.12") androidTestImplementation("com.android.support.test:runner:1.0.1") androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.1") } -
CONVERTING OUR ANDROID APP - 5 todos { keywords =
[“TODO", “WTH"] } -
CONVERTING OUR ANDROID APP - 5 todos { keywords =
listOf("TODO", "WTH") } -
RECAP • Converted from Groovy to Kotlin • Not that
easy to switch • Probably because it’s quite early… v0.1.6.3 • But it’s Kotlin, it’s fun! -
• Todos Gradle Plugin https://github.com/galex/android-todos-gradle-plugin • Android Test App https://github.com/galex/android-todos-test-app
• Some good blogposts on the Kotlin-DSL • https://antonioleiva.com/kotlin-dsl-gradle/ • https://kotlinexpertise.com/gradlekotlindsl/ • Books • Kotlin in Action https://www.manning.com/books/kotlin-in-action • Gradle in Action https://www.manning.com/books/gradle-in-action LINKS