将现有的groovy build.gradle文件转换为基于Kotlin的build.gradle.kts,

bqujaahr  于 2022-11-01  发布在  Kotlin
关注(0)|答案(2)|浏览(228)

我的项目有两个不同的用groovy语法编写的build.gradle文件。我想把这个groovy编写的gradle文件改成一个用Kotlin语法编写的gradle文件(build.gradle.kts)。
我将向您展示根项目的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文件,但该文件没有显示在我的项目中。
所以我的问题是:我如何将我的Groovy build.gradle文件转换为Kotlinbuild.gradle.kts并将这个新文件添加到我现有的项目中?
谢谢你的帮助。

ua4mk5z4

ua4mk5z41#

当然,重命名是没有用的。你需要用Kotlin DSL重写它。它类似于Groovy,但有一些不同。Read their docs,看the examples
在您的案例中,问题是:

  1. ext.kotlin_version不是有效的Kotlin语法,请使用方括号
    1.所有Kotlin字符串都使用双引号
    1.大多数函数调用的参数都需要大括号(也有例外,如中缀函数)
    1.任务管理API稍有不同。有不同的样式可用。您可以声明all the tasks in tasks block as strings,或使用单个类型函数,如下面的示例所示。
    看一看转换后的顶级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)
}
dfty9e19

dfty9e192#

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")
            }

    }

相关问题