Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

buildSrc/Composite Buildで必要なバージョン情報も Version Ca...

subroh_0508
February 17, 2023

buildSrc/Composite Buildで必要なバージョン情報も Version Catalogから参照したい!

shibuya.apk #40 〜 2023年復活祭〜のLT資料です。

https://shibuya-apk.connpass.com/event/272322/

サンプルアプリ: https://github.com/subroh0508/versioncatalog-sample

subroh_0508

February 17, 2023
Tweet

More Decks by subroh_0508

Other Decks in Technology

Transcript

  1. 7FSTJPO$BUBMPHͱ͸ 📚ϥΠϒϥϦɾϓϥάΠϯͷόʔδϣϯΛҰݩతʹ؅ཧ͢Δ(SBEMFͷػೳ  (SBEMFͰ௥Ճ 'FBUVSF1SFWJFX ɺͰ4UBCMFʹঢ֨  ϓϩδΣΫτ͕ࢀর͢ΔͷϥΠϒϥϦɾϓϥάΠϯͷόʔδϣϯ৘ใΛ 
 ϲॴͰ؅ཧ͢Δ͜ͱ͕Ͱ͖Δ🥳

     ϥΠϒϥϦɾϓϥάΠϯͷόʔδϣϯ৘ใΛܕ҆શͳܗͰࢀরͰ͖Δ🥳  ࿩͢͜ͱόʔδϣϯ؅ཧΛ7FSTJPO$BUBMPHʹҠߦͨ࣌͠ͷ5JQT ಛʹɺCVJME4SD$PNQPTJUF#VJMEͰڞ௨Խͨ͠ϏϧυεΫϦϓτ͔Β 7FSTJPO$BUBMPH؅ཧԼͷόʔδϣϯ৘ใΛͲ͏΍ͬͯࢀরͤ͞Δ͔
  2. 7FSTJPO$BUBMPHʹΑΔόʔδϣϯ؅ཧ  🐘settings.gradle.kts಺ʹόʔδϣϯ৘ใΛྻڍ  dependencyResolutionManagement { versionCatalogs { create("libs") {

    version("agp", "7.3.0") version("kotlin", "1.8.0") version("androidx-core", "1.9.0") plugin("kotlin-android", "org.jetbrains.kotlin.android").versionRef("kotlin") plugin("android-application", "com.android.application").versionRef("agp") library("androidx-core", "androidx.core", "core-ktx").versionRef("androidx-core") } } } TFUUJOHTHSBEMFLUT
  3. 7FSTJPO$BUBMPHʹΑΔόʔδϣϯ؅ཧ  🐘gradle/libs.versions.toml಺ʹόʔδϣϯ৘ใΛྻڍ  [versions] agp = "7.3.0" kotlin =

    "1.8.0" androidx-core = "1.9.0" [plugins] kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } android-application = { id = "com.android.application", version.ref = "agp" } [libraries] androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" } HSBEMFMJCTWFSTJPOTUPNM
  4. 7FSTJPO$BUBMPH͔Βόʔδϣϯ৘ใࢀর 🐘͍ͣΕͷํ๏Ͱ΋ɺ֤gradle.build.kts಺ͰҎԼͷΑ͏ʹࢀরͰ͖Δ  plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) } dependencies {

    implementation(libs.androidx.core) } HSBEMFCVJMELUT org.gradle.api.Project͔Β ֦ுϓϩύςΟlibs͕ੜ͑Δ ͔͜͜Βܕ҆શͳܗͰ όʔδϣϯ৘ใΛࢀরͰ͖Δ🥳
  5. 🐥buildSrc഑ԼʹΦϒδΣΫτΫϥεΛఆٛ͠ɺόʔδϣϯ৘ใΛྻڍ object Libraries { const val agp = "7.3.0" const

    val kotlin = "1.8.0" const val androidxCore = "1.9.0" object Plugins { const val androidApplication = "com.android.tools.build:gradle:$agp" const val kotlinAndroid = "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin" } object AndroidX { const val core = "androidx.core:core-ktx:$androidxCore" } } ͜Ε·Ͱͷόʔδϣϯ৘ใ؅ཧ  CVJME4SDTSDNBJOLPUMJO-JCSBSJFTLU ܕ҆શͳܗͰͷόʔδϣϯ৘ใࢀর͸Մೳ͕ͩʜ🤔
  6. 🐥buildSrc഑ԼʹΦϒδΣΫτΫϥεΛఆٛ͠ɺόʔδϣϯ৘ใΛྻڍ ͜Ε·Ͱͷόʔδϣϯ৘ใ؅ཧ  plugins { `kotlin-dsl` } // 中略 dependencies

    { implementation(Libraries.Plugins.androidApplication) implementation(Libraries.Plugins.kotlinAndroid) } CVJME4SDHSBEMFCVJMELUT buildSrc഑Լͷgradle.build.ktsͰ ఆٛͨ͠ΦϒδΣΫτΫϥε͕ࢀরͰ͖ͳ͍🥺 buildSrc഑ԼʹϏϧυεΫϦϓτΛஔ͍ͯɺϏϧυϩδοΫͷڞ௨Խ͕Ͱ͖Δ ͔͠͠ɺόʔδϣϯ৘ใ͸ΦϒδΣΫτΫϥεΛ࢖ͬͯڞ௨ԽͰ͖ͣɺෆศ🥺 ˠ7FSTJPO$BUBMPHͰղܾՄೳʂ💪
  7. ⚒buildSrc$PNQPTJUF#VJME͍ͣΕͷ৔߹΋ར༻Մೳ 7FSTJPO$BUBMPH ϏϧυϩδοΫڞ௨Խ  pluginManagement { repositories { // 中略

    includeBuild("plugins") } } TFUUJOHTHSBEMFLUT buildSrcར༻࣌ $PNQPTJUF#VJMEར༻࣌ ڞ௨Խͨ͠ ϏϧυϩδοΫ
  8. ⚒  settings.gradle.ktsʹUPNMϑΝΠϧͷಡΈࠐΈॲཧΛ௥ه dependencyResolutionManagement { repositories { mavenCentral() } versionCatalogs

    { create("libs") { from(files("../gradle/libs.versions.toml")) } } } 7FSTJPO$BUBMPH ϏϧυϩδοΫڞ௨Խ  CVJME4SDcQMVHJOT TFUUJOHTHSBEMFLUT
  9. ⚒  7FSTJPO$BUBMPH͔Βόʔδϣϯ৘ใΛऔಘ͢Δ֦ுؔ਺Λ௥Ճ internal val VersionCatalog.androidxCore get() = getLibrary("androidx-core") internal

    val Project.libs: VersionCatalog get() = extensions.getByType<VersionCatalogsExtension>().named("libs") private fun VersionCatalog.getLibrary( library: String, ) = findLibrary(library).get() 7FSTJPO$BUBMPH ϏϧυϩδοΫڞ௨Խ  CVJME4SDcQMVHJOT FYUFOTJPOT7FSTJPO$BUBMPH&YUFOTJPOLU ֦ுϓϩύςΟlibsͱόʔδϣϯ৘ใऔಘ༻ͷΫϥεɾϓϩύςΟ͕ ࣗಈੜ੒͞Εͳ͍ͨΊɺࣗྗͰ༻ҙ͢ΔඞཁΞϦ🧐
  10. 🛠$PNQPTFGPS.11ͷґଘؔ܎Λڞ௨Խ͢ΔΑ͏ͳϏϧυϩδοΫ  plugins { id("com.android.library") id("org.jetbrains.compose") kotlin("multiplatform") } kotlin {

    jvm("desktop") android() sourceSets { commonMain { dependencies { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material3) } } named("androidMain") { dependencies { implementation(libs.androidxCore) } } } } CVJME4SDcQMVHJOT DPNQPTFDPNNPOHSBEMFLUT CVJME4SDcQMVHJOT CVJMEHSBEMFLUT͔Β όʔδϣϯ৘ใΛࢀর
  11. 🛠$PNQPTFGPS.11ͷґଘؔ܎Λڞ௨Խ͢ΔΑ͏ͳϏϧυϩδοΫ  plugins { id("com.android.library") id("org.jetbrains.compose") kotlin("multiplatform") } kotlin {

    jvm("desktop") android() sourceSets { commonMain { dependencies { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material3) } } named("androidMain") { dependencies { implementation(libs.androidxCore) } } } } CVJME4SDcQMVHJOT DPNQPTFDPNNPOHSBEMFLUT plugins { id("compose-common") } DPNNPOCVJMEHSBELFLUT $PNQPTFGPS.11ͱBOESPJEYDPSFLUY΁ͷ ґଘΛQMVHJOTͷ௥Ճ͚ͩͰఆٛͰ͖Δʂ🥳 ܁Γฦ͠ॻ͖͕ͪͳґଘؔ܎Λ ͭͷϑΝΠϧʹ·ͱΊͯ؅ཧͰ͖Δ🥰 ͦͷ্ߋ৽νΣοΫ΋ࣗಈͰͰ͖Δ🥰 %BHHFS 
 $PSPVUJOFT 
 ࣗಈςετؔ࿈FUD $PNQPTFGPS.11 BOESPJEYDPSFLUY