Tho Nguyen Tho Nguyen - 1 month ago 16
Java Question

Android Import java.util.StringJoiner error

I tried to import

java.util.StringJoiner
but I received this message

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.


i'm using :

java version "1.8.0_102"

Java(TM) SE Runtime Environment (build 1.8.0_102-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

How to solve this problem. Thank you all for helping me!!

Answer

StringJoiner was added in API Level 24. If your minSdkVersion is 24 or higher (i.e., will only run on Android 7.0+), you are welcome to use it. If your minSdkVersion is lower than 24, either replace your use of StringJoiner entirely or only use it on devices running API Level 24 or higher.

The specific message that you are getting is because not only was this class introduced in API Level 24, but it came from Java 8. Older devices do not support Java 8 classes.

Also, Java 8 functionality requires the Jack compiler, which at the present time is not the default compiler. You will need to follow the instructions to enable Java 8 support in Gradle, adding the jackOptions and compileOptions closures:

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
    }
}
Comments