Search code examples
androidgradlebuildandroid-gradle-plugin

Android: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)


It's really frustrating that every time Android Studio suggests to upgrade your Gradle plugin -and you do- you end with a lot of problems.

I was advised to upgrade Gradle plugin from 8.2.2 to 8.3, and after that app stopped building.

The first thing I tried, to no avail, was git reset --hard HEAD to revert all modified config files to their respective previous state, but the same error still happens.

My app already uses minSdkVersion 26 in build.gradle, but as long as I can understand from the error stack-trace the problem could be happening in Apache POI and Apache log4j jar libraries, but I don't have a clue on how to deal with them, as they are apparently not part of my project, or at least not directly added by me.

These are the relevant parts of my error stack-trace:

AGPBI: {"kind":"error","text":"MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26): Lorg/apache/logging/log4j/util/ServiceLoaderUtil;callServiceLoader(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/Class;Ljava/lang/ClassLoader;Z)Ljava/lang/Iterable;","sources":[{"file":"/Users/diego/.gradle/caches/transforms-3/9a05842052291609defaad6cb51ec0bd/transformed/jetified-log4j-api-2.21.1.jar"}],"tool":"D8"}
AGPBI: {"kind":"error","text":"MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26): Lorg/apache/poi/poifs/nio/CleanerUtil;lambda$null$0(Ljava/lang/invoke/MethodHandle;Ljava/nio/ByteBuffer;)Ljava/lang/Throwable;","sources":[{"file":"/Users/diego/.gradle/caches/transforms-3/52819773c62ede0f70c689b16da3da7e/transformed/jetified-poi-5.2.5.jar"}],"tool":"D8"}

And more:

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':Services:mergeExtDexDebugAndroidTest'.
> Could not resolve all files for configuration ':Services:debugAndroidTestRuntimeClasspath'.
   > Failed to transform poi-5.2.5.jar (org.apache.poi:poi:5.2.5) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, dexing-enable-desugaring=true, dexing-enable-jacoco-instrumentation=false, dexing-is-debuggable=true, dexing-min-sdk=24, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: /Users/diego/.gradle/caches/transforms-3/52819773c62ede0f70c689b16da3da7e/transformed/jetified-poi-5.2.5.jar.
         > Error while dexing.
           Increase the minSdkVersion to 26 or above.

   > Failed to transform log4j-api-2.21.1.jar (org.apache.logging.log4j:log4j-api:2.21.1) to match attributes {artifactType=android-dex, asm-transformed-variant=NONE, dexing-enable-desugaring=true, dexing-enable-jacoco-instrumentation=false, dexing-is-debuggable=true, dexing-min-sdk=24, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: /Users/diego/.gradle/caches/transforms-3/9a05842052291609defaad6cb51ec0bd/transformed/jetified-log4j-api-2.21.1.jar.
         > Error while dexing.
           Increase the minSdkVersion to 26 or above.

This is my main module (app) build.gradle:

plugins {
    id('dagger.hilt.android.plugin')
    id('com.android.application')
    id('kotlin-android')
    id("com.google.devtools.ksp")
    id('kotlin-kapt')
}

android {
    compileSdk 34
    def code
    Properties versionProps = new Properties()
    def versionPropsFile = file('version.properties')
    if (versionPropsFile.exists())
        versionProps.load(new FileInputStream(versionPropsFile))
    code = (versionProps['VERSION_CODE'] ?: "0").toInteger()+ 1
    packagingOptions {
        resources {
            pickFirsts += ['META-INF/LICENSE.txt']
            excludes += ['META-INF/NOTICE.md', 'META-INF/LICENSE.md', 'META-INF/INDEX.LIST', 'META-INF/DEPENDENCIES', 'META-INF/io.netty.versions.properties']
        }
    }
    versionProps['VERSION_CODE'] = code.toString()
    versionProps.store(versionPropsFile.newWriter(), null)
    defaultConfig {
        applicationId 'com.xxx.xxx'
        minSdkVersion 26
        targetSdkVersion 34
        multiDexEnabled true
        versionCode code
        versionName "5.0." + code
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_17
            targetCompatibility JavaVersion.VERSION_17

        }
        kotlinOptions {
            jvmTarget = JavaVersion.VERSION_17
        }
        vectorDrawables.useSupportLibrary = true
    }
    bundle {
        density {
            // Different APKs are generated for devices with different screen densities; true by default.
            enableSplit true
        }
        abi {
            // Different APKs are generated for devices with different CPU architectures; true by default.
            enableSplit true
        }
        language {
            // This is disabled so that the App Bundle does NOT split the APK for each language.
            // We're gonna use the same APK for all languages.
            enableSplit false
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    project.ext { appName = 'xxx' }
                    def newName = 'xxx.apk'
                    outputFileName = new File("./build/", newName)
                }
            }
        }
        /*debug {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    project.ext { appName = 'xxx' }
                    def newName = 'xxx.apk'
                    outputFileName = new File("./build/", newName)
                }
            }
        }*/
    }
    productFlavors {
    }
    dataBinding{
        enabled = true
    }
    lint {
        abortOnError false
        checkReleaseBuilds false
    }
    namespace 'com.xxx.xxx'
}

dependencies {
    api files('libs/achartengine-1.2.0.jar')
    implementation 'com.facebook.android:facebook-android-sdk:16.3.0'
    //mail API 16
    implementation 'com.sun.mail:android-mail:1.6.7'
    implementation 'com.sun.mail:android-activation:1.6.7'
    //apache commons text
    implementation group: 'org.apache.commons', name: 'commons-text', version: '1.11.0'
    //Youtube player
    implementation 'com.thefinestartist:ytpa:1.2.1'
    //Font Selector List Preference
    //api 'com.vanniktech:vntfontlistpreference:1.0.0'
    //Rate my app
    implementation 'com.github.hotchemi:android-rate:1.0.1'
    //Support
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.appcompat:appcompat:1.7.0-alpha03'
    implementation 'androidx.activity:activity-ktx:1.8.2'
    implementation 'androidx.fragment:fragment-ktx:1.6.2'
    runtimeOnly('androidx.activity:activity-compose:1.8.2')
    implementation 'androidx.fragment:fragment-ktx:1.6.2'
    //Annotation
    implementation 'androidx.annotation:annotation:1.7.1'
    //AlertDialog
    implementation 'com.github.d-max:spots-dialog:1.1@aar'
    //glide animated gifs
    implementation 'com.github.bumptech.glide:glide:4.16.0'
    implementation 'androidx.window:window:1.3.0-alpha02'
    ksp 'com.github.bumptech.glide:ksp:4.16.0'
    //circular score
    implementation 'com.wssholmes.stark:circular-score:0.1.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'com.github.mejdi14:Flat-Dialog-Android:1.0.5'
    //picasso
    implementation 'com.squareup.picasso:picasso:2.71828'
    //Stream
    implementation 'com.annimon:stream:1.2.2'
    //Multidex
    implementation "androidx.multidex:multidex:2.0.1"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    //Hilt
    implementation 'com.google.dagger:hilt-android:2.50'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7'
    kapt 'com.google.dagger:hilt-android-compiler:2.50'
    kapt('org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.9.0')
    kapt 'androidx.hilt:hilt-compiler:1.1.0'
    //modelmapper
    implementation 'org.modelmapper:modelmapper:3.2.0'
    //volley
    implementation 'com.android.volley:volley:1.2.1'
    //openai
    implementation 'com.aallam.openai:openai-client:3.7.0'
    implementation('io.ktor:ktor-client-android:2.3.8')
    implementation platform('com.aallam.openai:openai-client-bom:3.7.0')
    //glide animated gifs
    implementation 'com.github.bumptech.glide:glide:4.16.0'
    //audio visualizer
    implementation('io.github.gautamchibde:audiovisualizer:2.2.7')
    //jsoup
    implementation 'org.jsoup:jsoup:1.17.2'
    //moshi
    implementation('com.squareup.moshi:moshi:1.15.1')
    ksp('com.squareup.moshi:moshi-kotlin:1.15.1')
    ksp('com.squareup.moshi:moshi-kotlin-codegen:1.15.1')

    implementation project(':DTO')
    implementation project(':Common')
    implementation project(':Background')
    implementation project(':Core')
    implementation project(':Services')
}

I don't know if it might be relevant, but before upgrading Gradle, I upgraded Android Studio to Iguana | 2023.2.1

How can I make my app build again?


Solution

  • Considering that the build is failing when executing the :Services:mergeExtDexDebugAndroidTest task, then most likely the build.gradle of the Services module does have a minSdkVersion lower than 26.

    Please make sure that all the other modules have minSdk greater than (or equal to) 26.