Android Studio转到声明不适用于buildSrc

时间:2019-04-21 06:35:01

标签: android-studio android-gradle gradle-kotlin-dsl

我正在按照tutorial进行Gradle依赖管理:在Android Studio中使用Kotlin和buildSrc进行build.gradle自动完成

在Dependencies.kt文件中

    object Versions {
        const val support_lib = "22.0.0"
        const val kotlin_version = "1.3.30"
        const val lifecycle_version = "1.1.1"
        const val rxjava_version = "2.2.8"
        const val rxandroid_version = "2.1.1"
    }

    object Modules{
        const val app = ":app"
        const val core = ":core"
    }

    object Libs {
        val support_appcompat_v7 = "com.android.support:appcompat-v7:${Versions.support_lib}"
        val constraint_layout = "com.android.support.constraint:constraint-layout:1.1.3"
        val lifecycle_extensions = "android.arch.lifecycle:extensions:${Versions.lifecycle_version}"
        val kotlin = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin_version}"
        val rxjava2_rxjava = "io.reactivex.rxjava2:rxjava:${Versions.rxjava_version}"
        val rxjava2_rxandroid = "io.reactivex.rxjava2:rxandroid:${Versions.rxandroid_version}"

}

在应用程序build.gradle中:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example."
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }    
    kapt {
        generateStubs = true
    }
}

dependencies {

    implementation project(Modules.core)

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation Libs.support_appcompat_v7
    implementation Libs.constraint_layout
    implementation Libs.lifecycle_extensions

    implementation Libs.kotlin
    implementation Libs.rxjava2_rxjava
    implementation Libs.rxjava2_rxandroid
    }

repositories {
    mavenCentral()
}

项目编译和构建成功,但是当我尝试从应用程序build.gradle也依赖于减速时,它显示了

enter image description here

1 个答案:

答案 0 :(得分:1)

首先,您需要将groovy gradle迁移到kotlin dsl。只有这样,您才能访问Dependencies.kt之类的Kotlin类。

第二,您必须在build.gradle.kts中导入Dependencies.kt文件,就像在Java中导入类一样。

在build.gradle的开头添加以下行

import <package_name>.Dependencies.*