在Android中引入Java8的lambda表達式


我用的是retrolambda這個插件,下面來說下如何添加它。

項目地址:https://github.com/evant/gradle-retrolambda

在根項目的build.gradle中添加這個classpath

classpath 'me.tatarka:gradle-retrolambda:3.2.0'//retrolambda

example:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'me.tatarka:gradle-retrolambda:3.2.0'//retrolambda
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

然后,在項目的bulid.gradle中添加插件,並讓其使用java8

apply plugin: 'com.android.application' apply plugin: 'me.tatarka.retrolambda'

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion

    defaultConfig {
        applicationId "kale.androidframework"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode Integer.parseInt(project.VERSION_CODE)
        versionName project.VERSION_NAME
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

    compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
}

retrolambda {
    javaVersion JavaVersion.VERSION_1_7
}

//設置編碼  
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

如果你用到了多個lib庫,需要在每個lib中進行添加:

apply plugin: 'me.tatarka.retrolambda'

android {


  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM