简体   繁体   中英

How do I use a Java project that requires a 1.7 compliance level on an Android project build path?

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?

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.

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

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.

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 eg) 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM