61

I just installed the Android SDK R16 and JDK 7, but I keep getting the error

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties."

I am using some features of the JRE 1.7, so I can't just switch it to 1.6. How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eBehbahani
  • 1,579
  • 5
  • 19
  • 41
  • What do you mean, "using some features of the 1.7 JRE"? – Dave Newton Dec 20 '11 at 17:17
  • 4
    I use strings in switch statements, Which is not supported in JRE 1.6 (JDK 6) – eBehbahani Dec 20 '11 at 17:32
  • 1
    And you've verified that the byte code will be successfully converted into something Android (which doesn't support JDK 7) can deal with? http://stackoverflow.com/questions/7153989/java-7-language-features-with-android – Dave Newton Dec 20 '11 at 18:17
  • 1
    Nope I just assumed that it would work. – eBehbahani Dec 20 '11 at 18:23
  • 6
    Like ATOM mentioned, _you must keep source compatibility with Java 5/6 during compile_, so no strings in switch statements unfortunately. Anyone know when Android will support compiler compliance level 7.0? – Dennis Oct 16 '12 at 09:21

4 Answers4

40

Yes, you can use Java 1.7 for the Android development. You must keep source compatibility with Java 6 during compile and only the next problem is because of the changed default algorithm for jarsigner.

This is solved in Stack Overflow question Android signing with Ant (workaround).

But it is already fixed in Ant. So the official support will be directly in the Android SDK soon.

If you want to use Maven for the build, the solution is in What kind of pitfals exist for the Android APK signing.

Update: Latest version of Ant, Maven Plugin or Gradle has no problem with Java 7 or Java 8, so this is already obsolete.

Community
  • 1
  • 1
ATom
  • 15,960
  • 6
  • 46
  • 50
  • 17
    It's a little bit outdated. Starting from build tools 19, Android has full support for Java 1.7. So you can set 1.7 as source and target for compilation. Support for this is from Android studio 0.3.2. Android kitkat has full support of JAVA 1.7 API, but most of language feature from 1.7 you can use on the old androids too. Except "try with resources". – ATom Dec 16 '13 at 16:03
  • 1
    Is it safe to use above Android API8(Froyo) project? – Youngjae Apr 06 '14 at 04:55
  • 1
    Yes, but support for API 8 is just waste of time. – ATom Apr 06 '14 at 10:03
  • 7
    unfortunately, 14% of our members are not waste. – Youngjae Apr 06 '14 at 11:22
  • @ATom I'm looking for Java 8 compatibility, but I found nothing. Where did you take your infos for Java 7 support? – doplumi Jul 21 '14 at 17:08
  • I don't know, where I found infos, but I'm already testing Java 8 and it looks good. If you still use ANT to build APK you will need to update ANT to latest version. With maven it also works. Only one problem that I found already is javadoc generator. New one in Java 8 is much more strict... but I think that it will be possible change it. – ATom Jul 21 '14 at 17:12
29

Starting with the ADT (22.6), it's possible to use Java 1.7 functionalities:

IMPORTANT:

The try-with-resources needs API 19 and above:

Added support for Java 7 language features like multi-catch, try-with-resources, and the diamond operator. These features require version 19 or higher of the Build Tools. Try-with-resources requires minSdkVersion 19; the rest of the new language features require minSdkVersion 8 or higher.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 9
    I'd like to emphasize from the release notes that your app **only need to be** minSdk 19 if you're using try-with-resources. Otherwise you'll need minSdk 8. (I had initially only read the first line of your answer, which implies otherwise.) – Paul Lammertsma Mar 25 '14 at 14:32
  • yes, that's what i meant. if you use it, you will need minSdk 19 . it's also what's written there. now we need to wait for support of JAVA 1.8 . :) – android developer Mar 25 '14 at 14:35
  • 1
    I just would like to add the also the ```java.util.Objects``` class can be used only from API 19 on (cause it has been added from that API level on). – Andrea Richiardi Apr 04 '14 at 20:37
  • @Kap interesting. never heard of this package. does "deepEquals" work differently only for arrays, and act as "equals" for the rest? – android developer Apr 05 '14 at 08:28
  • There a couple of nifty things in there...I have started using it, but a test on API 18 returned ClassNotFound. Can you confirm? Maybe I am doing something wrong. [Here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/Objects.java#Objects.deepEquals%28java.lang.Object%2Cjava.lang.Object%29) is the source code. – Andrea Richiardi Apr 05 '14 at 10:04
  • It's more NoClassDefFoundError :) – Andrea Richiardi Apr 05 '14 at 10:13
  • @Kap you've put a link to openJDK, and not to the source code of your project. – android developer Apr 05 '14 at 11:14
  • Sorry it was not clear...I wanted to show OpenJdk the source and I was wondering if I was doing something wrong in using java.util.Objects in API 18...It turns out that after the later Android SDK update, I found a nifty ```com.android.internal.util.Objects``` which I now can use for APIs < 19. – Andrea Richiardi Apr 07 '14 at 07:27
  • @Kap so it's available , yet with a totally different package name? – android developer Apr 07 '14 at 08:26
  • Yes, different package name for < 19 (for 19 you can use [java.util.Objects](https://developer.android.com/reference/java/util/Objects.html)) and different equals...called equal instead. – Andrea Richiardi Apr 07 '14 at 08:38
  • @Kap I don't see "Objects" on the path you've mentioned. Are you sure it's available ? In fact, I can see the content-assist trying to suggest that I'll use "java.util.Objects" ... – android developer Apr 30 '14 at 22:01
19

You just need 1.6 present on your PC. I had the same problem. Install 1.6 JDK, and add it as known JDK, but don't actually select it for your project.

Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
  • 1
    So that didn't work. I added the JDK 1.6 to the Installed JRE's tab, but I still get the error. I that what you meant by adding it to the known JDK? – eBehbahani Dec 20 '11 at 16:41
  • Yes, that's exactly what I ment, since I ran into the very same problem and it worked for me. – Tomislav Markovski Dec 20 '11 at 23:12
  • 3
    Right I guess my problem was that I wanted to use JRE 1.7 but can't since the android SDK does not support it. – eBehbahani Dec 20 '11 at 23:39
12

It's worth to mention that if you want compile project with API 21 (Android 5.0 Lollipop) you have to use JDK 1.7.

API 21 won't work with JDK 1.6 anymore.

klimat
  • 24,711
  • 7
  • 63
  • 70
  • yeah I just discovered this when I went to try and set the Material theme within `/res/values-v21/styles.xml`. Solution: installed Java 7u72 from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html then run `/usr/libexec/java_home -v 1.7` (on mac terminal) to get the path, then in And.Studio, right-click your project -> Open Module Settings -> SDK Location -> update the JDK Location to what the java_home command showed – Someone Somewhere Dec 24 '14 at 00:33