我正在使用新的Beta版本的Android Studio 3.4和kotlin dsl作为扩展gradle脚本。我在项目中有很多模块,这就是为什么我编写KotlinBuildScript扩展来构建项目的原因,但是对于所有外部依赖项,我都有错误消息,例如“ ERROR:无法解决:com.google.dagger:dagger:2.18”,“ ERROR :无法解决:androidx.recyclerview:recyclerview:1.0.0”。我该如何解决?
这是我的根build.gradle.kts:
buildscript {
repositories {
mavenCentral()
jcenter()
google()
maven(url = uri("https://dl.bintray.com/kotlin/kotlin-eap"))
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.10")
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
google()
maven(url = uri("https://dl.bintray.com/kotlin/kotlin-eap"))
}
}
还有我的KotlinBuildScript:
object Version {
val kotlin = "1.3.10"
val androidx = "1.0.0"
val constraintlayout = "1.1.3"
val lifecycle = "2.0.0"
val paging = "2.1.0-alpha01"
}
fun KotlinBuildScript.setupApplication(applicationId:String, versionCode:Int, versionName:String) =
setup(applicationId, versionCode, versionName, false)
fun KotlinBuildScript.setupLibrary(applicationId:String) = setup(applicationId, null, null, true)
private fun KotlinBuildScript.setup(
applicationId:String, versionCode:Int?, versionName:String?, library:Boolean
) {
setupPlugins(library)
setupConfiguration(library, applicationId, (versionCode ?: 1) * 2, versionName ?: "1.0")
setupDependencies(library)
}
private fun KotlinBuildScript.setupConfiguration(
library:Boolean, applicationId:String, versionCode:Int, versionName:String
) {
configure<BaseExtension> {
compileSdkVersion(28)
buildToolsVersion("28.0.3")
defaultConfig {
if (!library) {
this.applicationId = applicationId
this.versionCode = versionCode
this.versionName = versionName
}
minSdkVersion(16)
targetSdkVersion(28)
multiDexEnabled = true
vectorDrawables.useSupportLibrary = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
packagingOptions {
exclude("META-INF/LICENSE.txt")
}
lintOptions {
isCheckReleaseBuilds = false
}
if (library) {
defaultConfig.ndk {
abiFilters("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
}
externalNativeBuild {
cmake {
setPath("CMakeLists.txt")
}
}
} else {
flavorDimensions("architecture")
productFlavors {
create("arm") {
dimension = "architecture"
this.versionCode = versionCode - 1
versionNameSuffix = "-arm"
ndk {
abiFilters("armeabi")
}
}
create("x86") {
dimension = "architecture"
this.versionCode = versionCode
versionNameSuffix = "-x86"
ndk {
abiFilters("x86")
}
}
}
}
}
}
private fun KotlinBuildScript.setupPlugins(library:Boolean) {
apply {
plugin("com.android." + (if (library) "library" else "application"))
plugin("kotlin-android")
plugin("kotlin-android-extensions")
plugin("kotlin-kapt")
}
}
private fun KotlinBuildScript.setupDependencies(library:Boolean) {
dependencies {
add("testImplementation", "junit:junit:4.12")
add("implementation", fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
if (!library) {
add("implementation", project(":Library"))
}
add("kapt", "com.google.dagger:dagger-compiler:2.18")
add("implementation", "com.google.dagger:dagger:2.18")
add("kapt", "androidx.lifecycle:lifecycle-compiler:${Version.lifecycle}")
add("implementation", "androidx.lifecycle:lifecycle-extensions:${Version.lifecycle}")
add("implementation", "androidx.lifecycle:lifecycle-common-java8:${Version.lifecycle}")
add("implementation", "javax.inject:javax.inject:1")
add("implementation", "javax.annotation:javax.annotation-api:1.2")
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Version.kotlin}")
add("implementation", "org.jetbrains.kotlin:kotlin-reflect:${Version.kotlin}")
add("implementation", "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1")
}
}
应用程序模块gradle:
import Setup.Version
import Setup.setupLibrary
setupLibrary("com.esminis.server.library")
答案 0 :(得分:0)
我已经找到了解决问题的方法。使用kotlin dsl,您应该始终在项目的settings.gradle中设置buildFileName属性。
rootProject.buildFileName = 'build.gradle.kts'
此外,尝试删除.idea和.gradle文件夹,然后重新启动Android Studio。