如何为android6配置retrolambda(API23)

时间:2017-09-26 10:03:15

标签: android retrolambda

我想为我的项目添加Java8支持。我找到的唯一合适的解决方案是retrolambda gradle插件项目。这是我的相对项目和模块的build.gradle文件。

注意:请不要建议迁移到Android Studio 3,因为我想了解它为什么无效。

项目build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'me.tatarka:gradle-retrolambda:3.7.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

module build.gradle

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

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.test.test"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

retrolambda {
    javaVersion JavaVersion.VERSION_1_7
    defaultMethods true
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

应用程序本身非常简单。它只有一个默认活动,使用Java8代码,如下所示:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<String> strings = new ArrayList<>();
        strings.add("Hello");
        strings.add("World");
        strings.stream().forEach(System.out::println);
    }
}

启动应用程序时出现错误:

  

致命的例外:主要                                                                      处理:com.slesh.mtbbank,PID:25338                                                                      java.lang.NoSuchMethodError:没有接口方法   流()Ljava / util的/流/流;在类Ljava / util / List中;或者它的   超级类(声明&#39; java.util.List&#39;出现在   /system/framework/core-libart.jar)                                                                          在com.slesh.mtbbank.MainActivity.onCreate(MainActivity.java:19)                                                                          在android.app.Activity.performCreate(Activity.java:6303)                                                                          在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)                                                                          在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)                                                                          在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)                                                                          在android.app.ActivityThread.access $ 1000(ActivityThread.java:153)                                                                          在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1373)                                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                                          在android.os.Looper.loop(Looper.java:154)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5529)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

错过了什么?任何想法为什么它不起作用?

提前致谢!

1 个答案:

答案 0 :(得分:3)

"mysql:host=$this->host;dbname=$this->dbname"

Retrolambda只是它的名字所暗示的:它允许你使用lambdas。它不支持流。有些库可以反向传输流API,但是旧版本不会。