我的项目有两个不同的build.gradle文件,用时髦的语法编写。我想将这个时髦的书面 gradle 文件更改为使用 Kotlin 语法 (build.gradle.kts) 编写的 gradle 文件。
我将向您展示根项目 build.gradle 文件。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
//ext.kotlin_version = '1.2-M2'
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0-alpha01'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我尝试了在互联网上找到的几种"方法",但没有任何效果。重命名文件(显然不是解决方案)没有帮助。我在根项目中创建了一个新的build.gradle.kts文件,但该文件未显示在我的项目中。格拉德尔也没有识别新文件。
所以我的问题是:如何将我的 build.gradle 文件转换为 kotlin build.gradle.kts 并将这个新文件添加到我现有的项目中?
感谢您的帮助。
当然重命名无济于事。您需要使用 Kotlin DSL 重写它。它类似于Groovy,但有一些不同。阅读他们的文档,查看示例。
在您的情况下,问题是:
-
ext.kotlin_version
不是有效的 Kotlin 语法,请使用方括号 - 所有 Kotlin 字符串都使用双引号
- 大多数函数调用的参数周围都需要大括号(也有例外,如中缀函数)
- Slighlty 不同的任务管理 API。有不同的样式可用。您可以将块中的所有任务声明为字符串
tasks
或使用单个类型化函数,如下例所示。
看看转换后的顶级build.gradle.kts
:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext["kotlin_version"] = "1.1.51"
repositories {
google()
jcenter()
}
dependencies {
classpath ("com.android.tools.build:gradle:3.1.0-alpha01")
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:${ext["kotlin_version"]}")
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task<Delete>("clean") {
delete(rootProject.buildDir)
}
buildscript {
extra.apply {
var kotlin_version = "1.7.10"
var navigationVersion = "2.5.0"
var hilt_version = "2.42"
}
// .kotlin_version("1.7.10")
// ext.navigationVersion("2.5.0")
// ext.hilt_version("2.42")
repositories {
gradlePluginPortal()
google()
mavenCentral()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:7.2.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.9.1")
classpath("com.google.gms:google-services:4.3.13")
classpath("com.google.firebase:perf-plugin:1.4.1")
classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// classpath("com.google.dagger:hilt-android-gradle-plugin:$extra.hilt_version")
}
}
allprojects {
repositories {
gradlePluginPortal()
mavenCentral()
google()
jcenter()
maven { url = uri("https://jitpack.io") }
maven { url = uri("https://cdn.veriff.me/android/") }
flatDir {
dirs("libs")
}
}
}
tasks.register("clean"){
delete(setOf(rootProject.buildDir))
}
import extensions.*
import java.time.LocalDate
import java.time.format.DateTimeFormatter.*
plugins {
id(Plugins.ANDROID_APPLICATION)
id(Plugins.ANDROID)
id(Plugins.PARCELIZE)
id(Plugins.KAPT)
id(Plugins.NAVIGATION_SAFE_ARGS)
id(Plugins.GOOGLE_SERVICES)
id(Plugins.CRASH_ANALYTICS)
id(Plugins.PERFORMANCE_ANALYTICS)
id(Plugins.DAGGER_HILT)
}
var applicationName = "abc`enter code here`"
android {
compileSdk = AndroidConfig.COMPILE_SDK
bundle {
language {
enableSplit = false
}
}
defaultConfig {
configurations.all {
resolutionStrategy { force(AndroidConfig.FORCE_STRATEGY) }
}
applicationId = AndroidConfig.APPLICATION_ID
minSdkVersion(AndroidConfig.MIN_SDK)
targetSdkVersion(AndroidConfig.TARGET_SDK)
versionCode = AndroidConfig.VERSION_CODE
versionName = AndroidConfig.VERSION_NAME
testInstrumentationRunner = AndroidConfig.TEST_INSTRUMENTATION_RUNNER
//javaCompileOptions.annotationProcessorOptions.arguments['dagger.hilt.disableModulesHaveInstallInCheck'] = 'true'
}
applicationVariants.all {
outputs.all {
var formattedDate = LocalDate.now()//LocalDate.now().format(ofPattern("yyyy-MM-dd"))
var outputFileName =
"${applicationName}_${buildType.name}_${formattedDate}_v${defaultConfig.versionName}.apk"
}
}
buildTypes {
getByName("debug") {
// minifyEnabled = "false"
isMinifyEnabled = false
isTestCoverageEnabled = true
isShrinkResources = false
isDebuggable = true
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
testProguardFiles(getDefaultProguardFile("proguard-android.txt"),
"proguardTest-rules.pro")
// FirebasePerformance {
// // Set this flag to 'false' to disable @AddTrace annotation processing and
// // automatic HTTP/S network request monitoring
// // for a specific build variant at compile time.
// isInstrumentationEnabled = true
// }
}
getByName("release") {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
testProguardFiles(getDefaultProguardFile("proguard-android.txt"),
"proguardTest-rules.pro")
}
}
buildFeatures {
viewBinding = true
dataBinding = true
}
// compileOptions {
// sourceCompatibility = JavaVersion.VERSION_11
// targetCompatibility = JavaVersion.VERSION_11
// }
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
//freeCompilerArgs= ["-Xjvm-default=compatibility"]
freeCompilerArgs = listOf("-Xjvm-default=compatibility")
// freecompilerargs = List( -Xjvm-default=compatibility)
}
flavorDimensions("mode")
productFlavors {
maybeCreate("Staging")
maybeCreate("PreProduction")
maybeCreate("Production")
getByName("Staging") {
applicationIdSuffix = ".staging"
//versionNameSuffix = ".staging"
}
getByName("Production") {
dimension = "mode"
resValue("string", "app_name", "abc")
}
}
packagingOptions {
resources.excludes += "META-INF/DEPENDENCIES"
resources.excludes += "META-INF/NOTICE"
resources.excludes += "META-INF/LICENSE"
resources.excludes += "META-INF/LICENSE.txt"
resources.excludes += "META-INF/NOTICE.txt"
// excludes += ['META-INF/DEPENDENCIES', 'META-INF/NOTICE', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/NOTICE.txt']
}
dynamicFeatures += setOf(":BuyCryptoModule",":"points")
}
dependencies {
//includeing file libs
val daggerVersion = "2.42"
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(files("geetest_captcha_android_v1.7.1.1_20220308"))
appModuleDeps()
kaptAndroidTest("com.google.dagger:hilt-compiler:2.42")
// For local unit tests
testImplementation("com.google.dagger:hilt-android-testing:2.42")
kaptTest("com.google.dagger:hilt-compiler:2.42")
implementation(files("libs/opencsv-5.2.jar"))
kaptAndroidTest("com.google.dagger:dagger-compiler:$daggerVersion")
kaptTest("com.google.dagger:dagger-compiler:$daggerVersion")
releaseImplementation("com.github.ChuckerTeam.Chucker:library-no-op:3.5.2")
}
kapt {
correctErrorTypes = true
arguments {
// Make Hilt share the same definition of Components in tests instead of
// creating a new set of Components per test class.
arg("dagger.hilt.shareTestComponents", "true")
}
}