657

before i'm use build version gradle 26 but after change buildtoolsversion to 27 like as this image

I am using android studio 4.2.2 recently i update all my dependency and

      sourceCompatibility JavaVersion.VERSION_1_10
      targetCompatibility JavaVersion.VERSION_1_10

to

compileOptions {
           sourceCompatibility kotlin_version
           targetCompatibility kotlin_version
         }

after update i am getting this error please help

error : error build gradle screenshot

adrisons
  • 3,443
  • 3
  • 32
  • 48
Rona Idea
  • 6,734
  • 2
  • 9
  • 12
  • 84
    Do not attach screenshots/images with code/error messages. Post the actual (relevant) code + error message. – Robert Apr 18 '18 at 08:56
  • 1
    I started to get similar issues when i converted inline callbacks to lambda functions. Intellj was asking it. But compiler does not like it. – Gillis Haasnoot May 23 '18 at 22:28
  • 6
    It is a shame that the OP did not come back and mark an answer as correct, even though there is one. Actually, all three are, depending on whether you are using java 7, 8, or 10. Bottom line is, you need to inform the build what language level you are using. And yes, the screen caps are pretty much unreadable on the page. – Thomas Carlisle Sep 18 '18 at 20:19
  • 1
    That did not work for me. But adding it to a module's build.gradle did work. See Galley's Dec. 19 comment. Project is https://github.com/jrasor/ftc_5197all2019; module is TeamCode. – John Rasor Feb 27 '19 at 21:13
  • 2
    [An image of your code is not helpful](http://idownvotedbecau.se/imageofcode). Also, [an image of your exception is not helpful, either.](http://idownvotedbecau.se/imageofanexception/) – EJoshuaS - Stand with Ukraine Mar 31 '19 at 02:55
  • These screenshots are useful. I wanted to compare his minSdkVersion. If you raise minSdkVersion to 26 you do not have this issue. – Martin Berger Jan 09 '20 at 14:59

5 Answers5

2384

After hours of struggling, I solved it by including the following within app/build.gradle:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

https://github.com/mapbox/mapbox-gl-native/issues/11378

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Obakeng Molebatsi
  • 23,864
  • 1
  • 15
  • 9
  • 4
    This even seems to be the case when the gradle build says that it failed in a nother module. If module A has the declaration but B does not, sometimes for me it failed in A. After I added it to B it doesn't fail anymore – avalancha Aug 09 '18 at 10:42
  • Notice: You need add in sub module directory, for example `app/build.gradle`, NO top level build.gradle file. – Galley Dec 19 '18 at 04:37
  • Note, This lines configure only for each module that uses Java 8 language features (either in its source code or through dependencies). – Mahmoud Ibrahim Mar 21 '19 at 10:29
  • 164
    Android Studio devs need to change this error description. This `Error: Invoke-customs are only supported starting with Android O (--min-api 26)` tells nothing. Thank you so much for sharing your findings. You saved others time. – Zeeshan Mar 28 '19 at 21:25
  • 3
    After i add apachi commons lang library to project i faced this error.Then after add above code fixed my error.Thanks. – Saeid Z Jun 09 '19 at 15:42
  • But what if it runs on real device with higher java version, for example android 10 running higher java version than 1.8, Someone please throw more light, please. Thank you and upvoted as it helped me too. Google needs to change documenters first and programmers next. – Rushikant Pawar Jan 30 '20 at 14:54
  • 1
    Wait!! How can this be made default in Android Studio. I don't want to use this in every project I create. – Ümañg ßürmån May 24 '20 at 13:34
  • 1
    Thanks for this. But does anyone know why this happens? – Branddd Nov 16 '20 at 09:47
  • 1
    Fixed it for me, thanks for posting this instead of "nevermind I fixed it" like some people do. – fizgig Mar 05 '21 at 15:50
  • I just had to remove `implementation 'com.ea.async:ea-async:1.2.3'` from gradle and sync again. – Benur21 Apr 25 '21 at 16:49
  • This works for me. I have this added to many old projects already, and I am not aware of this problem, until I work on an external project and saw this error for the first time due to the absence of these few lines. – Ryan W Aug 28 '21 at 18:35
  • Do you know which feature requires Java8 in this case? I might prefer to remove that feature rather than require Java8 for my builds. – Edward Falk Sep 16 '21 at 20:50
53

If compileOptions doesn't work, try this

Disable 'Instant Run'.

Android Studio -> File -> Settings -> Build, Execution, Deployment -> Instant Run -> Disable checkbox

Saeed
  • 3,294
  • 5
  • 35
  • 52
Roshni Kyada
  • 725
  • 1
  • 6
  • 10
  • 7
    This and only this worked for me. Very annoying as this seems like a bug in Android Studio. Instant run is a needed feature - anyone know how to resolve this error and make use of instant run? – AlexVPerl Mar 27 '19 at 06:28
  • 3
    Instant Run is a grand headache. I can't count how much time I have lost due to this not so useful to saving time tool. I've always ended up disabling it on all my projects. I don't think is as much needed. – Rubén Viguera Apr 23 '19 at 09:19
  • 3
    Had this same problem after updating to Android Studio 3.4 and Gradle 5.1. Disabling Instant Run did the trick! Thanks – Henrique Apr 24 '19 at 15:59
  • 8
    This option is not available in my Android Studio. Details - (Android Studio 3.5.1 Build #AI-191.8026.42.35.5900203, built on September 26, 2019 JRE: 1.8.0_202-release-1483-b03 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0) – Nikhil Girraj Apr 18 '20 at 18:57
  • which version of ANdroid studio is this? – gumuruh Apr 27 '23 at 08:06
30

In my case the error was still there, because my system used upgraded Java. If you are using Java 10, modify the compileOptions:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_10
    targetCompatibility JavaVersion.VERSION_1_10

}
Nata Sturza
  • 311
  • 3
  • 4
24

If you have Java 7 so include the below following snippet within your app-level build.gradle :

compileOptions {

    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7

}
exploitr
  • 843
  • 1
  • 14
  • 27
Chirag Patel
  • 559
  • 6
  • 14
  • 4
    It is also necessary to set the **minSdkVersion** and **targetSdkVersion** according to the min api reported in the error message. `build.gradle` : defaultConfig { applicationId "com.mypack.myapp" **minSdkVersion 26** targetSdkVersion 29 versionCode 1 versionName "1.0" ... } – Pascal Chardon Jul 17 '20 at 17:06
  • Used same solution for Java 8, changed to: VERSION_1_8. – Elletlar Nov 23 '20 at 18:59
  • compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } go to build.gradle and add this file – Aqif Jul 30 '21 at 07:54
0

Try downgrading one or more of your libraries (Gradle dependencies). The most recent versions usually have less backwards-compatibility support.

For example:

build.gradle

dependencies {
    // Version 2.5.1 was released on 27 July 2022
    // This will produce the error:
    // "Invoke-customs are only supported starting with android 0 --min-api 26"
    // if you're using minSdkVersion 21 (Android 5.0)
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
}

If you downgrade this particular library to version 2.2.0, it will fix the error:

build.gradle

dependencies {
    // Version 2.2.0 was released on 22 Jan 2020
    // This will fix the error:
    // "Invoke-customs are only supported starting with android 0 --min-api 26"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
}

You can see the available versions and release dates here: https://maven.google.com/

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59