634

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
  • 2,557
  • 2
  • 22
  • 41
Rona Idea
  • 6,508
  • 2
  • 7
  • 11
  • 82
    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

4 Answers4

2327

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,777
  • 2
  • 26
  • 71
Obakeng Molebatsi
  • 23,294
  • 1
  • 15
  • 9
  • 3
    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
  • 160
    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
  • 41
    Still actual in 2020. – danyapd Apr 10 '20 at 13:40
  • Not Working for me, any idea? – zukijuki Apr 29 '20 at 11:57
  • 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
    Notice the lack of `=` here. This answer didn't work for me at first because I used `sourceCompatibility = JavaVersion.VERSION_1_8`, but after removing the equal sign, it did. Easy to overlook. – Namnodorel Jun 27 '20 at 09:22
  • Thanks for this. But does anyone know why this happens? – Branddd Nov 16 '20 at 09:47
  • 25
    Still happening in 2021 – Taha Gorme Jan 08 '21 at 04:02
  • 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 Wong 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
  • 4
    still happening in 2022 – user3162662 Dec 30 '21 at 15:27
  • Thanks. Worked for me. This is the best way :) – Saeid Z Jan 16 '22 at 18:26
  • 2
    Still happening 2022, but thunk God it's solved here! – Fotios Tsakiris Feb 07 '22 at 14:26
  • cannot upvote more then once unfortunately – ror Mar 15 '22 at 18:23
53

If compileOptions doesn't work, try this

Disable 'Instant Run'.

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

Saeed
  • 2,748
  • 5
  • 29
  • 46
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
  • 7
    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
31

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
  • 321
  • 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
  • 818
  • 1
  • 15
  • 26
Chirag Patel
  • 549
  • 6
  • 12
  • 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