Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's migrate to build.gradle.kts

Let's migrate to build.gradle.kts

A step-by-step guide of how to migrate to Gradle Kotlin DSL a.k.a. build.gradle.kts in a reproducutive way.

Video available at https://youtu.be/_s-0MbA5Gvw

Presented at DroidKaigi 2019 at Bellesalle Shinjuku Grand Conference Center
https://droidkaigi.jp/2019/en/timetable/70863

Yuki Fujisaki

February 08, 2019
Tweet

More Decks by Yuki Fujisaki

Other Decks in Programming

Transcript

  1. ؀ڥ  "OESPJE4UVEJP  "OESPJE(SBEMF1MVHJO  (SBEMF (SBEMF,PUMJO%4-3$  ݱ࣌఺Ͱ࠷৽͸3$

    (SBEMF,PUMJO%4-  ࢖ͬͯΔϓϥάΠϯ͕(SBEMFܥରԠͯ͠Ε͹࠷৽͕Φεεϝ ࡢ೔  ϦϦʔε
  2. ͦ΋ͦ΋extͬͯ  ΞυϗοΫʹ޷͖ͳ஋ΛಥͬࠐΊΔ,FZ7BMVF4UPSF  Project ˺build.gradle ͕ࣗಈతʹ͍࣋ͬͯΔ  extʹηοτͨ͠ޙ͸ProjectͷείʔϓͰ΋ࢀরՄೳ 

    rootProjectͰηοτͨ͠஋͸͢΂ͯͷࢠProject͔Β ΋ݟ͑Δ ࣮ߦॱংͷؔ܎্buildscript {}಺Ͱఆٛ͢Δඞཁ͕͋Δ
  3. ಁաੑ buildscript { ext.kotlin_version = "1.3.20" } // ext είʔϓͰ΋ɺ

    Project είʔϓͰ΋ࢀরͰ͖Δ println ext.kotlin_version // => "1.3.20" println kotlin_version // => "1.3.20" SPPU1SPKFDUͰఆٛͨ͠஋͸ଞͷ1SPKFDU͔Β΋ࢀরͰ͖Δ // app/build.gradle println kotlin_version // => "1.3.20" println rootProject.kotlin_version // => "1.3.20"
  4. ࣍ͷนtasksͷमਖ਼ taskͷఆٛͷߏจมߋ // Groovy task clean(type: Delete) {} ɹɹɹɹɹɹ‑ //

    Kotlin tasks.create("clean", type = Delete::class) {} // or task<Delete>("clean") {}
  5. ͜Ε͸୅ೖ ϝιουݺͼग़͠ applicationId "sh.nothing.myapplication" minSdkVersion 21 targetSdkVersion 28 versionCode 1

    versionName "1.0"  ϝιουݺͼग़͠ͷ()ΛলུͰ͖Δ  ϓϩύςΟ୅ೖͷ=ΛলུͰ͖Δ ‎ݟͨ໨ͷ۠ผ͕෇͔ͳ͍ʂ
  6. apply plugin:͔Βplugins {}ʹҠߦ // apply plugin: ܈Λ apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' ɹɹɹɹɹɹ‑ // plugins ߏจʹॻ͖׵͑Δ plugins { id("com.android.application") id("kotlin-android") id("kotlin-android-extensions") }
  7. kotlin_version  ͖ͬ͞΍Γ·ͨ͠ͶrootProjectͷext͸ʜ // ͢΂ͯͷ Project είʔϓͰݟ͑Δ val kotlin_version: String

    by project // ΋͘͠͸ `rootProject.extra` ͔Βऔಘͯ͠΋͍͍Ͱ͢ val kotlin_version: String by rootProject.extra
  8. ੾Γग़͢ͱ͖ͷ੍໿  buildscript {} ͸੾Γग़ͤͳ͍  ୯७ʹJODMVEFͯ͠ΔΘ͚Ͱͳ͘ϓϥάΠϯѻ͍  ϑΝΠϧ͝ͱʹผͷDMBTTMPBEFSʹͳΔ 

    it instanceOf AppPlugin తͳ൑ఆ͕ਖ਼͘͠ಈ࡞͠ͳ͍  hasPlugin("com.android.application") ͸0,
  9. idએݴʹapply falseΛ෇༩  apply plugin:͸ݩͷҐஔʹஔ͍ͨ·· plugins { // Groovy id

    "com.google.gms.google-services" apply false } plugins { // Kotlin id("com.google.gms.google-services") apply false }
  10. sourceSets {}ฤ sourceSets { // Groovy main.java.srcDirs += 'src/main/kotlin' }

    ɹɹɹɹɹɹɹɹ‑ sourceSets { // Kotlin getByName("main").java.srcDirs("src/main/kotlin") }  (SPPWZ΋·ͬͨ͘ಉ͡ίʔυ͕௨Δ
  11. ಥવLBQUౖ͕Γग़͢ ݱ৅4ZOD͸Ͱ͖͕ͨɺΞϓϦΛϏϧυ͢ΔͱLBQUౖ͕Γग़͠ ͯϏϧυ͕௨Βͳ͍ error: incompatible types: NonExistentClass cannot be converted

    to Annotation @error.NonExistentClass() ^ ݪҼandroid-extensionsͷઃఆ͕൓ө͞Εͳ͍  isExperimental = trueͬͯॻ͍ͯΔͷʹ൓ө͞Εͯͳ͍