4

使用以下文章作为参考将我的原始 Kotlin Gradle 脚本从使用 groovy 转换为使用 Kotlin DSL: A Better way to Manage Gradle Build ScriptMigrating Gradle Build Scripts to Kotlin DSL。编译我的代码后,我得到以下构建错误。

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could 
not resolve project :app.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for 
':app@debugAndroidTest/compileClasspath': Could not resolve project :app.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for 
':app@debugUnitTest/compileClasspath': Could not resolve project :app.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@release/compileClasspath': 
Could not resolve project :app.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for 
':app@releaseUnitTest/compileClasspath': Could not resolve project :app.
Show Details
Affected Modules: app

过去在这个平台上提出的问题表明,

  • 使缓存无效并重新启动我尝试过的android studio,但这似乎不起作用。
  • 确保 gradle 全局设置没有被选为离线工作,也被检查为离线并且问题仍然存在。

我已尝试使用建议的解决方案解决此问题。我的 app 模块的build.gradle如下:

plugins {
    id(Plugins.androidApplication)
    kotlin(Plugins.kotlinAndroid)
    kotlin(Plugins.kotlinExtensions)
    kotlin(Plugins.kapt)
}
android {

    compileSdkVersion(Configs.compileVersion)
    defaultConfig {
        applicationId = Configs.applicationId
        minSdkVersion(Configs.minSdkVersion)
        targetSdkVersion(Configs.targetSdkVersion)
        testInstrumentationRunner = Configs.testInstrumentationRunner
        var value = Integer.parseInt(project.property("build_version_code") as String?)
        versionCode = value
        versionName = project.property("build_version") as String?
    }

    signingConfigs {
        getByName("debug") {

        }

        create("release") {

        }
    } 
        buildTypes {
        getByName("debug") {
            buildConfigField("boolean", "MOCK_HARDWARE", "false")
            signingConfig = signingConfigs.findByName("debug")
            applicationIdSuffix = ".debug"
            matchingFallbacks = listOf("debug")
        }
        getByName("release") {
            isMinifyEnabled = false
            buildConfigField("boolean", "MOCK_HARDWARE", "false")
            proguardFiles(getDefaultProguardFile(ProGuards.proguardTxt), ProGuards.androidDefault)
            signingConfig = signingConfigs.findByName("release")
            matchingFallbacks = listOf("release")
        }

    dataBinding {
        isEnabled = true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
repositories {
    flatDir {
        dirs("libs")
    }
}
dependencies {

    implementation(fileTree(mapOf("dir" to "libs", "include" to 
    listOf("*.jar", "*.arr"))))
    implementation(Libs.stdLib)
    implementation(Libs.sunmiui)
    implementation(Libs.slf4j)
    implementation(Libs.appCompact)
    implementation(Libs.otpView)
    implementation(Libs.vectordrawableAnimated)
    implementation(Libs.materialComponents)
    implementation(Libs.recyclerView)
    implementation(Libs.constraintLayout)
    implementation(Libs.junit)
    implementation(Libs.testRunner)
    implementation(Libs.expressoCore)
    implementation(Libs.lifecyleExtensions)
    kapt(Libs.lifecyleCompiler)
    implementation(Libs.roomRuntime)
    implementation(Libs.databindingCompiler)
    implementation(Libs.rxjava)
    implementation(Libs.rxjavaAndroid)
    implementation(Libs.glide)
    implementation(Libs.glideCompiler)
    implementation(Libs.gson)
    implementation(Libs.joda)
    implementation(Libs.countrycodePicker)
    implementation(Libs.timber)
    implementation(Libs.daggerandroidSupport)
    implementation(Libs.daggerandroidProcessor)
}

我的Dependecies.kts如下:

    object Versions
    {
        // kotlin
        const val kotlin = "1.3.31"
        const val buildToolsVersion = "28.0.3"

        const val lifecycleExtensionVersion = "2.0.0"
        const val lifecyleCompilerVersion = "2.0.0"

        const val stdLibVersion = "1.3.21"
        const val otpViewVersion = "2.0.3"

        const val appCompactVersion = "1.1.0-alpha05"
        const val constraintLayoutVersion = "2.0.0-beta1"
        const val materialComponentsVersion = "1.1.0-alpha06"
        const val recyclerViewVersion = "1.0.0"

        const val junitVersion = "4.12"
        const val testRunnerVersion = "1.1.1"
        const val expressoCoreVersion = "3.1.1"

        const val roomRuntimeVersion = "2.1.0-alpha04"

        const val databindingCompilerVersion = "3.5.0-alpha06"

        const val rxjavaVersion = "2.2.7"
        const val rxjavaAndroidVersion = "2.1.1"

        const val glideVersion = "4.8.0"
        const val glideCompilerVersion = "4.8.0"

        const val gsonVersion = "2.8.5"

        const val jodaVersion = "2.7.2"

        const val countrycodePickerVersion = "2.3.0"

        const val timberVersion = "4.7.1"

        const val daggerandroidSupportVersion = "2.19"
        const val daggerandroidProcessorVersion = "2.19"

        const val slf4jVersion = "1.7.21"

        const val vectordrawableAnimatedVersion = "1.1.0-beta01"

        const val sunmiuiVersion = "1.1.27"
    }

    object Libs
    {
        const val lifecyleExtensions = "androidx.lifecycle:lifecycle-extensions:${Versions.lifecycleExtensionVersion}"
        const val lifecyleCompiler = "androidx.lifecycle:lifecycle-compiler:${Versions.lifecyleCompilerVersion}"

        const val stdLib = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}"

        const val otpView = "com.github.mukeshsolanki:android-otpview-pinview:${Versions.otpViewVersion}"

        const val appCompact = "androidx.appcompat:appcompat:${Versions.appCompactVersion}"
        const val constraintLayout = "androidx.constraintlayout:constraintlayout:${Versions.constraintLayoutVersion}"
        const val materialComponents = "com.google.android.material:material:${Versions.materialComponentsVersion}"
        const val recyclerView = "androidx.recyclerview:recyclerview:${Versions.recyclerViewVersion}"

        const val junit = "junit:junit:${Versions.junitVersion}"
        const val testRunner = "androidx.test:runner:${Versions.testRunnerVersion}"
        const val expressoCore = "androidx.test.espresso:espresso-core:${Versions.expressoCoreVersion}"

        const val roomRuntime = "androidx.room:room-runtime:${Versions.roomRuntimeVersion}"

        const val databindingCompiler = "androidx.databinding:databinding-compiler:${Versions.databindingCompilerVersion}"

        const val rxjava = "io.reactivex.rxjava2:rxjava:${Versions.rxjavaVersion}"
        const val rxjavaAndroid = "io.reactivex.rxjava2:rxandroid:${Versions.rxjavaAndroidVersion}"

        const val glide = "com.github.bumptech.glide:glide:${Versions.glideVersion}"
        const val glideCompiler = "com.github.bumptech.glide:compiler:${Versions.glideCompilerVersion}"

        const val gson = "com.google.code.gson:gson:${Versions.gsonVersion}"

        const val joda = "net.danlew:android.joda:${Versions.jodaVersion}"

        const val countrycodePicker = "com.github.joielechong:countrycodepicker:${Versions.countrycodePickerVersion}"

        const val timber = "com.jakewharton.timber:timber:${Versions.timberVersion}"

        const val daggerandroidSupport = "com.google.dagger:dagger-android-support:${Versions.daggerandroidSupportVersion}"
        const val daggerandroidProcessor = "com.google.dagger:dagger-android-processor:${Versions.daggerandroidProcessorVersion}"

        const val slf4j = "org.slf4j:slf4j-api:${Versions.slf4jVersion}"

        const val vectordrawableAnimated = "androidx.vectordrawable:vectordrawable-animated:${Versions.vectordrawableAnimatedVersion}"

        const val sunmiui = "com.sunmi:sunmiui:${Versions.sunmiuiVersion}"
    }

    object Plugins
    {
        const val androidApplication = "com.android.application"
        const val kotlinAndroid = "android"
        const val kotlinExtensions = "android.extensions"
        const val kapt = "kapt"
    }

    object ProGuards
    {
        val androidDefault = "proguard-rules.pro"
        val proguardTxt = "proguard-android.txt"
    }

    object ClassPaths
    {
        const val gradlePlugin = "com.android.tools.build:gradle:3.4.1"
        const val kotlinPlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31"
    }

    object Configs
    {
        const val applicationId = "eteetetet"
        const val compileVersion = 28
        const val minSdkVersion = 24
        const val targetSdkVersion = 28
        const val testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

我的项目模块的 build.gradle 如下:

    buildscript {
        repositories {
            google()
            jcenter()
            mavenCentral()
        }

        dependencies {
            classpath(ClassPaths.gradlePlugin)
            classpath(ClassPaths.kotlinPlugin)

            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    allprojects {
        repositories {
            jcenter()
            google()
            mavenCentral()
            maven {
                url = uri("https://plugins.gradle.org/m2/")
            }
            maven {
                url = uri("https://maven.google.com")  
            }
            maven { url = uri("https://jitpack.io") }
        }
    }

    tasks.register("clean", Delete::class) {
        delete(rootProject.buildDir)
    }

我的settings.gradle.kts文件如下:

include(":app",
        ":asterixclient",
        ":asterixtest",
        ":data",
        ":hardware",
        ":registrations",
        ":rewards",
        ":rules",
        ":rulescontracts",
        ":sdfclient",
        ":sdfservice",
        ":sunmiclient",
        ":sync",
        ":till",
        ":tsdevice",
        ":ui",
        ":webapi")

project(":asterixclient").projectDir = 
File("../modules/sdfservice/asterixclient")
project(":asterixtest").projectDir = 
File("../modules/sdfservice/asterixtest")
project(":data").projectDir = File("../modules/data/data")
project(":hardware").projectDir = File("../modules/hardware/hardware")
project(":registrations").projectDir = File("../modules/registrations/app")
project(":rewards").projectDir = File("../modules/rewards/rewards")
project(":rules").projectDir = File("../modules/rules/rules")
project(":rulescontracts").projectDir = 
File("../modules/rules/rulescontracts")
project(":sdfclient").projectDir = File("../modules/sdfservice/sdfclient")
project(":sdfservice").projectDir = File("../modules/sdfservice/sdfservice")
project(":sunmiclient").projectDir = 
File("../modules/sdfservice/sunmiclient")
project(":sync").projectDir = File("../modules/sync/sync")
project(":till").projectDir = File("../modules/till/till")
project(":tsdevice").projectDir = File("../modules/sdfservice/tsdevice")
project(":ui").projectDir = File("../modules/ui/ui")
project(":webapi").projectDir = File("../modules/webapi/webapi")
4

1 回答 1

1

我终于解决了这个问题,这就是我改变的方式

首先我更新到最新版本的 Gradle 发行版

#Tue May 14 11:11:48 CAT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

然后我将 app 模块的 gradle.buid.kts 更改为如下所示

plugins {
    id(Plugins.androidApplication)
    kotlin(Plugins.kotlinAndroid)
    kotlin(Plugins.kotlinExtensions)
    kotlin(Plugins.kapt)
}

android {
    compileSdkVersion(Configs.compileVersion)
    buildToolsVersion(Versions.buildToolsVersion)

    defaultConfig {
        multiDexEnabled = true
        applicationId = Configs.applicationId
        minSdkVersion(Configs.minSdkVersion)
        targetSdkVersion(Configs.targetSdkVersion)
        val version_code = Integer.parseInt(project.property("build_version_code") as String)
        versionCode = version_code
        versionName = project.property("build_version") as String
        testInstrumentationRunner = Configs.testInstrumentationRunner
        proguardFiles(getDefaultProguardFile(ProGuard.proguardTxt), ProGuard.androidDefault)
    }   

    signingConfigs {
        named("debug").configure {
            storeFile = file(project.property("keyStore") as String)
            storePassword = project.property("keystorePassword") as String
            keyPassword = project.property("keystorePassword") as String
            keyAlias = project.property("keyName") as String
        }   

        register("release") {
            storeFile = file(project.property("keyStore") as String)
            storePassword = project.property("keystorePassword") as String
            keyPassword = project.property("keystorePassword") as String
            keyAlias = project.property("keyName") as String
        }
    }

    buildTypes {
        named("release").configure {
            buildConfigField("boolean", "MOCK_HARDWARE", "false")
            proguardFiles(getDefaultProguardFile(ProGuard.proguardTxt), ProGuard.androidDefault)
            signingConfig = (signingConfigs.getByName("release"))
            matchingFallbacks = listOf("release")
        }
        named("debug").configure {
            buildConfigField("boolean", "MOCK_HARDWARE", "false")
            applicationIdSuffix = ".debug"
            signingConfig = signingConfigs.getByName("debug")
            matchingFallbacks = listOf("debug")
        }    
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    repositories {
        flatDir {
            dirs("libs")
        }
    }
}

dependencies {
    implementation(fileTree(mapOf("include" to listOf("*.jar", "*.arr"), "dir" to "libs")))
    implementation(Libs.stdLib)
    implementation(Libs.sunmiui)
    implementation(Libs.slf4j)
    implementation(Libs.appCompact)
    implementation(Libs.otpView)
    implementation(Libs.vectordrawableAnimated)
    implementation(Libs.materialComponents)
    implementation(Libs.recyclerView)
    implementation(Libs.constraintLayout)
    implementation(Libs.junit)
    implementation(Libs.testRunner)
    implementation(Libs.expressoCore)
    implementation(Libs.lifecyleExtensions)
    annotationProcessor(Libs.lifecyleCompiler)
    implementation(Libs.roomRuntime)
    annotationProcessor(Libs.databindingCompiler)
    implementation(Libs.rxjava)
    implementation(Libs.rxjavaAndroid)
    implementation(Libs.glide)
    annotationProcessor(Libs.glideCompiler)
    implementation(Libs.gson)
    implementation(Libs.joda)
    implementation(Libs.countrycodePicker)
    implementation(Libs.timber)
    implementation(Libs.daggerandroidSupport)
    annotationProcessor(Libs.daggerandroidProcessor)    
    implementation(project(":data"))
    implementation (project( ":ui"))
    implementation (project( ":webapi"))
    implementation (project(":hardware"))
    implementation (project(":sdfclient"))

}

我还更新了 settings.gradle.kts 文件中的项目引用,如下所示:

include(":app",
        ":data")    
project(":data").projectDir = file("../modules/data/data")) 

主要的变化是项目中的signingConfigs、buildTypes 和其他模块的引用。例如

implementation(project(":data"))

代替

implementation(project(path, ":data"))
于 2019-05-30T17:16:54.403 回答