build.gradle.ktsの内容を モジュール間で共有化する

build.gradle.ktsの内容を モジュール間で共有化する

2e43817e39d87da5d9d4efd6310c0d44?s=128

Takafumi Kanda

April 12, 2019
Tweet

Transcript

  1. build.gradle.ktsͷ಺༰Λ ϞδϡʔϧؒͰڞ༗Խ͢Δ @kafumi

  2. ࣗݾ঺հ • ਆా ਸ࢙ (͔Μͩ ͔ͨ;Έ) • Twitter: @kafumi__ •

    GitHub: @kafumi • ೔࢈ࣗಈंגࣜձࣾ @ த໨ࠇ • όοΫΤϯυɾiOSɾAndroidΤϯδχΞઈࢍืूத
  3. ࠓ೔ͷ࿩ • ϚϧνϞδϡʔϧϓϩδΣΫτͰ • GradleεΫϦϓτͷهड़ʹKotlin DSLΛ࢖͍ͬͯΔ৔߹ʹ • GradleεΫϦϓτͷ಺༰ΛϞδϡʔϧؒͰͲ͏΍ͬͯ
 ڞ௨Խ͢Δ͔

  4. αϯϓϧίʔυ • https://github.com/kafumi/android-kotlin-gradle-sample • ํ๏1: Kotlin֦ுؔ਺Λ࢖͏ํ๏
 kotlin-extϒϥϯν • ํ๏2: precompiled

    script pluginΛ࢖͏ํ๏ precompiled-scriptϒϥϯν
  5. ૝ఆ͢ΔϓϩδΣΫτ :app :featureA :featureB :data :model • τοϓϨϕϧͷΞϓϦϞδϡʔϧ • ApplicationΫϥε΍ɺDIͷઃఆͳͲΛ͢Δ

    • ػೳ͝ͱʹ෼ׂ͞ΕͨػೳϞδϡʔϧ • ͍ΘΏΔPresentation૚ͷΫϥεؚ͕·ΕΔ • σʔλI/OΛ࣮૷͢Δ • ϞδϡʔϧؒͰڞ༗͍ͨ͠σʔλΫϥεΛ
 ఆٛ͢Δ
  6. ૝ఆ͢ΔϓϩδΣΫτ :app :featureA :featureB :data :model • τοϓϨϕϧͷΞϓϦϞδϡʔϧ • ApplicationΫϥε΍ɺDIͷઃఆͳͲΛ͢Δ

    • ػೳ͝ͱʹ෼ׂ͞ΕͨػೳϞδϡʔϧ • ͍ΘΏΔPresentation૚ͷΫϥεؚ͕·ΕΔ • σʔλI/OΛ࣮૷͢Δ • ϞδϡʔϧؒͰڞ༗͍ͨ͠σʔλΫϥεΛ
 ఆٛ͢Δ
  7. buildSrcΛ࢖͏ • buildSrc • ࣗಈతʹίϯύΠϧ͞Εͯɺશ ϞδϡʔϧͷGradleεΫϦϓτ ͷґଘઌͱͯ͠࢖ΘΕΔ • ͭ·ΓɺbuildSrc಺Ͱఆٛͨ͠ ΋ͷ͸ɺશϞδϡʔϧͷGradle

    εΫϦϓτ͔Β࢖͑Δ
  8. ྫ: DependencyఆٛΛ
 ఆ਺ͱͯ͠ڞ௨Խ͢Δ object Versions { !// … 
 !//

    Google libraries const val appCompat = "1.0.2" const val constraintLayout = "1.1.3" const val lifecycle = "2.0.0" !// 3rd-party libraries const val timber = "4.7.1" } object Deps { !// … object AndroidX { const val appCompat = "androidx.appcompat:appcompat:${Versions.appCompat}" const val constraintLayout = "androidx.constraintlayout:constraintlayout:$ {Versions.constraintLayout}" const val lifecycle = "androidx.lifecycle:lifecycle-extensions:${Versions.lifecycle}" } !// … } buildSrc/src/main/kotlin/Dependencies.kt
  9. ྫ: DependencyఆٛΛ
 ఆ਺ͱͯ͠ڞ௨Խ͢Δ dependencies { implementation(project(":featureA")) !// Kotlin implementation(Deps.Kotlin.stdlibJdk) !//

    Debug utilities implementation(Deps.Debug.timber) } app/build.gradle.kts featureA/build.gradle.kts dependencies { implementation(project(":data")) implementation(project(":model")) !// Kotlin implementation(Deps.Kotlin.stdlibJdk) !// AndroidX implementation(Deps.AndroidX.appCompat) implementation(Deps.AndroidX.constraintLayout) implementation(Deps.AndroidX.lifecycle) !// Debug utilities implementation(Deps.Debug.timber) }
  10. None
  11. ΋ͬͱڞ௨Խ͍ͨ͠

  12. buildSrcΛ΋ͬͱ࢖͏ • ํ๏1: ڞ༗෦෼ΛbuildSrcʹ֦ுؔ਺ͱͯ͠ఆٛ͢Δ • ํ๏2: ڞ༗෦෼ΛPrecompiled script pluginͱͯ͠ఆٛ͢ Δ

  13. ํ๏1: ֦ுؔ਺ https://inside.pixiv.blog/makun/6409

  14. ํ๏1: ֦ுؔ਺ fun KotlinBuildScript.applyAndroidLibrary() { androidLibrary { compileSdkVersion(Versions.compileSdk) buildToolsVersion(Versions.buildTools) defaultConfig

    { minSdkVersion(Versions.minSdk) targetSdkVersion(Versions.targetSdk) } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } buildTypes { getByName("release") { isMinifyEnabled = false consumerProguardFile(file("proguard-rules.pro")) } } libraryVariants.all { generateBuildConfigProvider!?.configure { isEnabled = false } } } buildSrc/src/main/kotlin/AndroidConfigurations.kt GradleεΫϦϓτ͸ KotlinBuildScriptΫϥεͷϝ ιουͱ࣮ͯ͠ߦ͞Ε͍ͯΔ ͷͰɺಉΫϥεͷ֦ுؔ਺ͱ ͯ͠ڞ༗෦෼Λఆٛ
  15. ํ๏1: ֦ுؔ਺ fun KotlinBuildScript.applyAndroidLibrary() { androidLibrary { compileSdkVersion(Versions.compileSdk) buildToolsVersion(Versions.buildTools) defaultConfig

    { minSdkVersion(Versions.minSdk) targetSdkVersion(Versions.targetSdk) } compileOptions { … buildSrc/src/main/kotlin/AndroidConfigurations.kt ϓϥάΠϯ͕ఏڙ͢ΔΫϥε ΁ͷΞΫηα͕ࣗಈੜ੒͞Ε ͳ͍ͷͰɺࣗ෼Ͱఆٛ͢Δඞ ཁ͕͋Δ internal fun Project.android(configure: BaseAppModuleExtension.() !-> Unit): Unit = (this as ExtensionAware).extensions.configure("android", configure) internal fun Project.androidLibrary(configure: LibraryExtension.() !-> Unit): Unit = (this as ExtensionAware).extensions.configure("android", configure) internal fun Project.kotlin(configure: KotlinProjectExtension.() !-> Unit): Unit = (this as ExtensionAware).extensions.configure("kotlin", configure) buildSrc/src/main/kotlin/Accessors.kt
  16. ڞ༗෦෼Λ applyAndroidLibrary() ݺͼग़͠ʹஔ͖׵͑ΒΕΔ

  17. ࠩ෼͕͋Δͱ͜Ζ͚ͩɺ֤ϞδϡʔϧͷεΫϦϓτʹॻ͚͹Α͘ͳΔ

  18. ࠩ෼͕͋Δͱ͜Ζ͚ͩɺ֤ϞδϡʔϧͷεΫϦϓτʹॻ͚͹Α͘ͳΔ plugins෦෼΋
 ڞ༗ԽͰ͖ͳ͍͔

  19. ํ๏2: Precompiled script plugin • buildSrc಺ͷ *.gradle.kts ֦ுࢠͷϑΝΠϧ͸ɺϓϥάΠ ϯͱͯ͠ίϯύΠϧ͞ΕΔ •

    ௨ৗͷ build.gradle.kts ͱಉ͡ه๏͕࢖͑Δ • plugins (ଞͷϓϥάΠϯͷ apply) ΋ఆٛͰ͖Δ • Gradle 5.3͔Β (͖ͬ͞࿩ͨ͠Α͏ͳ) ΞΫηα͕ࣗಈੜ੒ ͞ΕΔΑ͏ʹͳͬͨʂ
  20. ํ๏2: Precompiled script plugin import org.gradle.api.JavaVersion plugins { id("com.android.library") id("kotlin-android")

    } android { compileSdkVersion(Versions.compileSdk) buildToolsVersion(Versions.buildTools) defaultConfig { minSdkVersion(Versions.minSdk) targetSdkVersion(Versions.targetSdk) } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } buildTypes { getByName("release") { isMinifyEnabled = false consumerProguardFile(file("proguard-rules.pro")) } } ↗ ʹଓ͘ ↙ ͔Βଓ͘ libraryVariants.all { generateBuildConfigProvider!?.configure { isEnabled = false } } } kotlin { sourceSets.all { languageSettings.progressiveMode = true } } buildSrc/src/main/kotlin/
 android-library.gradle.kts
  21. ڞ༗෦෼Λ `android-library` ϓϥάΠϯͷ apply ʹஔ͖׵͑ΒΕΔ

  22. ·ͱΊ • ϞδϡʔϧؒͰbuild.gradle.ktsͷ಺༰Λڞ௨Խ͢Δʹ͸ओ ʹ2ͭͷํ๏͕͋Δ • ํ๏1: ڞ༗෦෼ΛbuildSrcʹ֦ுؔ਺ͱͯ͠ఆٛ͢Δ • ํ๏2: ڞ༗෦෼ΛPrecompiled

    script pluginͱͯ͠ఆٛ ͢Δ • Gradle 5.3Ҏ߱Ͱ͋Ε͹ɺํ๏2ͷ΄͏͕Αͦ͞͏