Call requires API level 26 (current min is 23): java.time.Instant#now
Asked Answered
I

5

20

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

Individualize answered 6/4, 2019 at 16:9 Comment(2)
Is there a solution for this yes. Update minSdk or use something else than java.time.InstantIlarrold
@MarcinOrlowski A better solution is to add the ThreeTenABP library to your Android project to get a back-port of most of the java.time functionality. See the Answer by Ole V.V..Overthrow
C
3

After reviewing the developer guide, it turned out that either String now,

as well as common int UNIX epoch timestamps, are being acceptable values:

departure_time — Specifies the desired time of departure. You can specify the time as an integer in seconds since midnight, January 1, 1970 UTC. Alternatively, you can specify a value of now, which sets the departure time to the current time (correct to the nearest second).

When checking the source code of the Java client, there is a convenience method for that:

public DirectionsApiRequest departureTimeNow() {
    return param("departure_time", "now");
}

Therefore java.time.Instant can be circumvented, for the backwards compatibility.


I've filed issue #559... forking the library seems to be the only way to set other timestamps.

Cutcliffe answered 7/4, 2019 at 6:24 Comment(0)
U
41

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:

Unceasing answered 14/4, 2020 at 22:3 Comment(6)
perfect. also it lets to use other useful Apis like streamAzevedo
Not working on AS 4.2 Beta 6. Build fails with following Gradle 6.4.1 / AGP 4.0.2 exception: > No signature of method: build_dvug2jv3jezfh5oc48zh5hqe.android() is applicable for argument types: (build_dvug2jv3jezfh5oc48zh5hqe$_run_closure1) values: [build_dvug2jv3jezfh5oc48zh5hqe$_run_closure1@752d785a]Andromeda
For follow-up, issue submitted: github.com/StylingAndroid/Time/issues/1Andromeda
That link is broken.Stanchion
Java 8+ API desugaring support: developer.android.com/studio/write/java8-supportDew
Remember to use the right version, based on your Gradle plugin version. Read more here: developer.android.com/studio/write/…Deodorize
C
3

After reviewing the developer guide, it turned out that either String now,

as well as common int UNIX epoch timestamps, are being acceptable values:

departure_time — Specifies the desired time of departure. You can specify the time as an integer in seconds since midnight, January 1, 1970 UTC. Alternatively, you can specify a value of now, which sets the departure time to the current time (correct to the nearest second).

When checking the source code of the Java client, there is a convenience method for that:

public DirectionsApiRequest departureTimeNow() {
    return param("departure_time", "now");
}

Therefore java.time.Instant can be circumvented, for the backwards compatibility.


I've filed issue #559... forking the library seems to be the only way to set other timestamps.

Cutcliffe answered 7/4, 2019 at 6:24 Comment(0)
R
2

You can use the Instant class and other classes from java.time on Android API level 23

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Repartition answered 25/10, 2019 at 10:56 Comment(0)
D
1

I wrote about the recommended approach to solve this issue here: https://dev.to/cicerohellmann/what-is-desugaring-and-why-would-i-need-it-2bo0

But briefly you should use desugaring,it allows you to use some high level apis in lower levels. Worth checking. I also added all the links to find ho to implement it and some goods and bads about it

Duisburg answered 7/8, 2020 at 13:14 Comment(0)
P
-5

There is no other way to do that. You can't use an API which requires a higher SDK version with a lower one.

You should change minSdkVersion property in the app level build.gradle from 23 file to 26.

Partnership answered 6/4, 2019 at 16:17 Comment(1)
Answers like this do not take into consideration the OP's constraints. Changing the supported minSdkVersion is 'simple' to do technically but there are many non-technical reasons why this may not be so easy to do.Unceasing

© 2022 - 2024 — McMap. All rights reserved.