错误记录Android Studio 4.2.1 编译报错 ( 设置支持的 Java 和 Kotlin 版本 )
Posted 韩曙亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误记录Android Studio 4.2.1 编译报错 ( 设置支持的 Java 和 Kotlin 版本 )相关的知识,希望对你有一定的参考价值。
一、报错信息
更新了 android Studio 到 4.2.1 4.2.1 4.2.1 , 打开之前的老项目 , 部分界面是由 Kotlin 开发的 , 进入 Kotlin 开发的界面报错 , 进入 Java 开发的界面正常 ;
此外 , Kotlin 升级到了 1.5.0 1.5.0 1.5.0 ;
Android Studio 4.2.1 4.2.1 4.2.1 编译报错 , 凡是调用到 Kotlin 的 Activity 界面 , 都爆如下异常 ;
Java 代码的 Activity 正常运行 ;
2021-06-02 15:10:31.521 32223-32223/com.example.demo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demo, PID: 32223
java.lang.BootstrapMethodError: Exception from call site #491 bootstrap method
at com.example.demo.DemoActivity.initViews(DemoActivity.kt:758)
at com.example.demo.DemoActivity.onCreate(DemoActivity.kt:378)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at com.example.demo.DemoActivity.initViews(DemoActivity.kt:758)
at com.example.demo.DemoActivity.onCreate(DemoActivity.kt:378)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
二、解决方案
创建一个最新的应用 , 查看具体的配置与 老项目的 配置区别 :
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 16
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'
}
发现在 Module 下的 build.gradle 构建脚本中 , " android " 层级下 , 配置如下选项 ;
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
将上述配置配置到 老项目 的 " android " 层级下 , 编译通过 ;
以上是关于错误记录Android Studio 4.2.1 编译报错 ( 设置支持的 Java 和 Kotlin 版本 )的主要内容,如果未能解决你的问题,请参考以下文章
错误记录Android Studio 编译报错 ( Could not find com.android.tools.build:gradle:4.2.1. )
Android 11使用Android Studio调试系统应用:编译Settings 模块解决编译错误
错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段
错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段