Chỉ cần thiết lập dự án bằng Android Studio 2.2.3 với Java 1.8 và Android 7 (API cấp 24) đang cố gắng thử nghiệm tính năng java 8 "mới" Stream
.Cách sử dụng API Java 8 Stream trong Android 6.0
Ở đây tập gradle tôi:
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'
}
Đây mã:
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 nhấn mạnh dòng myList.stream()...
nói với tôi "Usage of API documented as @since 1.8+
" Gradle được biên dịch tất cả mọi thứ nhưng tại sao tôi nhận được thông báo này? Tôi nghĩ tính năng này được giới thiệu với Java 8 và có sẵn dưới API Android Leve 24 trở lên?
Làm cách nào để giải quyết sự cố này?
Cảm ơn trước
Có thể điều này có thể giúp (thiết lập cấp độ mô-đun lang): http://stackoverflow.com/questions/37787079/intellij-unable-to-use-newer-java-8-classes-error-usage-of-api -documented – Kelo
nếu android studio có tùy chọn này, tôi đã thay đổi nó. đáng buồn là không có tùy chọn như vậy (chỉ biên dịch phiên bản sdk và phiên bản công cụ xây dựng) – Radinator
API Cấp 24 = Nougat, Android 7.0-7.1.1, API Cấp 23 = Marshmallow, Android 6.0 (.1) – Sartorius