Android构建报错:Invoke-customs仅支持min-api 26 求解决方案
Hey there, let's work through this error you're encountering step by step. First, let's unpack why this is happening:
The error message points to Java 8+ language features (like invoke-custom bytecode) being used in your project, but your app's minSdkVersion is lower than 26. By default, these advanced Java features require a minimum API level of 26 to run on Android. On top of that, you mentioned changing sourceCompatibility and targetCompatibility to kotlin_version—that's a misstep, since those settings are meant for specifying Java versions, not Kotlin versions.
Here's how to fix this:
1. Correct your compileOptions configuration
First, revert the compileOptions back to a valid Java version (Java 8 is the standard for modern Android development, and it plays nicely with Kotlin). You'll also need to set the Kotlin JVM target version separately:
android { compileOptions { // Set these back to a valid Java version sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // Add or update this for Kotlin compatibility kotlinOptions { jvmTarget = "1.8" } }
2. Enable Desugaring if you need to support minSdk < 26
If your app needs to run on devices with API level lower than 26, you'll need to enable core library desugaring to make Java 8 features work on older Android versions. Add these changes to your app-level build.gradle:
android { compileOptions { coreLibraryDesugaringEnabled true // Enable desugaring sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } } dependencies { // Add the desugaring library coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2' }
3. Verify Gradle and build tool compatibility
Since you're using Android Studio 4.2.2, make sure your Gradle setup is compatible:
- Your Gradle plugin version (in project-level
build.gradle) should be4.2.2 - The Gradle wrapper version (in
gradle/wrapper/gradle-wrapper.properties) should be around6.7.1(this matches the 4.2.2 plugin) buildToolsVersion "27.0.3"is compatible with this setup, but you can also use a newer build tool version if needed (like30.0.3, which works well with AS 4.2.2)
Quick note on Kotlin version setup
Remember, your kotlin_version should be defined in your project-level build.gradle, like this:
buildscript { ext.kotlin_version = "1.5.31" // Recommended version for AS 4.2.2 repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.2.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
This is where you manage your Kotlin version, not in the compileOptions block.
That should resolve the invoke-customs error. Let me know if you hit any snags with these steps!
内容的提问来源于stack exchange,提问作者Rona Idea

