Skip to content

Commit 31adb2f

Browse files
committed
Use a newer Android framework in compile-time
Fixes #4158
1 parent 643c1aa commit 31adb2f

4 files changed

Lines changed: 14 additions & 23 deletions

File tree

gradle.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ jdk_toolchain_version=11
3232
animalsniffer_version=2.0.0
3333

3434
# Android versions
35-
android_version=4.1.1.4
35+
android_for_testing_version=4.1.1.4
36+
# We're using a non-default Android artifact for building.
37+
# See https://github.com/Kotlin/kotlinx.coroutines/issues/4158#issuecomment-2758057691
38+
android_via_robolectric_version=9-robolectric-4799589
3639
androidx_annotation_version=1.1.0
3740
robolectric_version=4.9
3841
baksmali_version=2.2.7

ui/kotlinx-coroutines-android/android-unit-tests/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project.configureAar()
33
dependencies {
44
configureAarUnpacking()
55

6-
testImplementation("com.google.android:android:${version("android")}")
6+
testImplementation("com.google.android:android:${version("android_for_testing")}")
77
testImplementation("org.robolectric:robolectric:${version("robolectric")}")
88
// Required by robolectric
99
testImplementation("androidx.test:core:1.2.0")

ui/kotlinx-coroutines-android/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ project.configureAar()
1111
dependencies {
1212
configureAarUnpacking()
1313

14-
compileOnly("com.google.android:android:${version("android")}")
14+
compileOnly("org.robolectric:android-all:${version("android_via_robolectric")}")
1515
compileOnly("androidx.annotation:annotation:${version("androidx_annotation")}")
1616

17-
testImplementation("com.google.android:android:${version("android")}")
17+
testImplementation("com.google.android:android:${version("android_for_testing")}")
1818
testImplementation("org.robolectric:robolectric:${version("robolectric")}")
1919
// Required by robolectric
2020
testImplementation("androidx.test:core:1.2.0")

ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.view.*
77
import androidx.annotation.*
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.internal.*
10-
import java.lang.reflect.*
1110
import kotlin.coroutines.*
1211

1312
/**
@@ -77,27 +76,16 @@ public fun Handler.asCoroutineDispatcher(name: String? = null): HandlerDispatche
7776
private const val MAX_DELAY = Long.MAX_VALUE / 2 // cannot delay for too long on Android
7877

7978
@VisibleForTesting
80-
internal fun Looper.asHandler(async: Boolean): Handler {
79+
internal fun Looper.asHandler(async: Boolean): Handler = when {
8180
// Async support was added in API 16.
82-
if (!async || Build.VERSION.SDK_INT < 16) {
83-
return Handler(this)
84-
}
85-
86-
if (Build.VERSION.SDK_INT >= 28) {
87-
// TODO compile against API 28 so this can be invoked without reflection.
88-
val factoryMethod = Handler::class.java.getDeclaredMethod("createAsync", Looper::class.java)
89-
return factoryMethod.invoke(null, this) as Handler
90-
}
91-
92-
val constructor: Constructor<Handler>
93-
try {
94-
constructor = Handler::class.java.getDeclaredConstructor(Looper::class.java,
95-
Handler.Callback::class.java, Boolean::class.javaPrimitiveType)
96-
} catch (ignored: NoSuchMethodException) {
81+
!async || Build.VERSION.SDK_INT < 16 -> Handler(this)
82+
Build.VERSION.SDK_INT >= 28 -> Handler.createAsync(this)
83+
else -> try {
84+
Handler(this, null, true)
85+
} catch (ignored: NoSuchMethodError) {
9786
// Hidden constructor absent. Fall back to non-async constructor.
98-
return Handler(this)
87+
Handler(this)
9988
}
100-
return constructor.newInstance(this, null, true)
10189
}
10290

10391
@JvmField

0 commit comments

Comments
 (0)