Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android switch to gradle doesn't compile JRE7 code features

I moved my android app over to Android Studio without switching to Gradle. Now I want to move to Gradle. The app compiles in Android Studio before switching to Gradle, but now that I have Gradle all set up, it won't compile the String Switch Statements or the diamond operators. The error I am getting is

Gradle: error: strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)

I have made sure that I am running on JRE 7 by printing the

System.getProperty("java.version")

in a task. The output is

1.7.0_25

What confuses me most is the discrepancy between "-source 1.6" and "use -source 7". But I know that both of these are names for Java sdk's so maybe the titles are just being mixed up.

Is there a Gradle setting I need to set? or is this not possible in Gradle? If not it is confusing why it works without Gradle.

It should be noted that the without Gradle version of my project runs the default Android Studio build. I didn't write an ant script or maven script for building it. One of those may be the way it is being built, but I don't have any project specific files for them. Just the Android Studio .iml files.

UPDATE I tried adding the following to the build.gradle android{} section

compileOptions {
   sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
   targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
}

but the .class files failed to build and it weren't included in the apk. See the "Android Projects Need Libraries Compiled with Java 1.6" section on this post

like image 830
laochiv Avatar asked Oct 20 '22 22:10

laochiv


1 Answers

You can upgrade an existing Android app/library module to Java 7 by adding the following in the android section of your build.gradle:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

For a Java (non-android) module, you should add the following to build.gradle just after apply plugin: 'java':

sourceCompatibility = 1.7
targetCompatibility = 1.7

For both types of modules, you will need to manually change the language level of your project in File -> Project Structure -> Project (or you can manually edit the config in .idea/misc.xml from JDK_1_6 to JDK_1_7).

You might also need to add the following to .idea/compiler.xml, in the <component name="CompilerConfiguration"> block (but see how you get on without it first):

<bytecodeTargetLevel target="1.7" />
like image 93
simonp Avatar answered Oct 27 '22 09:10

simonp