Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Never hand-edit generated output — change the source and regenerate. CI valida
- Native modules and components are declared by JavaScript specs (`Native*.js`, `*NativeComponent.js`); their native counterparts are generated at build time.
- Feature flags are declared in `packages/react-native/scripts/featureflags/ReactNativeFeatureFlags.config.js`; `yarn featureflags` regenerates the JavaScript, Java, and C++ accessors.
- 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.
- 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`.
- 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`).
- `CHANGELOG.md` is compiled at release time from pull request descriptions.

## Flow types for npm packages
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

buildscript { dependencies { classpath(libs.kotlin.metadata.jvm) } }

plugins {
alias(libs.plugins.nexus.publish)
alias(libs.plugins.android.library) apply false
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"packageManager": "yarn@1.22.22",
"scripts": {
"android": "yarn --cwd packages/rn-tester android",
"android-api-build": "node ./scripts/android-api/build-android-api.js",
"android-api-validate": "node ./scripts/android-api/build-android-api.js --validate",
"build-android": "./gradlew :packages:react-native:ReactAndroid:build",
"build": "node ./scripts/build/build.js",
"build-types": "node ./scripts/js-api/build-types",
Expand Down
48 changes: 48 additions & 0 deletions packages/react-native/ReactAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ 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 kotlinx.validation.api.dump
import kotlinx.validation.api.filterOutAnnotated
import kotlinx.validation.api.filterOutNonPublic
import kotlinx.validation.api.loadApiFromJvmClasses
import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

Expand Down Expand Up @@ -830,6 +834,50 @@ kotlin {

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

abstract class PublicApiDumpTask : DefaultTask() {
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val classFiles: ConfigurableFileCollection

@get:Input abstract val ignoredPackages: ListProperty<String>

@get:Input abstract val ignoredClasses: ListProperty<String>

@get:Input abstract val nonPublicMarkers: ListProperty<String>

@get:OutputFile abstract val outputFile: RegularFileProperty

@TaskAction
fun dump() {
val api =
classFiles.asFileTree
.matching { include("**/*.class") }
.files
.asSequence()
.map { it.readBytes().inputStream() }
.loadApiFromJvmClasses()
.filterOutNonPublic(ignoredPackages.get(), ignoredClasses.get())
.filterOutAnnotated(nonPublicMarkers.get().map { it.replace(".", "/") }.toSet())
.dump(StringBuilder())
outputFile.get().asFile.writeText(api.toString())
}
}

tasks.register<PublicApiDumpTask>("publicApiDump") {
description =
"Dumps the public API of the release classes. Run `yarn android-api-build` to update ReactAndroid.api."
fun listProperty(key: String) =
project.property("binaryCompatibilityValidator.$key").toString().split(",").map { it.trim() }
classFiles.from(tasks.named("compileReleaseKotlin"), tasks.named("compileReleaseJavaWithJavac"))
ignoredPackages.set(listProperty("ignoredPackages"))
ignoredClasses.set(listProperty("ignoredClasses"))
nonPublicMarkers.set(listProperty("nonPublicMarkers"))
outputFile.set(
project.findProperty("publicApiDumpFile")?.let { file(it) }
?: layout.buildDirectory.file("publicApi/ReactAndroid.api").get().asFile,
)
}

/* Publishing Configuration */
apply(from = "./publish.gradle")

Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ androidx-test-junit = "1.2.1"
androidx-tracing = "1.1.0"
androidx-window = "1.5.1"
assertj = "3.21.0"
binary-compatibility-validator = "0.13.2"
binary-compatibility-validator = "0.17.0"
download = "5.4.0"
espresso-core = "3.6.1"
fbjni = "0.8.1"
Expand Down Expand Up @@ -89,6 +89,7 @@ okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
javax-inject = { module = "javax.inject:javax.inject", version.ref = "javax-inject" }
javax-annotation-api = { module = "javax.annotation:javax.annotation-api", version.ref = "javax-annotation-api" }
jsc-android = { module = "io.github.react-native-community:jsc-android", version.ref = "jsc-android" }
kotlin-metadata-jvm = { module = "org.jetbrains.kotlin:kotlin-metadata-jvm", version.ref = "kotlin" }

junit = {module = "junit:junit", version.ref = "junit" }
assertj = {module = "org.assertj:assertj-core", version.ref = "assertj" }
Expand Down
37 changes: 37 additions & 0 deletions scripts/android-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# scripts/android-api

Regenerate the React Native Android public API snapshot,
`packages/react-native/ReactAndroid/api/ReactAndroid.api`, which tracks the
public Java/Kotlin API surface of ReactAndroid.

## Usage

```sh
# Regenerate the snapshot after an intentional public API change
yarn android-api-build

# Verify the committed snapshot is up to date without rewriting it (for CI)
yarn android-api-validate
```

These run ReactAndroid's `publicApiDump` Gradle task, which compiles the
release classes and dumps their public API with the
`binary-compatibility-validator` library, using the filters in
`packages/react-native/ReactAndroid/gradle.properties`.

The snapshot is maintained from a build that also records a few entries the
Gradle build does not produce: Kotlin `$DefaultImpls` classes and forwarders to
inherited interface default methods, `$$ReactModuleInfoProvider` classes, and
bridges for some internal members. The script keeps the ones already in the
snapshot. `$DefaultImpls` members follow each interface's default methods, and
`$DefaultImpls` classes, forwarders and bridges go away with the class or
default method they belong to. It does not add new ones; those are added when
the change is imported into Meta's repository.

## When to use it

Run `yarn android-api-build` after any change to a Kotlin or Java API in
`packages/react-native/ReactAndroid/` that is effectively public or
protected, and commit the updated `.api` file. APIs annotated with
`@UnstableReactNativeAPI`, APIs inside an `internal` package, and
Kotlin-`internal`/`private` declarations are excluded from tracking.
Loading
Loading