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
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With