Skip to content

Commit c2a82ab

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Add yarn scripts to regenerate ReactAndroid.api
Summary: The Android public API snapshot (packages/react-native/ReactAndroid/api/ReactAndroid.api) could previously only be regenerated with js1 build android-api, which runs a Buck-based generator that only exists in fbsource. Add yarn android-api-build and yarn android-api-validate, mirroring yarn cxx-api-build and yarn cxx-api-validate: in the open source repo they run the binary-compatibility-validator Gradle tasks (apiDump and apiCheck), and in a Meta-internal checkout they delegate to a stripped .fb.js backend that runs the same Buck generator js1 used. js1 build android-api now delegates to the yarn scripts, the way js1 build js-api delegates to yarn build-types. Changelog: [Internal] Differential Revision: D135395265
1 parent fa1dc61 commit c2a82ab

4 files changed

Lines changed: 107 additions & 1 deletion

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

‎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",

‎scripts/android-api/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
In the open source repo this runs the
18+
`binary-compatibility-validator` Gradle tasks (`apiDump` / `apiCheck`) for
19+
`:packages:react-native:ReactAndroid`. In a Meta-internal checkout it builds
20+
ReactAndroid with Buck and runs the same validator there instead.
21+
22+
## When to use it
23+
24+
Run `yarn android-api-build` after any change to a Kotlin or Java API in
25+
`packages/react-native/ReactAndroid/` that is effectively public or
26+
protected, and commit the updated `.api` file. APIs annotated with
27+
`@UnstableReactNativeAPI`, APIs inside an `internal` package, and
28+
Kotlin-`internal`/`private` declarations are excluded from tracking.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
const {REPO_ROOT} = require('../shared/consts');
12+
const {spawnSync} = require('node:child_process');
13+
const path = require('node:path');
14+
15+
function moduleExists(modulePath /*: string */) /*: boolean */ {
16+
try {
17+
require.resolve(modulePath);
18+
return true;
19+
} catch {
20+
return false;
21+
}
22+
}
23+
24+
// Resolved before requiring so that a failure to load the module is not
25+
// mistaken for its absence in the open source repo.
26+
const resolveFb /*: ?(validate: boolean) => number */ = moduleExists(
27+
'./build-android-api.fb.js',
28+
)
29+
? // $FlowFixMe[cannot-resolve-module] - not resolvable in OSS
30+
// eslint-disable-next-line import/no-unresolved
31+
require('./build-android-api.fb.js')
32+
: null;
33+
34+
function runGradle(validate /*: boolean */) /*: number */ {
35+
const gradlew = process.platform === 'win32' ? 'gradlew.bat' : './gradlew';
36+
const task = `:packages:react-native:ReactAndroid:${
37+
validate ? 'apiCheck' : 'apiDump'
38+
}`;
39+
const {status, error} = spawnSync(gradlew, [task], {
40+
cwd: REPO_ROOT,
41+
stdio: 'inherit',
42+
shell: process.platform === 'win32',
43+
});
44+
if (error != null) {
45+
console.error(`Failed to run ${gradlew}: ${error.message}`);
46+
return 1;
47+
}
48+
return status ?? 1;
49+
}
50+
51+
function main() /*: number */ {
52+
const args = process.argv.slice(2);
53+
if (args.includes('--help') || args.includes('-h')) {
54+
console.log(`
55+
Usage: node ./scripts/android-api/build-android-api.js [--validate]
56+
57+
Regenerate the React Native Android public API snapshot
58+
(packages/react-native/ReactAndroid/api/ReactAndroid.api).
59+
60+
Options:
61+
--validate Verify the committed snapshot is up to date instead of
62+
regenerating it. Exits non-zero when it differs.
63+
`);
64+
return 0;
65+
}
66+
const validate = args.includes('--validate');
67+
68+
// In a Meta-internal checkout the .fb.js backend (stripped from the open
69+
// source repo) generates the snapshot with Buck instead of Gradle.
70+
if (resolveFb != null) {
71+
return resolveFb(validate);
72+
}
73+
return runGradle(validate);
74+
}
75+
76+
process.exitCode = main();

0 commit comments

Comments
 (0)