【发布时间】:2021-12-07 06:41:13
【问题描述】:
我正在学习 Android Studio / Kotlin 的第一天,并通过(官方?)tutorials 工作。
在空布局中添加了一些基本视图并解决了几个“编码练习”警告后,当我尝试在 USB 连接的平板电脑上运行该应用程序时,我收到一条构建失败消息。 (我还没有任何 Kotlin 编码。) 在这个阶段,我完全没有深度,找不到不超出我理解水平的解决方案。
错误是...
原因:java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses
原因:java.lang.ClassNotFoundException: kotlin.reflect.full.KClasses
如果还有其他值得分享的信息,恐怕我现在还不知道是什么。 关于我需要做什么或调查以解决构建失败的任何建议?
第一个 build.gradle 文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第二个 build.gradle 文件:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.test04"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
【问题讨论】:
-
您最近是否下载了 Android Studio,您的项目是您在 Android Studio 中使用 New Project 创建的吗?问题似乎是您的项目依赖于使用 Kotlin 反射的库,但 Kotlin 反射库不在您的项目依赖项中。如果您没有弄乱您的
build.gradle文件或添加依赖项,我不确定这会发生什么。 -
谢谢。是的,它是最近下载的 AS,并且该项目是从 New Project 创建的。我不知道做过你提到的任何一件事情。由于我还没有走多远,卸载重装AS最简单吗?
-
您不需要卸载。尝试创建一个新项目并在不更改任何内容的情况下运行它。如果这不起作用,请将两个
build.gradle文件的文本发布到您的项目中,并告诉我们 Android Studio 版本以及您正在使用的项目模板。 -
谢谢。 Android Studio 版本是 4.2.2 我使用“空活动”作为起点。 (我假设这就是您所说的模板。)我在哪里可以找到 build.gradle 文件?
-
在左上角列出项目文件的地方,展开 Gradle Scripts。应该有两个名为
build.gradle的不同文件。
标签: android-studio kotlin build-error