5

I'm using Eclipse. If I change the compliance level of the main (Android) project from 1.6 to 1.7, I get an error:

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

If I change the compliance level of the "library" (Java) project from 1.7 to 1.6, a lot of the code in it becomes invalid.

Is going through the code of the Java project and fixing every error my single option here?

NoToast
  • 98
  • 5
  • 15
  • Might want to see [these SO answers](http://stackoverflow.com/questions/8578441/can-the-android-sdk-work-with-jdk-1-7) – codeMagic Aug 23 '13 at 20:28
  • The only thing I'm getting from that is that there's no solution to my problem yet – NoToast Aug 31 '13 at 13:00

6 Answers6

1

Google just released Eclipse ADT 22.6 which adds support for Java 7 language features, http://developer.android.com/tools/sdk/eclipse-adt.html. Once you upgrade to to the latest version of the plugin the Compiler compliance level will include the 1.7 option.

Matt Accola
  • 4,090
  • 4
  • 28
  • 37
0

Try to clean your project, and fix android properties. Link here and here

keybee
  • 1,498
  • 20
  • 32
  • 1
    Fix android properties just reverts it to 1.6. – NoToast Aug 23 '13 at 20:47
  • Does cleaning project properties make anything about the errors? – keybee Aug 23 '13 at 20:48
  • Obviously, it fixes the errors. I could have done that manually, but it doesn't help me with my incompatibility issue. Apparently the main project and the referenced project need to be on the same compliance level for the whole thing to work. – NoToast Aug 23 '13 at 20:53
  • Can't you set 1.6 for both? – keybee Aug 23 '13 at 20:56
  • 1
    > If I change the compliance level of the "library" (Java) project from 1.7 to 1.6, a lot of the code becomes invalid. I guess I'll just fix them manually, most of them are cast errors. – NoToast Aug 23 '13 at 20:57
  • I thought Android environment required Java-6? – NoBugs Sep 03 '13 at 06:29
0

I guess you can find your answer here: Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties

If that doesn't help, I'd suggest to update all android's sdk plugins. I've had similar issue, after updating android's sdk, but I've missed to update android build tools and that caused lot of errors - could be the same with you.

Community
  • 1
  • 1
ZZ 5
  • 1,744
  • 26
  • 41
0

You can use jar builded with Java7 but only in case if it is not using any special features of java7, otherwise dx wont be able to convert jvm byte code to dalvic format.

But there is a one problem: default build process of android sdk (from eclipse e.g.) fails if it finds classes with wrong version (compiled with java7, dx generates warning and fails). You should pass some argument to dx to drop such warnings. Android studio's and Idea's build process doesnt care about this warnings and dx tries co convert java byte code to dalvic despite class version.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
0

Use IntelliJ IDEA instead of Eclipse. In IDEA you can set the Product language level (in Project StructureProject) separately from the Android SDK's. As long as your code only uses 1.7 features that don't affect the bytecode, it'll work (but the compiler will give you warnings). This way, you can use diamonds, switch on strings, and other syntactic sugar, but you might be out of luck with multi-catch. In fact, you can even use experimental 1.8 features like annotations in strange places, but I haven't tested lambdas yet (since IDEA abbreviates the code anyway). See the tag for more info.

Since Android Studio is a special build of IntelliJ IDEA, it might have the same feature, but I haven't tried in that.

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
  • I'll accept this, seems like a good way to do it. Any idea if I can also use pre-compiled .class files that dx fails to convert with "bad class file magic" errors? I assume it's impossible to use those, but it's worth asking. – NoToast Sep 03 '13 at 16:31
  • Not that I know of. My workaround works with precompiled class files, but only if they don't use any new features in the bytecode (i.e. they only use switch-on-strings, etc.) – Dan Hulme Sep 03 '13 at 16:44
  • In Android Studio 2.3.3, the Product Language Level option is not in Project Structure → Project – Paul Wintz Sep 22 '17 at 21:27
0

If your library really needs java 7, there is nothing you can do.

Otherwise, go to your library project settings, go to the Java Compiler settings, and select 1.6 .class files compatibility.

If there are errors in your source files, it means your library uses java 7 functions that cannot be compiled in java 6 bytecode. You're screwed.

Otherwise, you can now export a .jar file that you can use in your android project.

njzk2
  • 38,969
  • 7
  • 69
  • 107