【Android】 Android使用Java 8 語言功能注意事項

weixin_34402408發表於2019-02-25

Android studio 3.0以前,如果我們要使用Java 8的語言功能如Lamb表示式,我們需要這麼配置:
工程的build.gradle裡面配置如下:

buildscript {
  ...
   dependencies {
      // Remove the following dependency.
      classpath 'me.tatarka:gradle-retrolambda:<version_number>'
   }
}

app或者每個模組中的 build.gradle配置如下:

// Remove the following plugin.
apply plugin: 'me.tatarka.retrolambda'
...
// Remove this block after migrating useful configurations.
retrolambda {
    ...
    // If you have arguments for the Java VM you want to keep,
    // move them to your project's [gradle.properties](https://docs.gradle.org/current/userguide/build_environment.html) file.
    jvmArgs '-Xmx2048m'
}</pre>

可能還會配置如下的:

android {
    ...
    defaultConfig {
        ...
        // Remove this block.
        jackOptions {
            enabled true
            ...
        }
    }

    // Keep the following configuration in order to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Android studio升級到3.0以後以上統統不需要了,只需要在app或者每個module裡面做如下配置:

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

參考連結:
1.https://developer.android.com/studio/write/java8-support?utm_source=android-studio

相關文章