Shutting down in the Android Support Library?

So, I transferred the Android Studio project to Java 8, Android API 24 and Jack toolchain today to test new features, especially lambdas and CompletableFuture .

Unfortunately, CompletableFuture seems to be available only from API level 24 (my minimum API level for this project is 16).

Are you aware of any plans to bring CompletableFuture to the Android support library? This seems like a nice solution for the Promises template.

+13
source share
4 answers

The streamsupport project provides streamsupport-cfuture CompletableFuture in its streamsupport-cfuture that can be used for Android development, supported on all devices.

Either use

 dependencies { compile 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.0' } 

or a more modern fork of Android Retrofuture for Android Studio 3.x

 dependencies { compile 'net.sourceforge.streamsupport:android-retrofuture:1.7.0' } 

if you can use Android Studio 3.x.

New Java 12 exception handling methods for CompletableFuture JDK-8211010 have been integrated into release 1.7.0

+15
source

The streamsupport library mentioned in Stefan Zobel, the answer was forked specifically for Android Studio> = 3.0 desugar toolchain, please check android-retrofuture

+5
source

Related and possibly useful for you: Java: optimizing your application using asynchronous programming

This answer concerns CompletableFuture in Java 7 using the library mentioned in the comment above, and not on Android. However, the lib documentation says that it runs on Android. I did not use it myself, though.

+4
source

If you do not need all the functions of the CompletableFuture (for example, the result chain), you can use this class (Kotlin):

 /** * A backport of Java 'CompletableFuture' which works with old Androids. */ class CompletableFutureCompat<V> : Future<V> { private sealed class Result<out V> { abstract val value: V class Ok<V>(override val value: V) : Result<V>() class Error(val e: Throwable) : Result<Nothing>() { override val value: Nothing get() = throw e } object Cancel : Result<Nothing>() { override val value: Nothing get() = throw CancellationException() } } /** * Offers the completion result for [result]. * * If this queue is not empty, the future is completed. */ private val completion = LinkedBlockingQueue<Result<V>>(1) /** * Holds the result of the computation. Takes the item from [completion] upon running and provides it as a result. */ private val result = FutureTask<V> { completion.peek()!!.value } /** * If not already completed, causes invocations of [get] * and related methods to throw the given exception. * * @param ex the exception * @return 'true' if this invocation caused this CompletableFuture * to transition to a completed state, else 'false' */ fun completeExceptionally(ex: Throwable): Boolean { val offered = completion.offer(Result.Error(ex)) if (offered) { result.run() } return offered } /** * If not already completed, completes this CompletableFuture with * a [CancellationException]. * * @param mayInterruptIfRunning this value has no effect in this * implementation because interrupts are not used to control * processing. * * @return 'true' if this task is now cancelled */ override fun cancel(mayInterruptIfRunning: Boolean): Boolean { val offered = completion.offer(Result.Cancel) if (offered) { result.cancel(mayInterruptIfRunning) } return offered } /** * If not already completed, sets the value returned by [get] and related methods to the given value. * * @param value the result value * @return 'true' if this invocation caused this CompletableFuture * to transition to a completed state, else 'false' */ fun complete(value: V): Boolean { val offered = completion.offer(Result.Ok(value)) if (offered) { result.run() } return offered } override fun isDone(): Boolean = completion.isNotEmpty() override fun get(): V = result.get() override fun get(timeout: Long, unit: TimeUnit): V = result.get(timeout, unit) override fun isCancelled(): Boolean = completion.peek() == Result.Cancel } 
0
source

All Articles