尝试运行基本活动时生成错误



我在学习Android Studio/Kotlin的第一天,并通过(官方的?)教程工作。

在空白布局中添加了一些基本视图并解决了一些"编码实践"。警告,当我试图在USB连接的平板电脑上运行应用程序时,我得到一个构建失败的消息。(我还没有得到任何Kotlin编码。)在这个阶段,我完全不知所措,找不到一个不超出我理解水平的解决方案。

错误是…

肇因:java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses

肇因:java.lang.ClassNotFoundException: kotlin.reflect.full.KClasses

如果还有什么值得分享的信息,恐怕我现在还不知道是什么。有什么建议,我需要做什么或调查,以解决构建失败?第一次构建。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
}

第二次构建。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'
}

试试这些步骤。

  1. 更新您的Kotlin版本从"1.5.10"到最新的"1.5.31"在您的第一个build.gradle文件。同时更新Kotlin插件:File ->设置→语言和框架->芬兰湾的科特林如果有新版本,会有Install按钮单击。然后重启Android Studio,运行你的项目,看看它是否能工作。

  2. 如果这不起作用,您可以手动将缺失的反射库添加到项目中。我在依赖项中没有看到任何使用反射的东西,但也许我遗漏了什么。因此,将这一行添加到第二个build.gradle文件的dependencies部分:

    implementation "org.jetbrains.kotlin:kotlin-reflect"

    然后点击"与Gradle文件同步项目"单击工具栏右端附近的按钮(带有蓝色箭头图标的大象),并尝试再次运行您的项目。

  3. 你正在使用的Android Build Tools也过时了。我不确定它是否相关,但您也可以在第一个build.gradle文件中更新它。将4.2.2改为7.0.3,并再次点击Gradle同步按钮。Android Studio可能会抱怨你需要更新Gradle版本,所以如果它提供更新,请点击yes。

最新更新