Skip to content

Commit 6baf7c8

Browse files
authored
Restore Android compatibility in Executor.asCoroutineDispatcher (#4396)
An Android compatibility trick was removed in #4196, and animal-sniffer was misconfigured and didn't catch that. With this change, animal-sniffer works once again, and the trick is restored.
1 parent dbca4c1 commit 6baf7c8

17 files changed

+110
-86
lines changed

build.gradle.kts

+13-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,19 @@ allprojects {
9595
}
9696

9797
// needs to be before evaluationDependsOn due to weird Gradle ordering
98-
apply(plugin = "animalsniffer-conventions")
98+
configure(subprojects) {
99+
fun Project.shouldSniff(): Boolean =
100+
platformOf(project) == "jvm" && project.name !in unpublished && project.name !in sourceless
101+
&& project.name !in androidNonCompatibleProjects
102+
// Skip JDK 8 projects or unpublished ones
103+
if (shouldSniff()) {
104+
if (isMultiplatform) {
105+
apply(plugin = "animalsniffer-multiplatform-conventions")
106+
} else {
107+
apply(plugin = "animalsniffer-jvm-conventions")
108+
}
109+
}
110+
}
99111

100112
configure(subprojects.filter { !sourceless.contains(it.name) }) {
101113
if (isMultiplatform) {
@@ -157,4 +169,3 @@ rootProject.registerTopLevelDeployTask()
157169

158170
// Report Kotlin compiler version when building project
159171
println("Using Kotlin compiler version: ${KotlinCompilerVersion.VERSION}")
160-

buildSrc/src/main/kotlin/AuxBuildConfiguration.kt

-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ object AuxBuildConfiguration {
1313
@JvmStatic
1414
fun configure(rootProject: Project) {
1515
rootProject.allprojects {
16-
workaroundForCleanTask()
1716
CacheRedirector.configure(this)
1817
workaroundForCoroutinesLeakageToClassPath()
1918
}
@@ -27,18 +26,6 @@ object AuxBuildConfiguration {
2726
}
2827
}
2928

30-
private fun Project.workaroundForCleanTask() {
31-
// the 'clean' task cannot delete expanded.lock file on Windows as it is still held by Gradle, failing the build
32-
// Gradle issue: https://github.com/gradle/gradle/issues/25752
33-
tasks {
34-
val clean by existing(Delete::class) {
35-
setDelete(fileTree(layout.buildDirectory) {
36-
exclude("tmp/.cache/expanded/expanded.lock")
37-
})
38-
}
39-
}
40-
}
41-
4229
/*
4330
* 'kotlinx-coroutines-core' dependency leaks into test runtime classpath via 'kotlin-compiler-embeddable'
4431
* and conflicts with our own test/runtime incompatibilities (e.g. when class is moved from a main to test),

buildSrc/src/main/kotlin/animalsniffer-conventions.gradle.kts

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id("ru.vyarus.animalsniffer")
3+
}
4+
5+
project.plugins.withType(JavaPlugin::class.java) {
6+
val signature: Configuration by configurations
7+
dependencies {
8+
signature("net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature")
9+
signature("org.codehaus.mojo.signature:java17:1.0@signature")
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id("ru.vyarus.animalsniffer")
3+
}
4+
5+
project.plugins.withType(KotlinMultiplatformConventionsPlugin::class.java) {
6+
val signature: Configuration by configurations
7+
dependencies {
8+
signature("net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature")
9+
signature("org.codehaus.mojo.signature:java17:1.0@signature")
10+
}
11+
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jna_version=5.9.0
2828

2929
# Gradle
3030
jdk_toolchain_version=11
31-
animalsniffer_version=1.7.1
31+
animalsniffer_version=2.0.0
3232

3333
# Android versions
3434
android_version=4.1.1.4

gradle/wrapper/gradle-wrapper.jar

243 Bytes
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
3+
distributionSha256Sum=fba8464465835e74f7270bbf43d6d8a8d7709ab0a43ce1aa3323f73e9aa0c612
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-all.zip
45
networkTimeout=10000
56
validateDistributionUrl=true
67
zipStoreBase=GRADLE_USER_HOME

gradlew

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
1820

1921
##############################################################################
2022
#
@@ -55,7 +57,7 @@
5557
# Darwin, MinGW, and NonStop.
5658
#
5759
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
60+
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5961
# within the Gradle project.
6062
#
6163
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -84,7 +86,7 @@ done
8486
# shellcheck disable=SC2034
8587
APP_BASE_NAME=${0##*/}
8688
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87-
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
8890

8991
# Use the maximum available, or set MAX_FD != -1 to use that value.
9092
MAX_FD=maximum
@@ -203,7 +205,7 @@ fi
203205
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204206

205207
# Collect all arguments for the java command:
206-
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
208+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207209
# and any embedded shellness will be escaped.
208210
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209211
# treated as '${Hostname}' itself on the command line.

gradlew.bat

+12-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
@rem See the License for the specific language governing permissions and
1414
@rem limitations under the License.
1515
@rem
16+
@rem SPDX-License-Identifier: Apache-2.0
17+
@rem
1618

1719
@if "%DEBUG%"=="" @echo off
1820
@rem ##########################################################################
@@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
4345
%JAVA_EXE% -version >NUL 2>&1
4446
if %ERRORLEVEL% equ 0 goto execute
4547

46-
echo.
47-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48-
echo.
49-
echo Please set the JAVA_HOME variable in your environment to match the
50-
echo location of your Java installation.
48+
echo. 1>&2
49+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
50+
echo. 1>&2
51+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
52+
echo location of your Java installation. 1>&2
5153

5254
goto fail
5355

@@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
5759

5860
if exist "%JAVA_EXE%" goto execute
5961

60-
echo.
61-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62-
echo.
63-
echo Please set the JAVA_HOME variable in your environment to match the
64-
echo location of your Java installation.
62+
echo. 1>&2
63+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
64+
echo. 1>&2
65+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
66+
echo location of your Java installation. 1>&2
6567

6668
goto fail
6769

kotlinx-coroutines-bom/build.gradle.kts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication
2+
import java.util.Locale
23

34
plugins {
45
id("java-platform")
@@ -38,7 +39,9 @@ publishing {
3839
forEach { pub ->
3940
pub as DefaultMavenPublication
4041
pub.unsetModuleDescriptorGenerator()
41-
tasks.matching { it.name == "generateMetadataFileFor${pub.name.capitalize()}Publication" }.all {
42+
tasks.matching {
43+
it.name == "generateMetadataFileFor${ pub.name.replaceFirstChar { it.uppercaseChar() } }Publication"
44+
}.all {
4245
onlyIf { false }
4346
}
4447
}

kotlinx-coroutines-core/build.gradle.kts

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import org.gradle.api.tasks.testing.*
22
import org.gradle.kotlin.dsl.*
3+
import org.gradle.kotlin.dsl.withType
34
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
45
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
56
import org.jetbrains.kotlin.gradle.plugin.mpp.*
67
import org.jetbrains.kotlin.gradle.targets.native.tasks.*
78
import org.jetbrains.kotlin.gradle.tasks.*
89
import org.jetbrains.kotlin.gradle.testing.*
10+
import ru.vyarus.gradle.plugin.animalsniffer.AnimalSniffer
911

1012
plugins {
1113
kotlin("multiplatform")
@@ -84,11 +86,6 @@ kotlin {
8486
}
8587
}
8688
}
87-
88-
jvm {
89-
// For animal sniffer
90-
withJava()
91-
}
9289
}
9390

9491
private fun KotlinMultiplatformExtension.setupBenchmarkSourceSets(ss: NamedDomainObjectContainer<KotlinSourceSet>) {
@@ -293,3 +290,19 @@ artifacts {
293290
tasks.named("dokkaHtmlPartial") {
294291
dependsOn(jvmJar)
295292
}
293+
294+
// Specific files so nothing from core is accidentally skipped
295+
tasks.withType<AnimalSniffer> {
296+
exclude("**/future/FutureKt*")
297+
exclude("**/future/ContinuationHandler*")
298+
exclude("**/future/CompletableFutureCoroutine*")
299+
300+
exclude("**/stream/StreamKt*")
301+
exclude("**/stream/StreamFlow*")
302+
303+
exclude("**/time/TimeKt*")
304+
}
305+
306+
animalsniffer {
307+
defaultTargets = setOf("jvmMain")
308+
}

kotlinx-coroutines-core/jvm/src/Executors.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ private class DispatcherExecutor(@JvmField val dispatcher: CoroutineDispatcher)
117117

118118
internal class ExecutorCoroutineDispatcherImpl(override val executor: Executor) : ExecutorCoroutineDispatcher(), Delay {
119119

120+
/*
121+
* Attempts to reflectively (to be Java 6 compatible) invoke
122+
* ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy in order to cleanup
123+
* internal scheduler queue on cancellation.
124+
*/
120125
init {
121-
/* Attempt to invoke ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy in order to clean up
122-
* the internal scheduler queue on cancellation. */
123-
if (executor is ScheduledThreadPoolExecutor) {
124-
executor.removeOnCancelPolicy = true
125-
}
126+
removeFutureOnCancel(executor)
126127
}
127128

128129
override fun dispatch(context: CoroutineContext, block: Runnable) {

kotlinx-coroutines-core/jvm/src/internal/Concurrent.kt

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package kotlinx.coroutines.internal
22

3+
import java.lang.reflect.Method
34
import java.util.*
5+
import java.util.concurrent.Executor
6+
import java.util.concurrent.ScheduledThreadPoolExecutor
47
import kotlin.concurrent.withLock as withLockJvm
58

69
internal actual typealias ReentrantLock = java.util.concurrent.locks.ReentrantLock
@@ -18,3 +21,22 @@ internal actual annotation class BenignDataRace()
1821
@Suppress("NOTHING_TO_INLINE") // So that R8 can completely remove ConcurrentKt class
1922
internal actual inline fun <E> identitySet(expectedSize: Int): MutableSet<E> =
2023
Collections.newSetFromMap(IdentityHashMap(expectedSize))
24+
25+
private val REMOVE_FUTURE_ON_CANCEL: Method? = try {
26+
ScheduledThreadPoolExecutor::class.java.getMethod("setRemoveOnCancelPolicy", Boolean::class.java)
27+
} catch (_: Throwable) {
28+
null
29+
}
30+
31+
/* We can not simply call `setRemoveOnCancelPolicy`, even though the code would compile and tests would pass,
32+
* because older Android versions don't support it. */
33+
@Suppress("NAME_SHADOWING")
34+
internal fun removeFutureOnCancel(executor: Executor): Boolean {
35+
try {
36+
val executor = executor as? ScheduledThreadPoolExecutor ?: return false
37+
(REMOVE_FUTURE_ON_CANCEL ?: return false).invoke(executor, true)
38+
return true
39+
} catch (_: Throwable) {
40+
return false // failed to setRemoveOnCancelPolicy, assume it does not remove the future on cancel
41+
}
42+
}

kotlinx-coroutines-test/build.gradle.kts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import org.jetbrains.kotlin.gradle.plugin.mpp.*
2-
import org.jetbrains.kotlin.gradle.targets.js.dsl.*
1+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
32

43
kotlin {
54
sourceSets {

reactive/kotlinx-coroutines-rx2/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tasks.withType(DokkaTaskPartial::class) {
1818

1919
val testNG by tasks.registering(Test::class) {
2020
useTestNG()
21-
reports.html.outputLocation = file("$buildDir/reports/testng")
21+
reports.html.outputLocation = file("${layout.buildDirectory.get()}/reports/testng")
2222
include("**/*ReactiveStreamTckTest.*")
2323
// Skip testNG when tests are filtered with --tests, otherwise it simply fails
2424
onlyIf {
@@ -32,5 +32,5 @@ val testNG by tasks.registering(Test::class) {
3232

3333
val test by tasks.getting(Test::class) {
3434
dependsOn(testNG)
35-
reports.html.outputLocation = file("$buildDir/reports/junit")
35+
reports.html.outputLocation = file("${layout.buildDirectory.get()}/reports/junit")
3636
}

reactive/kotlinx-coroutines-rx3/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tasks.withType(DokkaTaskPartial::class) {
1818

1919
val testNG by tasks.registering(Test::class) {
2020
useTestNG()
21-
reports.html.outputLocation = file("$buildDir/reports/testng")
21+
reports.html.outputLocation = file("${layout.buildDirectory.get()}/reports/testng")
2222
include("**/*ReactiveStreamTckTest.*")
2323
// Skip testNG when tests are filtered with --tests, otherwise it simply fails
2424
onlyIf {
@@ -32,5 +32,5 @@ val testNG by tasks.registering(Test::class) {
3232

3333
val test by tasks.getting(Test::class) {
3434
dependsOn(testNG)
35-
reports.html.outputLocation = file("$buildDir/reports/junit")
35+
reports.html.outputLocation = file("${layout.buildDirectory.get()}/reports/junit")
3636
}

0 commit comments

Comments
 (0)