Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - Call requires API level 26 (current min is 23): java.time.Instant#now

I'm implementing Google's Directions API. My app supports minSdkVersion 23 and java.time.Instant is not supported here.

Is there a solution for this, or should i just check for user's version and allow this functionality if it's version is supported?

    DirectionsResult directionsResult = DirectionsApi.newRequest(geoApiContext)
            .mode(TravelMode.DRIVING)
            .origin(new com.google.maps.model.LatLng(mapFragment.getUserCoords().latitude, mapFragment.getUserCoords().longitude))
            .destination(tabHandlerCommunication.destinationBarCoords)
            .departureTime(Instant.now()) // <-- error here
            .await();

Error:

Call requires API level 26 (current min is 23): java.time.Instant#now less... (Ctrl+F1) Inspection info:This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest). If you really want to use this API and don't need to support older devices just set the minSdkVersion in your build.gradle or AndroidManifest.xml files. If your code is deliberately accessing newer APIs, and you have ensured (e.g. with conditional execution) that this code will only ever be called on a supported platform, then you can annotate your class or method with the @TargetApi annotation specifying the local minimum SDK to apply, such as @TargetApi(11), such that this check considers 11 rather than your manifest file's minimum SDK as the required API level. If you are deliberately setting android: attributes in style definitions, make sure you place this in a values-vNN folder in order to avoid running into runtime conflicts on certain devices where manufacturers have added custom attributes whose ids conflict with the new ones on later platforms. Similarly, you can use tools:targetApi="11" in an XML file to indicate that the element will only be inflated in an adequate context. Issue id: NewApi

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're using Gradle plugin 4.0 or later (with Android Studio 4.0 or later), you can take advantage of D8 Core Library Desugaring. This includes a subset of the functionality found in java.time and will allow you to use java.time.Instant in your project; even if you need to support versions older than API 26.

In your module's build.gradle file:

android {

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        coreLibraryDesugaringEnabled true
    }

    // If using Kotlin
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }
}
dependencies {
    …
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.5'
}

You should now be able to use this class error free.

Some sources:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...