From ed2499be3c53c952056f3f8f55340497242d9cf6 Mon Sep 17 00:00:00 2001 From: bqliang Date: Wed, 7 Dec 2022 00:04:24 +0800 Subject: [PATCH] refactor: migrate Gradle Groovy to Gradle Kotlin DSL --- app/build.gradle | 138 ------------------------- app/build.gradle.kts | 111 ++++++++++++++++++++ app/proguard-rules.pro | 2 +- build.gradle | 17 --- build.gradle.kts | 19 ++++ gradle/libs.versions.toml | 67 ++++++++++++ settings.gradle => settings.gradle.kts | 4 +- 7 files changed, 200 insertions(+), 158 deletions(-) delete mode 100644 app/build.gradle create mode 100644 app/build.gradle.kts delete mode 100644 build.gradle create mode 100644 build.gradle.kts create mode 100644 gradle/libs.versions.toml rename settings.gradle => settings.gradle.kts (77%) diff --git a/app/build.gradle b/app/build.gradle deleted file mode 100644 index a656243..0000000 --- a/app/build.gradle +++ /dev/null @@ -1,138 +0,0 @@ -plugins { - id 'com.android.application' - id 'org.jetbrains.kotlin.android' - id 'kotlin-kapt' - id 'kotlinx-serialization' - id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' -} - -def srcDirs = [ - 'main', - 'settings' -] - -android { - signingConfigs { - release { - storeFile file('../key.jks') - storePassword System.getenv("KEY_STORE_PASSWORD") - keyAlias System.getenv("SIGNING_KEY_ALIAS") - keyPassword System.getenv("KEY_PASSWORD") - } - } - namespace 'com.bqliang.leavesheet' - compileSdk 33 - - defaultConfig { - applicationId 'com.bqliang.leavesheet' - minSdk 26 - targetSdk 33 - versionCode rootProject.ext.versionCode - versionName rootProject.ext.versionName - - testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' - - javaCompileOptions { - annotationProcessorOptions { - arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] - } - } - } - - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' - signingConfig signingConfigs.release - } - - debug { - applicationIdSuffix ".debug" - versionNameSuffix ".debug" - } - - applicationVariants.all { variant -> - variant.outputs.all { - outputFileName = "${rootProject.name}-${variant.versionName}-${variant.name}.apk" - } - } - } - - dataBinding { - enabled = true - } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } - kotlinOptions { - jvmTarget = '1.8' - } - - sourceSets { - main { - res.srcDirs = ['src/main/res'] - srcDirs.forEach { - res.srcDirs += 'src/main/java/com/bqliang/leavesheet/' + it + '/res' - } - } - } -} - -dependencies { - // MMKV - implementation 'com.tencent:mmkv:1.2.14' - // RecyclerViewSwipeDecorator - implementation 'com.github.xabaras:RecyclerViewSwipeDecorator:1.4' - // Preference - implementation 'androidx.preference:preference-ktx:1.2.0' - // Timber - implementation 'com.jakewharton.timber:timber:5.0.1' - // swipe refresh layout - implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' - // FancyShowCaseView - implementation 'com.github.faruktoptas:FancyShowCaseView:1.3.9' - // Jetpack Room - implementation 'androidx.room:room-runtime:2.4.3' - // To use Kotlin annotation processing tool (kapt) - kapt 'androidx.room:room-compiler:2.4.3' - // Kotlin Extensions and Coroutines support for Room - implementation 'androidx.room:room-ktx:2.4.3' - // RikkaX Simple Menu - implementation 'dev.rikka.rikkax.preference:simplemenu-preference:1.0.3' - // splashscreen - implementation 'androidx.core:core-splashscreen:1.0.0' - // RikkaX Html - implementation("dev.rikka.rikkax.html:html:1.1.2") - implementation("dev.rikka.rikkax.html:html-ktx:1.1.2") - // App Center - def appCenterSdkVersion = '5.0.0' - implementation "com.microsoft.appcenter:appcenter-analytics:${appCenterSdkVersion}" - implementation "com.microsoft.appcenter:appcenter-crashes:${appCenterSdkVersion}" - implementation "com.microsoft.appcenter:appcenter-distribute:${appCenterSdkVersion}" - - // Preferences DataStore (SharedPreferences like APIs) - implementation 'androidx.datastore:datastore-preferences:1.0.0' - // Typed DataStore (Typed API surface, such as Proto) - implementation 'androidx.datastore:datastore:1.0.0' - implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1' - // Immutable Collections Library for Kotlin - implementation 'org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.5' - // Coroutines - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4' - // ViewModel - implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' - // LiveData - implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1' - // Lifecycles only (without ViewModel or LiveData) - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' - - - implementation 'androidx.core:core-ktx:1.9.0' - implementation 'androidx.appcompat:appcompat:1.5.1' - implementation 'androidx.activity:activity-ktx:1.6.1' - implementation 'androidx.fragment:fragment-ktx:1.5.4' - implementation 'com.google.android.material:material:1.7.0' - implementation 'androidx.constraintlayout:constraintlayout:2.1.4' -} \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..37b2cbc --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,111 @@ +val appVersionCode: Int by rootProject.extra +val appVersionName: String by rootProject.extra + +@Suppress("DSL_SCOPE_VIOLATION") // https://youtrack.jetbrains.com/issue/KTIJ-19369 +plugins { + alias(libs.plugins.android.application) + kotlin("android") // alias(libs.plugins.kotlin.android) + kotlin("kapt") + kotlin("plugin.serialization") // alias(libs.plugins.kotlinx.serialization) + alias(libs.plugins.secrets) // Secrets Gradle Plugin for Android: https://github.com/google/secrets-gradle-plugin +} + +android { + namespace = "com.bqliang.leavesheet" + compileSdk = 33 + + defaultConfig { + applicationId = "com.bqliang.leavesheet" + minSdk = 26 + targetSdk = 33 + versionCode = appVersionCode + versionName = appVersionName + + javaCompileOptions { + annotationProcessorOptions { + argument("room.schemaLocation", "$projectDir/schemas") + } + } + } + + signingConfigs { + signingConfigs.create("release") { + storeFile = file("../key.jks") + storePassword = System.getenv("KEY_STORE_PASSWORD") + keyAlias = System.getenv("SIGNING_KEY_ALIAS") + keyPassword = System.getenv("KEY_PASSWORD") + } + } + + buildTypes { + getByName("debug") { + isMinifyEnabled = false + applicationIdSuffix = ".debug" + versionNameSuffix = ".debug" + } + getByName("release") { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" + ) + signingConfig = signingConfigs.getByName(name) + } + } + + applicationVariants.configureEach { + outputs.configureEach { + (this as? com.android.build.gradle.internal.api.ApkVariantOutputImpl)?.outputFileName = + "${rootProject.name}-${versionName}-${name/* variant name */}.apk" + } + } + + buildFeatures { + dataBinding = true + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + kotlinOptions { + jvmTarget = "11" + } + + sourceSets { + getByName("main") { + res { + val srcDirs = listOf( + "main", + "settings" + ).map { "src/main/java/com/bqliang/leavesheet/$it/res" }.plus("src/main/res") + setSrcDirs(srcDirs) + } + } + } +} + +dependencies { + implementation(libs.tencent.mmkv) + implementation(libs.timber) + implementation(libs.rikkaX.preference.simplemenu) + implementation(libs.bundles.rikkaX.html) + implementation(libs.kotlinX.serialization.json) + implementation(libs.kotlinX.coroutines.android) + implementation(libs.bundles.appCenter) + implementation(libs.xabaras.recyclerViewSwipeDecorator) + implementation(libs.faruktoptas.fancyShowCaseView) + implementation(libs.google.material) + implementation(libs.androidX.core) + implementation(libs.androidX.appCompat) + implementation(libs.androidX.activity) + implementation(libs.androidX.fragment) + implementation(libs.bundles.androidX.lifeCycle) + implementation(libs.androidX.constraintLayout) + implementation(libs.androidX.preference) + implementation(libs.androidX.splashScreen) + implementation(libs.androidX.swipeRefreshLayout) + implementation(libs.bundles.androidX.dataStore) + implementation(libs.bundles.androidX.room) + kapt(libs.androidX.room.compiler) +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 481bb43..ff59496 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,6 +1,6 @@ # Add project specific ProGuard rules here. # You can control the set of applied configuration files using the -# proguardFiles setting in build.gradle. +# proguardFiles setting in build.gradle.kts. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html diff --git a/build.gradle b/build.gradle deleted file mode 100644 index ecb137d..0000000 --- a/build.gradle +++ /dev/null @@ -1,17 +0,0 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. -plugins { - id 'com.android.application' version '7.3.1' apply false - id 'com.android.library' version '7.3.1' apply false - id 'org.jetbrains.kotlin.android' version '1.7.20' apply false - id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.20' -} - -def gitCommitId = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim() -def gitCommitCount = Integer.parseInt('git rev-list --count HEAD'.execute([], project.rootDir).text.trim()) -def gitTag = 'git describe --abbrev=0 --tags'.execute([], project.rootDir).text.trim() -def version = "${if (gitTag.isEmpty()) "v1.0.0" else gitTag}" - -ext { - versionCode = gitCommitCount - versionName = "${version}.r${gitCommitCount}.${gitCommitId}" -} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..2972623 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,19 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +@Suppress("DSL_SCOPE_VIOLATION") // https://youtrack.jetbrains.com/issue/KTIJ-19369 +plugins { + alias(libs.plugins.android.application) apply false + alias(libs.plugins.android.library) apply false + alias(libs.plugins.kotlin.android) apply false + alias(libs.plugins.kotlinx.serialization) apply false +} +extra.apply { + val gitCommitId = ProcessBuilder("git", "rev-parse", "--short", "HEAD").execute() + val gitCommitCount = ProcessBuilder("git", "rev-list", "--count", "HEAD").execute().toInt() + val gitTag = ProcessBuilder("git", "describe", "--tags", "--abbrev=0").execute().let { tag -> + if (tag.isEmpty()) "" else "$tag." + } + set("appVersionCode", gitCommitCount) + set("appVersionName", "${gitTag}r${gitCommitCount}.${gitCommitId}") +} + +fun ProcessBuilder.execute() = this.start().inputStream.bufferedReader().readText().trim() \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..e6e9c18 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,67 @@ +[versions] # used to declare versions which can be referenced by dependencies +agp = "7.3.1" +kotlin = "1.7.21" +androidX-room = "2.4.3" +lifeCycle = "2.5.1" +appCenter = "5.0.0" + + +[plugins] # used to declare plugins +android-application = { id = "com.android.application", version.ref = "agp" } +android-library = { id = "com.android.library", version.ref = "agp" } +kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } +secrets = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:2.0.1" +kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +ksp = { id = "com.google.devtools.ksp", version = "1.7.21-1.0.8" } + + +[libraries] # used to declare the aliases to coordinates +gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "agp" } +gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } + +kotlinX-serialization-json = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1" +kotlinX-coroutines-android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4" + +androidX-core = "androidx.core:core-ktx:1.9.0" +androidX-appCompat = "androidx.appcompat:appcompat:1.5.1" +androidX-constraintLayout = "androidx.constraintlayout:constraintlayout:2.1.4" +androidX-activity = "androidx.activity:activity-ktx:1.6.1" +androidX-fragment = "androidx.fragment:fragment-ktx:1.5.4" +androidX-preference = "androidx.preference:preference-ktx:1.2.0" +androidX-splashScreen = "androidx.core:core-splashscreen:1.0.0" +androidX-swipeRefreshLayout = "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" + +androidX-room-compiler = { module = "androidx.room:room-compiler", version.ref = "androidX-room" } +androidX-room-ktx = { module = "androidx.room:room-ktx", version.ref = "androidX-room" } +androidX-room-runtime = { module = "androidx.room:room-runtime", version.ref = "androidX-room" } + +androidX-lifeCycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifeCycle" } +androidX-lifeCycle-viewModel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifeCycle" } +androidX-lifeCycle-livedata = { module = "androidx.lifecycle:lifecycle-livedata-ktx", version.ref = "lifeCycle" } + +# Typed DataStore (Typed API surface, such as Proto) +androidX-dataStore = "androidx.datastore:datastore:1.0.0" +# Preferences DataStore (SharedPreferences like APIs) +androidX-dataStore-preferences = "androidx.datastore:datastore-preferences:1.0.0" + +google-material = "com.google.android.material:material:1.7.0" +tencent-mmkv = "com.tencent:mmkv:1.2.14" +timber = "com.jakewharton.timber:timber:5.0.1" +xabaras-recyclerViewSwipeDecorator = "com.github.xabaras:RecyclerViewSwipeDecorator:1.4" +faruktoptas-FancyShowCaseView = "com.github.faruktoptas:FancyShowCaseView:1.3.9" + +rikkaX-html = "dev.rikka.rikkax.html:html:1.1.2" +rikkaX-html-ktx = "dev.rikka.rikkax.html:html-ktx:1.1.2" +rikkaX-preference-simplemenu = "dev.rikka.rikkax.preference:simplemenu-preference:1.0.3" + +appCenter-crashes = { module = "com.microsoft.appcenter:appcenter-crashes", version.ref = "appCenter" } +appCenter-analytics = { module = "com.microsoft.appcenter:appcenter-analytics", version.ref = "appCenter" } +appCenter-distribute = { module = "com.microsoft.appcenter:appcenter-distribute", version.ref = "appCenter" } + + +[bundles] # used to declare dependency bundles +androidX-room = ["androidX-room-runtime", "androidX-room-ktx"] +appCenter = ["appCenter-crashes", "appCenter-analytics", "appCenter-distribute"] +rikkaX-html = ["rikkaX-html", "rikkaX-html-ktx"] +androidX-lifeCycle = ["androidX-lifeCycle-runtime", "androidX-lifeCycle-viewModel", "androidX-lifeCycle-livedata"] +androidX-dataStore = ["androidX-dataStore", "androidX-dataStore-preferences"] \ No newline at end of file diff --git a/settings.gradle b/settings.gradle.kts similarity index 77% rename from settings.gradle rename to settings.gradle.kts index 0f12817..69e9328 100644 --- a/settings.gradle +++ b/settings.gradle.kts @@ -10,8 +10,8 @@ dependencyResolutionManagement { repositories { google() mavenCentral() - maven { url 'https://jitpack.io' } //增加 jitPack Maven 仓库 + maven("https://jitpack.io") //增加 jitPack Maven 仓库 } } rootProject.name = "Leave Sheet" -include ':app' +include(":app") \ No newline at end of file