LoginSignup
8
8

More than 5 years have passed since last update.

Android Gradle Plugin で JDK Version が自動的に設定される

Last updated at Posted at 2015-10-06

悩んだ点

Android Studio で apk などをビルドするとき、build.gradle に、Java JDK のバージョンを指定する場合がありますが、指定しない(android.compileOptions を指定しない)場合は、Android Gradle Plugin がよしなに処理します。

    • compileSdkVersion を 19 にする -> JDK 1.6 が設定される
    • compileSdkVersion を 23 にする -> JDK 1.7 が設定される

どこでよしなにやっているのかが分からなかったので調べました。

AbstractCompilesUtil.java

AbstractCompilesUtil.java
JavaVersion javaVersionToUse;
if (compileSdkLevel == null || (0 <= compileSdkLevel && compileSdkLevel <= 20)) {
    javaVersionToUse = JavaVersion.VERSION_1_6;
} else {
    javaVersionToUse = JavaVersion.VERSION_1_7;
}

つまるところ、Google としては、compileSdkVersion が API20 以下だと JDK1.6、API21からは JDK 1.7 でコンパイルすることをデフォルトとしている、ということになります。ちなみに CompileOptions クラスの JDK のデフォルト値は JavaVersion.VERSION_1_6 になっています。

おまけ

gradle(w) 実行時に、--debug オプションを付けるとわかるのですが、ログに -source, -target が出ないことがあります。

  • 例:JDK1.7 環境 + compatibility を 1.6 にする

[DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler] Compiler arguments: -source 1.7 -target 1.7 -d ...

  • 例:JDK1.7 環境 + compatibility を 1.7 にする

[DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler] Compiler arguments: -d ...

つまり、ビルド環境の JDK のバージョンと、compatibility のバージョンが一致していた場合は、-source, -target がログに出ないことになります。こちらは Gradle Java Plugin の仕様のようです。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8