5

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

laochiv
  • 2,433
  • 1
  • 15
  • 17
  • Did you solve the problem now? – Display Name May 04 '14 at 17:21
  • Actually I gave up trying. I changed all of those places in the code back to be JRE6 compliant and am just living with it. I would love to get it working if I can, but I can't afford to rathole on it. – laochiv May 05 '14 at 17:56
  • I did solve this. BTW, if all other methods fail and you are too lazy to figure out why, there's a sledgehammer-like solution: http://stackoverflow.com/a/21051506/1418097 – Display Name May 05 '14 at 18:23
  • For me it was as simple as adding `sourceCompatibility = 1.7` after `apply plugin: "java"` in `build.gradle` *for exactly that project that needed this* (I was using multi-module setup) If you have 1 global `build.gradle` and several other `build.gradle` files for individual projects, the latter have higher precedence. – Display Name May 05 '14 at 18:26
  • possible duplicate of [How to set -source 1.7 in Android Studio and Gradle](http://stackoverflow.com/questions/17637179/how-to-set-source-1-7-in-android-studio-and-gradle) – naXa stands with Ukraine Aug 18 '14 at 07:47

1 Answers1

3

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" />
simonp
  • 2,827
  • 1
  • 14
  • 19