Skip to content

Commit 5dc00cb

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Add yarn scripts to regenerate ReactAndroid.api
Summary: `ReactAndroid.api` tracks the public Java/Kotlin API of ReactAndroid, but nothing in this repo could regenerate or check it, so a contributor changing a public Android API had no way to update the snapshot (#60446). This adds `yarn android-api-build` and `yarn android-api-validate`, which work like `yarn cxx-api-build` and `yarn cxx-api-validate`: - A new `publicApiDump` Gradle task on ReactAndroid compiles the release classes and dumps their public API with the `binary-compatibility-validator` library, using the filters already in `ReactAndroid/gradle.properties`. The validator plugin's own `apiDump`/`apiCheck` tasks never existed here: they only attach to projects that apply `kotlin-android`, which AGP 9's built-in Kotlin replaces, and `validationDisabled=true` turns them off anyway. - `scripts/android-api/build-android-api.js` runs that dump, then writes the snapshot or prints a diff against it. - The validator moves to 0.17.0, and `kotlin-metadata-jvm` joins the root build script classpath because the validator library needs it at runtime. The snapshot itself does not change. It also records entries the Gradle build does not produce: Kotlin `$DefaultImpls` classes and forwarders to inherited interface default methods (from modules compiled in DefaultImpls-compatible mode), `$$ReactModuleInfoProvider` classes for `ReactModuleList` packages, and bridges for some internal members. The script adds those to the Gradle dump: `$$ReactModuleInfoProvider` classes from the `ReactModuleList` annotations in the sources, bridges from the committed snapshot while the class still overrides the internal member a superclass declares, and the rest from the committed snapshot. A source directory whose existing interfaces and classes all carry `$DefaultImpls` classes and forwarders gets them for new interfaces, new default methods and new classes too, and `$DefaultImpls` members follow each interface's default methods. A Gradle dump of an unchanged tree reproduces `ReactAndroid.api` byte for byte. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D135395265
1 parent 6aa3991 commit 5dc00cb

7 files changed

Lines changed: 710 additions & 2 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Never hand-edit generated output — change the source and regenerate. CI valida
6969
- Native modules and components are declared by JavaScript specs (`Native*.js`, `*NativeComponent.js`); their native counterparts are generated at build time.
7070
- Feature flags are declared in `packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js`; `yarn featureflags` regenerates the JavaScript, Java, and C++ accessors.
7171
- JavaScript sources are typed with Flow, and the public API is exported from `packages/react-native/index.js`. TypeScript types are generated from those sources, and `packages/react-native/ReactNativeApi.d.ts` is a committed snapshot of that API — run `yarn build-types` to regenerate both whenever the public API changes, then `yarn test-generated-typescript` to type-check the result.
72-
- The public native API is snapshotted as well: C++ under `scripts/cxx-api` (`yarn cxx-api-build`), and Android in `packages/react-native/ReactAndroid/api/ReactAndroid.api`.
72+
- The public native API is snapshotted as well: C++ under `scripts/cxx-api` (`yarn cxx-api-build`), and Android in `packages/react-native/ReactAndroid/api/ReactAndroid.api` (`yarn android-api-build`).
7373
- `CHANGELOG.md` is compiled at release time from pull request descriptions.
7474

7575
## Flow types for npm packages

‎build.gradle.kts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
buildscript { dependencies { classpath(libs.kotlin.metadata.jvm) } }
9+
810
plugins {
911
alias(libs.plugins.nexus.publish)
1012
alias(libs.plugins.android.library) apply false

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"packageManager": "yarn@1.22.22",
77
"scripts": {
88
"android": "yarn --cwd packages/rn-tester android",
9+
"android-api-build": "node ./scripts/android-api/build-android-api.js",
10+
"android-api-validate": "node ./scripts/android-api/build-android-api.js --validate",
911
"build-android": "./gradlew :packages:react-native:ReactAndroid:build",
1012
"build": "node ./scripts/build/build.js",
1113
"build-types": "node ./scripts/js-api/build-types",

‎packages/react-native/ReactAndroid/build.gradle.kts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import com.facebook.react.tasks.internal.*
1111
import com.facebook.react.tasks.internal.utils.*
1212
import de.undercouch.gradle.tasks.download.Download
1313
import java.nio.file.Paths
14+
import kotlinx.validation.api.dump
15+
import kotlinx.validation.api.filterOutAnnotated
16+
import kotlinx.validation.api.filterOutNonPublic
17+
import kotlinx.validation.api.loadApiFromJvmClasses
1418
import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode
1519
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
1620

@@ -830,6 +834,50 @@ kotlin {
830834

831835
tasks.withType<Test> { jvmArgs = listOf("-Xshare:off") }
832836

837+
abstract class PublicApiDumpTask : DefaultTask() {
838+
@get:InputFiles
839+
@get:PathSensitive(PathSensitivity.RELATIVE)
840+
abstract val classFiles: ConfigurableFileCollection
841+
842+
@get:Input abstract val ignoredPackages: ListProperty<String>
843+
844+
@get:Input abstract val ignoredClasses: ListProperty<String>
845+
846+
@get:Input abstract val nonPublicMarkers: ListProperty<String>
847+
848+
@get:OutputFile abstract val outputFile: RegularFileProperty
849+
850+
@TaskAction
851+
fun dump() {
852+
val api =
853+
classFiles.asFileTree
854+
.matching { include("**/*.class") }
855+
.files
856+
.asSequence()
857+
.map { it.readBytes().inputStream() }
858+
.loadApiFromJvmClasses()
859+
.filterOutNonPublic(ignoredPackages.get(), ignoredClasses.get())
860+
.filterOutAnnotated(nonPublicMarkers.get().map { it.replace(".", "/") }.toSet())
861+
.dump(StringBuilder())
862+
outputFile.get().asFile.writeText(api.toString())
863+
}
864+
}
865+
866+
tasks.register<PublicApiDumpTask>("publicApiDump") {
867+
description =
868+
"Dumps the public API of the release classes. Run `yarn android-api-build` to update ReactAndroid.api."
869+
fun listProperty(key: String) =
870+
project.property("binaryCompatibilityValidator.$key").toString().split(",").map { it.trim() }
871+
classFiles.from(tasks.named("compileReleaseKotlin"), tasks.named("compileReleaseJavaWithJavac"))
872+
ignoredPackages.set(listProperty("ignoredPackages"))
873+
ignoredClasses.set(listProperty("ignoredClasses"))
874+
nonPublicMarkers.set(listProperty("nonPublicMarkers"))
875+
outputFile.set(
876+
project.findProperty("publicApiDumpFile")?.let { file(it) }
877+
?: layout.buildDirectory.file("publicApi/ReactAndroid.api").get().asFile,
878+
)
879+
}
880+
833881
/* Publishing Configuration */
834882
apply(from = "./publish.gradle")
835883

‎packages/react-native/gradle/libs.versions.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ androidx-test-junit = "1.2.1"
2121
androidx-tracing = "1.1.0"
2222
androidx-window = "1.5.1"
2323
assertj = "3.21.0"
24-
binary-compatibility-validator = "0.13.2"
24+
binary-compatibility-validator = "0.17.0"
2525
download = "5.4.0"
2626
espresso-core = "3.6.1"
2727
fbjni = "0.8.1"
@@ -89,6 +89,7 @@ okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
8989
javax-inject = { module = "javax.inject:javax.inject", version.ref = "javax-inject" }
9090
javax-annotation-api = { module = "javax.annotation:javax.annotation-api", version.ref = "javax-annotation-api" }
9191
jsc-android = { module = "io.github.react-native-community:jsc-android", version.ref = "jsc-android" }
92+
kotlin-metadata-jvm = { module = "org.jetbrains.kotlin:kotlin-metadata-jvm", version.ref = "kotlin" }
9293

9394
junit = {module = "junit:junit", version.ref = "junit" }
9495
assertj = {module = "org.assertj:assertj-core", version.ref = "assertj" }

‎scripts/android-api/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# scripts/android-api
2+
3+
Regenerate the React Native Android public API snapshot,
4+
`packages/react-native/ReactAndroid/api/ReactAndroid.api`, which tracks the
5+
public Java/Kotlin API surface of ReactAndroid.
6+
7+
## Usage
8+
9+
```sh
10+
# Regenerate the snapshot after an intentional public API change
11+
yarn android-api-build
12+
13+
# Verify the committed snapshot is up to date without rewriting it (for CI)
14+
yarn android-api-validate
15+
```
16+
17+
These run ReactAndroid's `publicApiDump` Gradle task, which compiles the
18+
release classes and dumps their public API with the
19+
`binary-compatibility-validator` library, using the filters in
20+
`packages/react-native/ReactAndroid/gradle.properties`.
21+
22+
The snapshot is maintained from a build that also records a few entries the
23+
Gradle build does not produce: Kotlin `$DefaultImpls` classes and forwarders to
24+
inherited interface default methods (from modules compiled in
25+
DefaultImpls-compatible mode), `$$ReactModuleInfoProvider` classes for
26+
`@ReactModuleList` packages, and bridges for some internal members. The script
27+
adds them from the committed snapshot: a source directory whose existing
28+
interfaces and classes all carry these entries gets them for new ones too, and
29+
`$DefaultImpls` members follow each interface's default methods.
30+
31+
## When to use it
32+
33+
Run `yarn android-api-build` after any change to a Kotlin or Java API in
34+
`packages/react-native/ReactAndroid/` that is effectively public or
35+
protected, and commit the updated `.api` file. APIs annotated with
36+
`@UnstableReactNativeAPI`, APIs inside an `internal` package, and
37+
Kotlin-`internal`/`private` declarations are excluded from tracking.

0 commit comments

Comments
 (0)