2016-05-04

讓 Android Studio 2.1 支援 Lambda expression & Stream API & ButterKnife依賴注入框架


要讓 Android Studio 2.1 能支援java 8的Lambda expression & Stream API & ButterKnife依賴注入框架,
請先安裝JDK8,並修改專案gradle設定檔如下:

gradle.build (Project):

buildscript {
    repositories {
        jcenter()
        //加入套件來源maven
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        // 加入dependency (gradle-retrolambda 外掛)
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        // 加入dependency (android-apt 外掛)
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

repositories {
    //加入maven 
    mavenCentral()
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}



gradle.build (Module) :

apply plugin: 'com.android.application'
//加入me.tatarka.retrolambda
apply plugin: 'me.tatarka.retrolambda'
//加入com.neenbedankt.android-apt
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "com.example.user.helloworld"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //指定編譯時原始碼(-source)及編譯碼(-target)皆為java 8版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    //butterknife: lint warning
//    lintOptions {
//        disable 'InvalidPackage'
//    }
    //butterknife: prevent processor conflicts
//    packagingOptions {
//        exclude 'META-INF/services/javax.annotation.processing.Processor'
//    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:support-v13:23.3.0'

    //使用最新的retrolambda版本,預設是用 2.1.0
    //查看最新版本:http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22net.orfjackal.retrolambda%22%20AND%20a%3A%22retrolambda%22
    retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:2.3.0'
    //加入Stream API suppoort,使用:https://github.com/aNNiMON/Android-Java-8-Stream-Example
    compile 'com.annimon:stream:1.0.9'
    //butterknife
    compile 'com.jakewharton:butterknife:8.0.1'
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

//以下視狀況加入 retrolambda 設定
retrolambda {
//    jdk System.getenv("JAVA8_HOME") //jdk8路徑,預設會自行尋找JAVA8_HOME環境變數
//    oldJdk System.getenv("JAVA6_HOME") //若要執行unit test,需指定java 5, 6或7的jdk路徑,預設會找AVA5_HOME/JAVA6_HOME/JAVA7_HOME環境變數
      javaVersion JavaVersion.VERSION_1_7 //決定編譯後的版本, 5,6,7皆可,預設是6
//    jvmArgs '-arg1', '-arg2' // 執行retrolambda時,加入jvm 參數
      defaultMethods true //加入java 8中,讓介面interface可支援default方法及static方法,這會讓incremental = false,預設false
//    incremental true //強制每個檔案讓retorlambda重新處理,而不是只限於改變的檔案,預設true
//    jvmArgs '-noverify' //fix google play 5.0.77 bug
}

proguard-rules.pro

# retrolambda
-dontwarn java.lang.invoke.*

# butterknife
-dontwarn butterknife.internal.**
#-keep class butterknife.** { *; }
#-keep class **$$ViewBinder { *; }
#-keepclasseswithmembernames class * {
#    @butterknife.* <fields>;
#}
#-keepclasseswithmembernames class * {
#    @butterknife.* <methods>;
#}

接下來試試看用java 8中的方法參照及Stream是否正常:
List<String> gg = Arrays.asList(new String[]{"Google", "Microsoft", "Java", "Facebook"});
Collections.sort(gg, String::compareTo);
System.out.println(gg); //[Facebook, Google, Java, Microsoft]

List<String> yy = Stream.of("c", "a", "b")
                .sorted()
                .collect(Collectors.toList());
System.out.println(yy);  //[a, b, c]

沒有留言:

張貼留言