com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O
Asked Answered
B

8

21

I'm developping android native applications and i have for them a library project containing all the common tools for these applications. I was importing my library with jfrog artifactory and it was working well ; now i want to use git and sub-module to have my library inside my project as a module to be able to work both on app and library at the same time. But now (in my projects containing two modules (app and library)), the build fails... I've tried a lot of things before to ask help here ... I think the problem comes from duplicated dependencies between the two modules because without the library submodule build is OK.

Here is the beginning of the stack trace :

C:\Users\sri\.gradle\caches\transforms-2\files-2.1\841047937c984e1ac263db2768d03619\jetified-objenesis-3.0.1.jar: D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
Stack trace:
com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
        at com.android.tools.r8.errors.a.a(:7)
...........
//end 
* What went wrong:
Execution failed for task ':app:dexBuilderDebug'.
> There was a failure while executing work items
   > A failure occurred while executing com.android.build.gradle.internal.dexing.DexWorkAction
      > Failed to process: C:\Users\sri\.gradle\caches\transforms-2\files-2.1\841047937c984e1ac263db2768d03619\jetified-objenesis-3.0.1.jar

1. Application build.gradle files

top-level :

buildscript {
    ext.versions = [
            'kotlin'            : '1.4.0',
            'powermock'         : '2.0.7',
            'constraintLayout'  : '2.0.0',
            'butterknife'       : '10.2.3',
            'jsonwebtoken'      : '0.10.7',
            'retrofit'          : '2.9.0',
            'supportLibrary'    : '1.1.0',
            'module'            : '3.5.10',
            'okhttp'            : '4.8.1',
            'mockito'           : '3.3.3',
            'lifecycleVersion'  : '2.2.0',
            'junit'             : '4.13',
            'eventBus'          : '3.2.0',
            'picasso'           : '2.71828',
            'swipeRefreshLayout': '1.1.0',
            'cardView'          : '1.0.0',
            'viewPager'         : '1.0.0',
            'material'          : '1.3.0-alpha02',
            'rxjava'            : '3.0.4',
            'rxandroid'         : '3.0.0',
            'spoon'             : '1.7.1'
    ]
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "io.realm:realm-gradle-plugin:7.0.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
        classpath 'com.google.gms:google-services:4.3.3'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app-level :

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.firebase.crashlytics'

//.. signing removed ... //

android {
    signingConfigs {
        //........
    }
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com......."
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 42
        versionName versionProperties['versionName']
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk.abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix '-debug'
        }
    }
    lintOptions {
        abortOnError false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
        dataBinding true
    }
    configurations {
        all*.exclude group: 'xmlpull', module: 'xmlpull'
        all*.exclude group: 'xpp3', module: 'xpp3_min'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.3.0-rc03', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation project(':mob-android-qualiacmodule:qualiac-module')
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation "com.google.android.material:material:${versions.material}"
    implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:${versions.swipeRefreshLayout}"
    implementation "androidx.cardview:cardview:${versions.cardView}"
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    implementation 'com.google.firebase:firebase-crashlytics:17.2.1'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'

    implementation 'com.google.firebase:firebase-ml-vision:24.1.0'
    implementation 'com.google.android.gms:play-services-vision-image-label:18.0.5'
    implementation 'com.google.android.gms:play-services-vision-common:19.1.1'
    implementation 'com.google.android.gms:play-services-vision:20.1.1'

    kapt "com.jakewharton:butterknife-compiler:${versions.butterknife}"
    implementation "com.jakewharton:butterknife:${versions.butterknife}"
    implementation "com.squareup.retrofit2:retrofit:${versions.retrofit}"
    implementation "com.squareup.retrofit2:converter-gson:${versions.retrofit}"
    implementation "com.squareup.retrofit2:adapter-rxjava2:${versions.retrofit}"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}"
    implementation "com.squareup.okhttp3:logging-interceptor:${versions.okhttp}"
    implementation "com.squareup.okhttp3:mockwebserver:${versions.okhttp}"
    implementation "com.squareup.okhttp3:okhttp:${versions.okhttp}"
    implementation "org.greenrobot:eventbus:${versions.eventBus}"
    implementation "io.reactivex.rxjava3:rxjava:${versions.rxjava}"
    implementation "io.reactivex.rxjava3:rxandroid:${versions.rxandroid}"

    implementation "com.squareup.picasso:picasso:${versions.picasso}"
    implementation 'com.jakewharton.timber:timber:4.7.1'
    implementation "androidx.core:core-ktx:1.3.1"
    implementation "android.arch.lifecycle:extensions:1.1.1"
    api 'com.google.guava:guava:29.0-jre'
    implementation "androidx.viewpager2:viewpager2:${versions.viewPager}"
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.lifecycleVersion}"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:${versions.lifecycleVersion}"
    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:${versions.lifecycleVersion}"

    testImplementation 'junit:junit:4.13'

    androidTestImplementation "com.squareup.spoon:spoon-client:${versions.spoon}"
    androidTestImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-rc03'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0-rc03'
}

kapt {
    generateStubs = true
}

android.applicationVariants.all { variant ->
    variant.outputs.all {
        def appName = "jobs"
        outputFileName = variant.name == "debug" ? "app-debug.apk" : "${appName}-${variant.name}-${variant.versionName}.apk"
    }
}

apply plugin: 'com.google.gms.google-services'

2. Library build.gradle files

//top level

buildscript {
    ext.versions = [
            'kotlin'            : '1.3.72',
            ......
    ]
    repositories {
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:7.0.1"
        classpath 'com.android.tools.build:gradle:4.0.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.support:support-v4:28.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2"

    }
}
allprojects {

    repositories {
        jcenter()
        google()
        mavenCentral()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

//library level

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
apply plugin: 'kotlin-android-extensions'

def libraryVersion = '3.5.10'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        ndk.abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    viewBinding {
        enabled = true
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.3.0-rc03', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "androidx.appcompat:appcompat:${versions.supportLibrary}"
    implementation "com.google.android.material:material:${versions.material}"
    implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:${versions.swipeRefreshLayout}"

    implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
    implementation 'com.google.android.gms:play-services-vision-image-label:18.0.5'
    implementation 'com.google.android.gms:play-services-vision-common:19.1.1'
    implementation 'com.google.android.gms:play-services-vision:20.1.1'

    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation "androidx.cardview:cardview:${versions.cardView}"
    api 'com.google.firebase:firebase-core:17.4.4'
    implementation "com.squareup.retrofit2:retrofit:${versions.retrofit}"
    implementation "com.squareup.retrofit2:converter-gson:${versions.retrofit}"
    implementation "com.squareup.retrofit2:adapter-rxjava2:${versions.retrofit}"
    implementation "com.squareup.retrofit2:retrofit-mock:${versions.retrofit}"
    implementation "org.greenrobot:eventbus:${versions.eventBus}"
    api "com.squareup.okhttp3:logging-interceptor:${versions.okhttp}"
    implementation "com.squareup.okhttp3:mockwebserver:${versions.okhttp}"
    implementation "com.squareup.okhttp3:okhttp:${versions.okhttp}"
    api "io.jsonwebtoken:jjwt-api:${versions.jsonwebtoken}"
    runtimeOnly "io.jsonwebtoken:jjwt-impl:${versions.jsonwebtoken}"
    runtimeOnly("io.jsonwebtoken:jjwt-orgjson:${versions.jsonwebtoken}") {
        exclude group: 'org.json', module: 'json'
    }
    implementation "com.squareup.picasso:picasso:${versions.picasso}"
    implementation "io.reactivex.rxjava3:rxjava:${versions.rxjava}"
    implementation "io.reactivex.rxjava3:rxandroid:${versions.rxandroid}"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}"
    implementation "androidx.core:core-ktx:1.3.1"

    testImplementation "junit:junit:${versions.junit}"
    implementation "org.powermock:powermock-module-junit4:${versions.powermock}"
    implementation "org.powermock:powermock-module-junit4-rule:${versions.powermock}"
    implementation "org.powermock:powermock-api-mockito2:${versions.powermock}"
    implementation "org.powermock:powermock-classloading-xstream:${versions.powermock}"
    testImplementation 'org.mockito:mockito-core:3.4.6'

    implementation 'androidx.test:core:1.2.0'
    implementation 'androidx.test.ext:junit:1.1.1'
    implementation 'androidx.test:runner:1.2.0'
    implementation 'androidx.test:rules:1.2.0'
    implementation "androidx.test.espresso:espresso-core:${versions.espresso}"
    implementation "androidx.test.espresso:espresso-contrib:${versions.espresso}"
    implementation "com.squareup.spoon:spoon-client:${versions.spoon}"
    implementation "androidx.viewpager2:viewpager2:${versions.viewPager}"
    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.lifecycleVersion}"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:${versions.lifecycleVersion}"
    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:${versions.lifecycleVersion}"

    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation "androidx.test.espresso:espresso-contrib:${versions.espresso}"

    api 'com.google.guava:guava:29.0-jre'
    implementation "androidx.lifecycle:lifecycle-extensions:${versions.lifecycleVersion}"

}
Birthmark answered 27/8, 2020 at 8:58 Comment(0)
B
12

I find a solution :

Using command gradlew :app:dependencies i saw that the dependency which cause the problem ( org.objenesis:objenesis:2.6) was used by one of my dependency : org.powermock:powermock-api-mockito2:2.0.7. It's a dependency I only use one time for a test and moreover it could be replace by another one (that i already use) to do the same thing so I decided to remove this dependency and now it works good !

Conclusion : Use this command to identify which library is using the .jar that caused the problem and see if the library is really usefull for you or not.

gradlew :<yourmodule>:dependencies 
Birthmark answered 31/8, 2020 at 13:31 Comment(1)
hi. if your module name is "app" (proably the case) you can use gradlew :app:dependencies in the terminal in your project directory and then find (in all the lines displayed by this command) the library which use the dependency that's cause the problem. Have also a look at github.com/mockito/mockito/issues/2007 for more help.Birthmark
T
29

my solution is to add the following lines at the end of build.gradle

allprojects {
    configurations.all {
        resolutionStrategy.force 'org.objenesis:objenesis:2.6'
    }
}

thanks to https://github.com/mockito/mockito/issues/2007

Tallbot answered 3/10, 2020 at 6:39 Comment(2)
Thanks for the solution. In my case, I couldn't increase the SDK to 26 but I needed to upgrade mockitoBoehike
In my case, and instead of your answer, increasing the API number fixed this issue in Gradle: compileSdkVersion 33, minSdkVersion 26, targetSdkVersion 33. Error gone.Radio
S
21

This error can happen if you use mockk.

If so, replace :

androidTestImplementation group: 'io.mockk', name: 'mockk', version: mockk_version

by :

androidTestImplementation group: 'io.mockk', name: 'mockk-android', version: mockk_version

See https://github.com/mockk/mockk/issues/281#issuecomment-642176970

Shanell answered 16/6, 2021 at 11:34 Comment(1)
in my case project was using implementation instead of testImplementation. Thanks @Louis.Niblick
B
12

I find a solution :

Using command gradlew :app:dependencies i saw that the dependency which cause the problem ( org.objenesis:objenesis:2.6) was used by one of my dependency : org.powermock:powermock-api-mockito2:2.0.7. It's a dependency I only use one time for a test and moreover it could be replace by another one (that i already use) to do the same thing so I decided to remove this dependency and now it works good !

Conclusion : Use this command to identify which library is using the .jar that caused the problem and see if the library is really usefull for you or not.

gradlew :<yourmodule>:dependencies 
Birthmark answered 31/8, 2020 at 13:31 Comment(1)
hi. if your module name is "app" (proably the case) you can use gradlew :app:dependencies in the terminal in your project directory and then find (in all the lines displayed by this command) the library which use the dependency that's cause the problem. Have also a look at github.com/mockito/mockito/issues/2007 for more help.Birthmark
H
5

Just add fresh new version of objenesis to your build.gradle file

 implementation 'org.objenesis:objenesis:3.2'
Heartwood answered 21/8, 2022 at 16:54 Comment(0)
R
1

The error message

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

is caused by the input program using one of the methods (MethodHandle.invoke or MethodHandle.invokeExact).

The following code demonstrated this:

        try {
            MethodHandles.Lookup lookup = MethodHandles.publicLookup();
            MethodType methodType = MethodType.methodType(String.class, char.class, char.class);
            MethodHandle replaceMethodHandle =
                lookup.findVirtual(String.class, "replace", methodType);

            String output = (String) replaceMethodHandle.invoke("jovo", 'o', 'a');
            System.out.println(output);
        } catch (Throwable e) {
            System.out.println("Exception from MethodHandle code " + e);
        }

This code will build fine if the project minSdkLevel is 26 or above. It is not possible to have code like this under a MIN_SDK level check as the Android code requires a DEX code version only supported from Android O.

You will need to resolve which of the inputs use a MethodHandle like this.

Note, that when debugging in Android Studio this code will work if the attacked debugging device is running Android O or above. This is caused by Android Studio injecting the API level of the attached debugging device for debug builds overriding the minSdkLevel configured for the project.

Rhomb answered 28/8, 2020 at 9:46 Comment(1)
"You will need to resolve which of the inputs use a MethodHandle like this" -> How to resolve this ? My code never call MethodHandle, it comes probably from an external library but how to find it ? The problem is that if i include my library by JfrogArtifactory build is working, when i try with subModule it's not working ...Birthmark
F
1

Don't forget to use coreLibraryDesugaring instead of implementation when importing desugering lib: coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.0"

Fick answered 18/11, 2022 at 4:34 Comment(0)
F
0

Just change maven dependency to latest and this will fix this bug!

Forint answered 30/6, 2022 at 14:37 Comment(0)
B
0

Increasing minSdk to 26 fix this issue

Question itself mention that it supports only versions after Android O

Bug answered 1/12, 2023 at 5:48 Comment(2)
This would benefit from a referenceSty
@Sty the question itself mentions itBug

© 2022 - 2024 — McMap. All rights reserved.