2017-03-07 83 views
6

只需使用Android Studio 2.2.3和Java 1.8和Android 7(API Level 24)设置项目,试图测试“新”java 8功能Stream如何在Android 6.0下使用Java 8 Stream API

这里我gradle这个文件:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.example.radinator.myproject" 
     minSdkVersion 24 
     targetSdkVersion 24 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

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:25.2.0' 
    testCompile 'junit:junit:4.12' 
} 

下面的代码:

import java.util.stream.*; 
public class MainActivity extens Activity { 

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

    void foo(){ 
     List<String> myList = Arrays.asList("one", "two", "three", "four", "five", "six"); 
     myList.stream() 
      .filter(s -> s.length() > 0) 
      .collect(Collectors.toList()) 
    } 

} 

Android Studio中强调了线myList.stream()...告诉我 “Usage of API documented as @since 1.8+” 摇篮正在编制的一切,但为什么我得到这个消息?我认为这个特性是在Java 8中引入的,并且可以在Android API Level 24和更高版本中获得?

我该如何解决这个烂摊子?

在此先感谢

+0

也许这可能有助于(设置模块lang级别):http://stackoverflow.com/questions/37787079/intellij-unable-to-use-newer-java-8-classes-error-usage-of-api - 如果安卓工作室有这个选项我已经改变它的文档 – Kelo

+0

。可悲的是没有这样的选择(只编译sdk版本和构建工具版本) – Radinator

+2

API等级24 =牛轧糖,Android 7.0-7。1.1,API Level 23 = Marshmallow,Android 6.0(.1) – Sartorius

回答

8

更新 2017年4月4日

Jack is deprecated,谷歌正与一些所谓的desugar取代它。它现在可用于Android Studio 2.4 preview 4及更高版本。

Java 8语言/库功能的可用性仍然取决于设备API级别和Android Studio版本,因此请确保double check what you can and can't use

要使用它,你只设置源语言和目标语言水平的Java 8

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

请注意,如果您已经使用retrolambda或插孔,则需要禁用它们desugar到使用。你可以找到更多关于如何使用它的信息here

我还没有尝试过,因为我更喜欢IntelliJ IDEA,并且快速研究如何将它与IDEA一起使用并不富有成效。一旦我找出如何在IDEA中使用它,我会再次更新这个答案。

老答案

一些的Java 8语言特性使用杰克编译器时可用。

,使杰克,编辑build.gradle像这样

android { 
    ... 
    defaultConfig { 
     ... 
     jackOptions { 
      enabled true 
     } 
    } 
    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_8 
     targetCompatibility JavaVersion.VERSION_1_8 
    } 
} 

更多信息,可以发现here

不过,我想补充一点,我已经与插孔非常糟糕的经验,到目前为止,特别是在调试时。我会推荐使用streamsupportretrolambda,而不是,直到jack的妹妹Jill发布

+3

杰克从下个月开始不推荐使用。 –

+2

显然,https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html ...好,retrolambda救援...虽然仍然有希望 – Zharf

+1

我一旦我看到新东西,我会尽量记住更新这个答案 – Zharf