SamAko
SamAko

Reputation: 3615

What does the language level setting in android studio 0.3.2 Do?

In the latest release of Android Studio (0.4.2), a new setting has been added when creating a new project, namely Language Level I want to know what that refers to, and the consequence of selecting any of the provided options. The doc doesn't give me a clear picture of this and i don't want to select an option when i don't know what that will result in? Thanks

Upvotes: 3

Views: 6493

Answers (3)

Kishan Vaghela
Kishan Vaghela

Reputation: 7938

Java 7 support was added at build tools 19. You can use now features like the diamond operator, multi-catch, try-with-resources, strings in switches, etc. Add the folowing to your build.gradle.

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

defaultConfig {
    minSdkVersion 7
    targetSdkVersion 19
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

Gradle 1.7+, Android gradle plugin 0.6.+ are required.

Note, that only try with resources require minSdkVersion 19. Other features works on previous platforms.

Upvotes: 0

Nick Westgate
Nick Westgate

Reputation: 3273

The answer can be phrased better, IMHO. The docs are clear about Java 7 and below.

  1. This setting affects which Java language features you can use in your source code.
  2. All Java language features that you use will be compiled so that the code will work in all Android versions, except for the "try with resources" language feature.
  3. From the docs: "If you want to use try with resources, you will need to also use a minSdkVersion of 19. You also need to make sure that Gradle is using version 1.7 or later of the JDK. (And version 0.6.1 or later of the Android Gradle plugin.)"

Upvotes: 1

zapl
zapl

Reputation: 63955

It's about what Java language level you want to use. KitKat supports full Java 7, Gingerbread and up support Java 6 and older versions are Java 5. At least when it comes to the core Java APIs. Roughly the same setting is mentioned here.

You can often use language features that were added in one version in an older version if the compiler knows how to do it. For example the Diamond Operator was added in Java 7 but you can still use that feature in Java 6 since

List<String> list = new ArrayList<>();

actually compiles into the same thing as

List<String> list = new ArrayList<String>();

did.

The "try with resource" construct on the other hand can't be compiled into legal Java 6 compatible code and is therefore exclusive to Apps that require KitKat and up.

Upvotes: 3

Related Questions