5

When using Gradle to compile my Android applications, how do I determine which Java version to use? For instance, some snippets of my gradle build file look like:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    // Other settings

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 15
        targetSdkVersion 22

        // Other default settings
    }
}

Java SE 7 is supported for android SDK version 19 and higher, that is for KitKat and above. Given the above snippet, how do I determine whether I should use Java SE 7 or Java SE 6 for compilation and source code compatibility?

Patrick W
  • 1,485
  • 4
  • 19
  • 27

1 Answers1

2

Neither the minSdkVersion nor the targetSdkVersion are used to determine the source code compatibility for compilation. The determinant for source compatibility and compilation is both the compileSdkVersion and buildToolsVersion.

I used a gradle file with the following values:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    // Other settings

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 8
        targetSdkVersion 23

        // Other default settings
    }
}

A snippet of the code specifically used for test is:

public void onCreate(Bundle bundle) {

    ...

    List<String> strings = new ArrayList<>();
    strings.add("John Doe");
    strings.add("Jane Doe");
    strings.add("Bill Gates");
    strings.add("Mark Zuckerberg");

    ListView listView = (ListView) findViewById(R.id.list);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_gallery_item, strings);
    listView.setAdapter(adapter);

    ...
}

When you change the build tools to buildToolsVersion = '17' you get an error message:

The SDK Build Tools revision (17.0.0) is too low for project ':app'. Minimum required is 19.1.0

When you change this to buildToolsVersion = '19.1.0', the build is successful.

When you set the compileSdkVersion to any value less than the buildToolsVersion major value, build fails, mainly due to missing resource dependencies.

Setting the minSdkVersion and targetSdkVersion to any version below the compileSdkVersion has no effect.

P.S. 1. Major value is the first integer before the first dot. e.g. buildToolsVersion '23.0.2' has major value is 23.

P.S. 2. I tested the results of a successful build with a real device with Android API Level 10, (Gingerbread 2.3.6). I set source code compatibility to JDK 7, and used the Diamond Operator introduced in Java 7.

Community
  • 1
  • 1
Patrick W
  • 1,485
  • 4
  • 19
  • 27