【错误记录】Android Studio 4.2.1 编译报错 ( 设置支持的 Java 和 Kotlin 版本 | java.lang.BootstrapMethodError )

2023-03-29 13:22:51 浏览数 (1)

文章目录

  • 一、报错信息
  • 二、解决方案

一、报错信息


更新了 Android Studio 到 4.2.1 , 打开之前的老项目 , 部分界面是由 Kotlin 开发的 , 进入 Kotlin 开发的界面报错 , 进入 Java 开发的界面正常 ;

此外 , Kotlin 升级到了 1.5.0 ;

Android Studio 4.2.1 编译报错 , 凡是调用到 Kotlin 的 Activity 界面 , 都爆如下异常 ;

Java 代码的 Activity 正常运行 ;

代码语言:javascript复制
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) 

二、解决方案


创建一个最新的应用 , 查看具体的配置与 老项目的 配置区别 :

代码语言:javascript复制
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 " 层级下 , 配置如下选项 ;

代码语言:javascript复制
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

将上述配置配置到 老项目 的 " android " 层级下 , 编译通过 ;

0 人点赞