Fix Kotlin 2.3 warnings in Android Gradle builds (#59548) (#59548)

Summary:

Kotlin 2.3 introduced warnings that fail the Android Gradle builds when warnings are treated as errors:

- API version 2.0 is deprecated. Bump `apiVersion` to `KOTLIN_2_1` in the four gradle-plugin build files, as the previous Kotlin upgrade did when it moved from 1.9 to 2.0. Gradle 9, which React Native requires, ships a Kotlin 2.2 runtime, so the plugin still loads on every supported Gradle version.
- `-Xjvm-default` is deprecated in favor of `-jvm-default`. Replace `-Xjvm-default=all` in `ReactAndroid` with its equivalent, `jvmDefault = JvmDefaultMode.NO_COMPATIBILITY`.
- Kotlin 2.3 reports more redundant casts, non-null assertions, safe calls and Elvis operators after smart casts or on non-null platform types. Remove them in the gradle-plugin `JsonUtilsTest`, in `ReactAndroid` sources (`WritableNativeArray`, `MountItemDispatcher`, `PreAllocateViewMountItem`, `AppStateModule`, `NetworkingModule`, `UIManagerModule`, `ReactTextViewAccessibilityDelegate`) and in `ReactAndroid` unit tests (`NativeAnimatedNodeTraversalTest`, `DeviceInfoModuleTest`, `UIManagerModuleConstantsTest`).

Changelog:
[Android][Changed] - Bumped the Gradle plugin minimum Kotlin API version to 2.1

Reviewed By: andrewdacenko

Differential Revision: D127831836
This commit is contained in:
Peter Abbondanzo authored and GitHub committed 2026-10-28 11:26:48 +00:00
1 parent caf9cbcd63
commit 3cee68fa7a
16 files changed
+56 -60

No files matched your search

@@ -63,7 +63,7 @@ kotlin { jvmToolchain(17) }
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
apiVersion.set(KotlinVersion.KOTLIN_2_0)
apiVersion.set(KotlinVersion.KOTLIN_2_1)
// See comment above on JDK 11 support
jvmTarget.set(JvmTarget.JVM_11)
allWarningsAsErrors.set(
@@ -53,7 +53,7 @@ kotlin { jvmToolchain(17) }
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
apiVersion.set(KotlinVersion.KOTLIN_2_0)
apiVersion.set(KotlinVersion.KOTLIN_2_1)
// See comment above on JDK 11 support
jvmTarget.set(JvmTarget.JVM_11)
allWarningsAsErrors.set(
@@ -24,7 +24,7 @@ kotlin { jvmToolchain(17) }
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
apiVersion.set(KotlinVersion.KOTLIN_2_0)
apiVersion.set(KotlinVersion.KOTLIN_2_1)
// See comment above on JDK 11 support
jvmTarget.set(JvmTarget.JVM_11)
allWarningsAsErrors.set(
@@ -30,7 +30,7 @@ kotlin { jvmToolchain(17) }
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
apiVersion.set(KotlinVersion.KOTLIN_2_0)
apiVersion.set(KotlinVersion.KOTLIN_2_1)
// See comment above on JDK 11 support
jvmTarget.set(JvmTarget.JVM_11)
allWarningsAsErrors.set(
@@ -87,8 +87,8 @@ class JsonUtilsTest {
val parsed = JsonUtils.fromPackageJson(validJson)!!
assertThat("an awesome library").isEqualTo(parsed.codegenConfig!!.name)
assertThat("../js/").isEqualTo(parsed.codegenConfig!!.jsSrcsDir)
assertThat("com.awesome.library").isEqualTo(parsed.codegenConfig!!.android!!.javaPackageName)
assertThat("../js/").isEqualTo(parsed.codegenConfig.jsSrcsDir)
assertThat("com.awesome.library").isEqualTo(parsed.codegenConfig.android!!.javaPackageName)
}
@Test
@@ -182,13 +182,12 @@ class JsonUtilsTest {
val parsed = JsonUtils.fromAutolinkingConfigJson(validJson)!!
assertThat("./packages/rn-tester").isEqualTo(parsed.project!!.android!!.sourceDir)
assertThat("RN-Tester").isEqualTo(parsed.project!!.android!!.appName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project!!.android!!.packageName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project!!.android!!.applicationId)
assertThat(".RNTesterActivity").isEqualTo(parsed.project!!.android!!.mainActivity)
assertThat("--mode HermesDebug")
.isEqualTo(parsed.project!!.android!!.watchModeCommandParams!![0])
assertThat("implementation").isEqualTo(parsed.project!!.android!!.dependencyConfiguration)
assertThat("RN-Tester").isEqualTo(parsed.project.android.appName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project.android.packageName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project.android.applicationId)
assertThat(".RNTesterActivity").isEqualTo(parsed.project.android.mainActivity)
assertThat("--mode HermesDebug").isEqualTo(parsed.project.android.watchModeCommandParams!![0])
assertThat("implementation").isEqualTo(parsed.project.android.dependencyConfiguration)
}
@Test
@@ -231,13 +230,12 @@ class JsonUtilsTest {
val parsed = JsonUtils.fromAutolinkingConfigJson(validJson)!!
assertThat("./packages/rn-tester").isEqualTo(parsed.project!!.android!!.sourceDir)
assertThat("RN-Tester").isEqualTo(parsed.project!!.android!!.appName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project!!.android!!.packageName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project!!.android!!.applicationId)
assertThat(".RNTesterActivity").isEqualTo(parsed.project!!.android!!.mainActivity)
assertThat("--mode HermesDebug")
.isEqualTo(parsed.project!!.android!!.watchModeCommandParams!![0])
assertThat("implementation").isEqualTo(parsed.project!!.android!!.dependencyConfiguration)
assertThat("RN-Tester").isEqualTo(parsed.project.android.appName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project.android.packageName)
assertThat("com.facebook.react.uiapp").isEqualTo(parsed.project.android.applicationId)
assertThat(".RNTesterActivity").isEqualTo(parsed.project.android.mainActivity)
assertThat("--mode HermesDebug").isEqualTo(parsed.project.android.watchModeCommandParams!![0])
assertThat("implementation").isEqualTo(parsed.project.android.dependencyConfiguration)
}
@Test
@@ -286,47 +284,47 @@ class JsonUtilsTest {
assertThat("./node_modules/@react-native/oss-library-example")
.isEqualTo(parsed.dependencies!!["@react-native/oss-library-example"]!!.root)
assertThat("@react-native/oss-library-example")
.isEqualTo(parsed.dependencies!!["@react-native/oss-library-example"]!!.name)
.isEqualTo(parsed.dependencies["@react-native/oss-library-example"]!!.name)
assertThat("react-native_oss-library-example")
.isEqualTo(parsed.dependencies!!["@react-native/oss-library-example"]!!.nameCleansed)
.isEqualTo(parsed.dependencies["@react-native/oss-library-example"]!!.nameCleansed)
assertThat("./node_modules/@react-native/oss-library-example/android")
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.sourceDir,
)
assertThat("import com.facebook.react.osslibraryexample.OSSLibraryExamplePackage;")
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.packageImportPath,
)
assertThat("new OSSLibraryExamplePackage()")
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.packageInstance,
)
assertThat(listOf("staging", "debug", "release"))
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.buildTypes,
)
assertThat("OSSLibraryExampleSpec")
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.libraryName,
)
assertThat(listOf("SampleNativeComponentComponentDescriptor"))
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.componentDescriptors,
@@ -335,27 +333,27 @@ class JsonUtilsTest {
"./node_modules/@react-native/oss-library-example/android/build/generated/source/codegen/jni/CMakeLists.txt",
)
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.cmakeListsPath,
)
assertThat(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.cxxModuleHeaderName,
)
.isNull()
assertThat(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.cxxModuleCMakeListsPath,
)
.isNull()
assertThat(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.cxxModuleCMakeListsModuleName,
@@ -363,13 +361,13 @@ class JsonUtilsTest {
.isNull()
assertThat("implementation")
.isEqualTo(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.dependencyConfiguration,
)
assertThat(
parsed.dependencies!!["@react-native/oss-library-example"]!!
parsed.dependencies["@react-native/oss-library-example"]!!
.platforms!!
.android!!
.isPureCxxDependency!!,
@@ -11,6 +11,7 @@ import com.facebook.react.tasks.internal.*
import com.facebook.react.tasks.internal.utils.*
import de.undercouch.gradle.tasks.download.Download
import java.nio.file.Paths
import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
@@ -575,8 +576,8 @@ android {
kotlin {
compilerOptions {
// Using '-Xjvm-default=all' to generate default java methods for interfaces
freeCompilerArgs = listOf("-Xjvm-default=all")
// Generate default java methods for interfaces
jvmDefault = JvmDefaultMode.NO_COMPATIBILITY
// Using -PenableWarningsAsErrors=true prop to enable allWarningsAsErrors
allWarningsAsErrors = enableWarningsAsErrors()
}
@@ -35,13 +35,13 @@ public class WritableNativeArray : ReadableNativeArray(), WritableArray {
// Note: this consumes the map so do not reuse it.
override fun pushArray(array: ReadableArray?) {
check(array == null || array is ReadableNativeArray) { "Illegal type provided" }
pushNativeArray(array as ReadableNativeArray?)
pushNativeArray(array)
}
// Note: this consumes the map so do not reuse it.
override fun pushMap(map: ReadableMap?) {
check(map == null || map is ReadableNativeMap) { "Illegal type provided" }
pushNativeMap(map as ReadableNativeMap?)
pushNativeMap(map)
}
private external fun initHybrid()
@@ -151,7 +151,7 @@ internal class MountItemDispatcher(
} catch (e: RetryableMountingLayerException) {
if (item is DispatchCommandMountItem) {
// Only DispatchCommandMountItem supports retries
val mountItem: DispatchCommandMountItem = item as DispatchCommandMountItem
val mountItem: DispatchCommandMountItem = item
// Retrying exactly once
if (mountItem.getRetries() == 0) {
mountItem.incrementRetries()
@@ -59,7 +59,7 @@ internal class PreAllocateViewMountItem(
if (FabricUIManager.IS_DEVELOPMENT_ENVIRONMENT) {
result
.append(" props: ")
.append(props.toString() ?: "<null>")
.append(props.toString())
.append(" state: ")
.append(stateWrapper?.toString() ?: "<null>")
}
@@ -62,7 +62,6 @@ internal class AppStateModule(reactContext: ReactApplicationContext) :
Arguments.createMap().apply { putString("app_state", appState) }
private fun sendEvent(eventName: String, data: Any?) {
val reactApplicationContext = reactApplicationContext ?: return
// We don't gain anything interesting from logging here, and it's an extremely common
// race condition for an AppState event to be triggered as the Catalyst instance is being
// set up or torn down. So, just fail silently here.
@@ -1097,7 +1097,7 @@ public class NetworkingModule(
// would throw on every request, and spaces/invalid chars would violate the RFC 7231
// User-Agent product-token syntax. Reduce the label to token-safe characters; if nothing
// usable remains, fall back to the package name, which is always a valid token.
val label = appInfo?.let { pm.getApplicationLabel(it)?.toString() }
val label = appInfo?.let { pm.getApplicationLabel(it).toString() }
val appName = sanitizeToUserAgentToken(label).ifEmpty { sanitizeToUserAgentToken(packageName) }
if (appName.isEmpty()) return null
val version =
@@ -603,7 +603,7 @@ public open class UIManagerModule : ReactContextBaseJavaModule, LifecycleEventLi
try {
@Suppress("UNCHECKED_CAST")
return UIManagerModuleConstantsHelper.createConstants(
viewManagers as List<ViewManager<in Nothing, in Nothing>>,
viewManagers,
customBubblingEvents as? MutableMap<String, Any>,
customDirectEvents as? MutableMap<String, Any>,
)
@@ -630,7 +630,7 @@ public open class UIManagerModule : ReactContextBaseJavaModule, LifecycleEventLi
@Suppress("UNCHECKED_CAST")
val viewManagerConstants =
UIManagerModuleConstantsHelper.createConstantsForViewManager(
viewManager as ViewManager<in Nothing, in Nothing>,
viewManager,
null,
null,
null,
@@ -82,13 +82,13 @@ internal class ReactTextViewAccessibilityDelegate(
if (span is ReactClickableSpan && hostView is TextView) {
span.isKeyboardFocused = hasFocus
span.focusBgColor = (hostView as TextView).highlightColor
span.focusBgColor = hostView.highlightColor
hostView.invalidate()
} else if (hostView is PreparedLayoutTextView) {
if (hasFocus) {
(hostView as PreparedLayoutTextView).setSelection(link.start, link.end)
hostView.setSelection(link.start, link.end)
} else {
(hostView as PreparedLayoutTextView).clearSelection()
hostView.clearSelection()
}
}
}
@@ -155,11 +155,11 @@ internal class ReactTextViewAccessibilityDelegate(
private fun getLayoutFromHost(): Layout? {
return if (hostView is PreparedLayoutTextView) {
(hostView as PreparedLayoutTextView).preparedLayout?.layout
} else if (hostView is ReactTextView && (hostView as ReactTextView).preparedLayout != null) {
(hostView as ReactTextView).preparedLayout?.layout
hostView.preparedLayout?.layout
} else if (hostView is ReactTextView && hostView.preparedLayout != null) {
hostView.preparedLayout?.layout
} else if (hostView is TextView) {
(hostView as TextView).layout
hostView.layout
} else {
null
}
@@ -91,7 +91,7 @@ class NativeAnimatedNodeTraversalTest {
whenever(uiManagerMock.directEventNamesResolver).thenAnswer {
object : UIManagerModule.CustomEventNamesResolver {
override fun resolveCustomEventName(eventName: String): String {
val constants: Map<String, Any?> = uiManagerMock.constants ?: emptyMap()
val constants: Map<String, Any?> = uiManagerMock.constants
val directEventTypes: Any? = constants["customDirectEventTypes"]
if (directEventTypes != null && directEventTypes is Map<*, *>) {
val customEventType = directEventTypes[eventName]
@@ -100,7 +100,7 @@ class DeviceInfoModuleTest {
deviceInfoModule.typedExportedConstants
deviceInfoModule.emitUpdateDimensionsEvent()
verify(reactContext, times(0))
?.emitDeviceEvent(ArgumentMatchers.anyString(), ArgumentMatchers.any())
.emitDeviceEvent(ArgumentMatchers.anyString(), ArgumentMatchers.any())
}
@Test
@@ -71,8 +71,7 @@ class UIManagerModuleConstantsTest {
val viewManagers = listOf(manager)
val uiManagerModule = UIManagerModule(reactContext, viewManagers, 0)
val viewManagerConstants =
uiManagerModule.constants?.get(VIEW_MANAGER_NAME) as Map<String, Any>?
val viewManagerConstants = uiManagerModule.constants[VIEW_MANAGER_NAME] as Map<String, Any>?
Assertions.assertThat(viewManagerConstants)
.containsKey(BUBBLING_EVENTS_TYPES_KEY)
@@ -88,7 +87,7 @@ class UIManagerModuleConstantsTest {
val viewManagers = listOf(manager)
val uiManagerModule = UIManagerModule(reactContext, viewManagers, 0)
val viewManagerConstants = uiManagerModule.constants?.get(VIEW_MANAGER_NAME) as Map<String, Any>
val viewManagerConstants = uiManagerModule.constants[VIEW_MANAGER_NAME] as Map<String, Any>
Assertions.assertThat(viewManagerConstants).containsKey(BUBBLING_EVENTS_TYPES_KEY)
val bubblingEventTypes = viewManagerConstants[BUBBLING_EVENTS_TYPES_KEY] as Map<String, Any>
@@ -104,7 +103,7 @@ class UIManagerModuleConstantsTest {
val viewManagers = listOf(manager)
val uiManagerModule = UIManagerModule(reactContext, viewManagers, 0)
val viewManagerConstants = uiManagerModule.constants?.get(VIEW_MANAGER_NAME) as Map<String, Any>
val viewManagerConstants = uiManagerModule.constants[VIEW_MANAGER_NAME] as Map<String, Any>
Assertions.assertThat(viewManagerConstants).containsKey(DIRECT_EVENTS_TYPES_KEY)
val directEventTypes = viewManagerConstants[DIRECT_EVENTS_TYPES_KEY] as Map<String, Any>
@@ -123,8 +122,7 @@ class UIManagerModuleConstantsTest {
val constants = uiManagerModule.constants
Assertions.assertThat(constants).containsKey(VIEW_MANAGER_NAME)
Assertions.assertThat(constants!![VIEW_MANAGER_NAME] as Map<String, Any>)
.containsKey("Constants")
Assertions.assertThat(constants[VIEW_MANAGER_NAME] as Map<String, Any>).containsKey("Constants")
Assertions.assertThat(
valueAtPath(constants, VIEW_MANAGER_NAME, "Constants") as Map<String, Any>?,
)
@@ -181,7 +179,7 @@ class UIManagerModuleConstantsTest {
val viewManagers = listOf(managerX, managerY)
val uiManagerModule = UIManagerModule(reactContext, viewManagers, 0)
val constants = uiManagerModule.constants
val viewManagerConstants = constants!!["ManagerX"] as Map<String, Any>
val viewManagerConstants = constants["ManagerX"] as Map<String, Any>
Assertions.assertThat(viewManagerConstants[DIRECT_EVENTS_TYPES_KEY] as Map<String, Any>)
.containsKey("onTwirl")
val twirlMap =