Pro Yearly is on sale from $80 to $50! »

build.gradle.kts

3295d80faee461db1093982361256fe4?s=47 Panini
October 19, 2018

 build.gradle.kts

build.gradleをKotlinに書き換えた話
チラッと導入仕方と罠を紹介します

3295d80faee461db1093982361256fe4?s=128

Panini

October 19, 2018
Tweet

Transcript

  1. build.gradle.kts KotlinConf 2018 報告会

  2. About Me • Matthew Vern / Panini • Twitter: @callipan

    Github: panpanini • Mercari, Inc • Android Engineer (US) • Kotlin可愛い
  3. Summary • build.gradle → build.gradle.kts に書き換えた結果 ◦ autocompleteなどを使える ◦ 書き換えるところでちょっと苦労する

    • 99% 問題なくうごく
  4. kotlin-dsl

  5. kotlin-dsl • build.gradleをKotlinで書けるplugin ◦ https://github.com/gradle/kotlin-dsl ◦ https://blog.gradle.org/kotlin-meets-gradle

  6. How to • No auto-convert • Almost the same as

    Groovy, so can copy-paste, or write by hand
  7. tips

  8. Tips • buildSrc/src/main/java ◦ 普通のkotlinファイルおける → compileTaskでコンパイルされる ◦ Build.gradle.ktsで使える •

    Dependency managementに良い
  9. buildSrc/Credentials.kt object Credentials { val hogeUsername = System.getProperty("hoge_username") val hogePassword

    = System.getProperty("hoge_password") }
  10. buildSrc/Dependencies.kt object Versions { val supportLibrary = "27.1.0" ... }

    object Dependencies { // support val supportV4 = "com.android.support:support-v4:${Versions.supportLibrary}" }
  11. buildSrc/Dependencies.kt object Dependencies { ... } object DebugDependencies { ...

    } object ReleaseDependencies { ... }
  12. buildSrc/Dependencies.kt object Plugins { val unusedResourceRemover = "com.github.konifar.gradle.unused-resources-remover" }

  13. app/build.gradle.kts plugins { id(Plugins.unusedResourceRemover) } repositories { maven("https://hoge.com/gradle/gradle-release/") { credentials.username

    = Credentials.hogeUsername credentials.password = Credentials.hogePassword } } dependencies { implementation(Dependencies.supportV4) releaseImplementation(ReleaseDependencies.hoge) }
  14. pain points

  15. app/build.gradle buildScript { dependencies { classpath(BuildScriptDependencies.removeResourcesPlugin) } } plugins {

    id(Plugins.unusedResourceRemover) }
  16. app/build.gradle FAILURE: Build failed with an exception. * Where: Build

    file '/app/build.gradle.kts' line: 4 * What went wrong: Script compilation error: Line 4: classpath(BuildScriptDependencies.removeResourcesPlugin) ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public final fun DependencyHandler.classpath(dependencyNotation: Any): Dependency? defined in org.gradle.kotlin.dsl.ScriptHandlerScope
  17. app/build.gradle buildScript { dependencies { classpath(BuildScriptDependencies.removeResourcesPlugin) } } plugins {

    id(Plugins.unusedResourceRemover) }
  18. app/build.gradle buildScript { dependencies { classpath(BuildScriptDependencies.removeResourcesPlugin) } } plugins {

    id(Plugins.unusedResourceRemover) }
  19. app/build.gradle • Gradle: buildscript { } → 追加 • Kotlin:

    buildscript { } → 上書き
  20. app/build.gradle • Gradle: buildscript { } → 追加 • Kotlin:

    buildscript { } → 上書き ↳ buildscriptはroot/build.gradle.ktsで書く
  21. app/build.gradle • Gradle: buildscript { } → 追加 • Kotlin:

    buildscript { } → 上書き ↳ buildscriptはroot/build.gradle.ktsで書く 本当にこれでいいのか分からないので 他にいいアイデアあれば是非聞かせて ください!
  22. app/build.gradle buildscript { classpath(BuildScriptDependencies.fabricPlugin) } plugins { id(Plugins.androidApplication) id(Plugins.fabric) ...

    }
  23. app/build.gradle FAILURE: Build failed with an exception. IllegalStateException: 'android' or

    'android-library' plugin required.
  24. app/build.gradle FAILURE: Build failed with an exception. IllegalStateException: 'android' or

    'android-library' plugin required. plugins { } を使うと同時にapplyされるっぽい
  25. app/build.gradle FAILURE: Build failed with an exception. IllegalStateException: 'android' or

    'android-library' plugin required. plugins { } を使うと同時にapplyされるっぽい この問題gradle-4.10.2で 治ってるっぽい!
  26. まとめ

  27. まとめ • build.gradleはKotlinで書こう! • 動くよ! • そんなに罠がないよ!

  28. Have a nice Kotlin!