0

When building an Android application, which Java version should I compile the code for? I'd guess this depends on the minSdkVersion? And if so, where can I find the required Java version for each minSdkVersion?

And once I determine the Java version I want, how do I set it? I build my project using Ant (from the command line). Currently it seems to always use 1.5 because the build.xml file imports ${sdk.dir}/tools/ant/build.xml which contains:

<property name="java.target" value="1.5" />
<property name="java.source" value="1.5" />
...

<javac ... source="${java.source}" target="${java.target}" ... >

Should I change it there? Is there a way to set it per project?

And is there a way to set the Java version if I build using Eclipse?

Iamat8
  • 3,888
  • 9
  • 25
  • 35
Yaron Cohen-Tal
  • 2,005
  • 1
  • 15
  • 26

2 Answers2

2

In Eclipse, just right-click on the project, then Properties|Java Compiler. Then choose the desired java compliance level.

Look at this question about the relation between the Android SDK and the Java runtime version. As reported there:

With Android KitKat (buildToolsVersion 19 you can use 1.7 tools.android.com/tech-docs/new-build-system/

Community
  • 1
  • 1
onof
  • 17,167
  • 7
  • 49
  • 85
  • Thanx. Is there a way to see which target Java version is used to build the project in Eclipse, to make sure that it does indeed compile to the version I want? – Yaron Cohen-Tal Aug 26 '15 at 11:48
  • 1
    I looked at your link and I still can't find an answer to the question: if I develop for Android API level `X`, which Java version should I use? I see that version 6 was supported, and at some point version 7 was supported too. Since which Android API level is Java 7 supported? And where can I find current information about the connection between Android API level and the Java version? – Yaron Cohen-Tal Aug 26 '15 at 11:51
  • With Android KitKat (buildToolsVersion 19 you can use 1.7 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1.7 – onof Aug 26 '15 at 13:06
  • Thanx! Perhaps u can add your comment to your answer, and then I can formally "accept" it. – Yaron Cohen-Tal Aug 26 '15 at 16:40
  • @YaronCohen-Tal I added the comment. Regards – onof Aug 31 '15 at 10:24
0

Seems like I found an answer at least to one of my questions: to change the Java version for Ant (when used from the command line) I created a file "ant.properties" at the top level directory of my project with the contents:

java.target=1.7
java.source=1.7

(to change to Java 7). I don't know, though, if it also affects building inside Eclipse.

Yaron Cohen-Tal
  • 2,005
  • 1
  • 15
  • 26