Android导入java.util.StringJoiner错误

时间:2016-10-09 10:14:40

标签: java android android-studio

我尝试导入java.util.StringJoiner,但收到了此消息

Usage of API documented as @since 1.8+ less... (⌘F1) This inspection finds all usages of methods that have @since tag in their documentation. This may be useful when development is performed under newer SDK version as the target platform for production.

我正在使用:

java版“1.8.0_102”

Java(TM)SE运行时环境(版本1.8.0_102-b14)

Java HotSpot(TM)64位服务器VM(版本25.102-b14,混合模式)

如何解决这个问题。谢谢大家的帮助!!

1 个答案:

答案 0 :(得分:0)

在API级别24中添加了

StringJoiner。如果您的minSdkVersion为24或更高(即,只能在Android 7.0及更高版本上运行),欢迎您使用它。如果您的minSdkVersion低于24,请完全替换StringJoiner的使用,或仅在运行API级别24或更高级别的设备上使用它。

您获得的具体消息是因为不仅在API Level 24中引入了此类,而且它来自Java 8.较旧的设备不支持Java 8类。

此外,Java 8功能需要Jack编译器,它目前不是默认编译器。您需要关注the instructions to enable Java 8 support in Gradle,添加jackOptionscompileOptions关闭:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "com.commonsware.myapplication"
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        jackOptions {
            enabled true
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}