Skip to content

Commit

Permalink
build: patch codegen and ensure android builds correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
wjaykim committed Jan 11, 2025
1 parent c3f01aa commit 94c8d3b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ android {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/newarch']
java.srcDirs += ['src/codegen']
java.srcDirs += ['src/codegen/java']
} else {
java.srcDirs += ['src/oldarch']
}
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@
"build": "genversion --es6 --semi src/version.ts && bob build && yarn codegen",
"build:clean": "rimraf android/build && rimraf ios/build && rimraf lib && rimraf plugin/build",
"build:plugin": "yarn tsc --build plugin",
"codegen": "yarn codegen:android && yarn codegen:ios",
"codegen:android": "npx --yes @react-native-community/cli codegen --platform android --outputPath android/src/codegen",
"codegen:ios": "npx --yes @react-native-community/cli codegen --platform ios --outputPath ios/RNGoogleMobileAdsSpec",
"codegen": "npx --yes @react-native-community/cli codegen && node scripts/patch-codegen.mjs",
"lint:code": "yarn lint:js && yarn lint:android && yarn lint:ios:check",
"lint:js": "eslint src/ --ext .js,.jsx,.ts,.tsx --max-warnings=0",
"lint:android": "google-java-format --set-exit-if-changed --replace --glob=\"android/src/main/**/*.java\"",
Expand Down Expand Up @@ -144,6 +142,7 @@
"eslint-plugin-react": "^7.31.10",
"expo": "^47.0.0",
"expo-module-scripts": "^3.5.2",
"fs-extra": "^11.2.0",
"genversion": "^3.1.1",
"google-java-format": "^1.1.0",
"jest": "^29.2.2",
Expand Down Expand Up @@ -172,6 +171,10 @@
"name": "RNGoogleMobileAdsSpec",
"type": "all",
"jsSrcsDir": "./src/specs",
"outputDir": {
"android": "./android/src/codegen",
"ios": "./ios/RNGoogleMobileAdsSpec"
},
"includesGeneratedCode": true,
"android": {
"javaPackageName": "io.invertase.googlemobileads"
Expand Down
83 changes: 83 additions & 0 deletions scripts/patch-codegen.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import fs from 'fs-extra';
import path from 'node:path';

/**
* Currently, running react-native codegen generates java files with package name `com.facebook.fbreact.specs`.
* This is a known issue in react-native itself.
* You can find the relevant line here: https://github.com/facebook/react-native/blob/dc460147bb00d6f912cc0a829f8040d85faeeb13/packages/react-native/scripts/codegen/generate-artifacts-executor.js#L459.
* To workaround, this function renames the package name to the one provided in the codegenConfig.
* @throws if codegenConfig.outputDir.android or codegenConfig.android.javaPackageName is not defined in package.json
* @throws if the codegenAndroidPath does not exist
*/
async function patchCodegen() {
const projectPath = path.resolve(import.meta.dirname, '..');
const packageJsonPath = path.resolve(projectPath, 'package.json');
const packageJson = await fs.readJson(packageJsonPath);

let codegenAndroidPath = packageJson.codegenConfig.outputDir.android;
if (!codegenAndroidPath) {
throw new Error('You need to define codegenConfig.outputDir.android in your package.json');
}
codegenAndroidPath = path.resolve(projectPath, codegenAndroidPath);

if (!(await fs.pathExists(codegenAndroidPath))) {
throw new Error(
`Could not find ${codegenAndroidPath}. Make sure you are in the correct directory and react-native codegen works properly.`,
);
}

const codegenJavaPackageName = packageJson.codegenConfig.android.javaPackageName;
if (!codegenJavaPackageName) {
throw new Error(
'You need to define codegenConfig.android.javaPackageName in your package.json',
);
}

const codegenJavaPath = path.resolve(codegenAndroidPath, `java/com/facebook/fbreact/specs`);

// If this issue is ever fixed in react-native, this check will prevent the patching from running.
if (!(await fs.pathExists(codegenJavaPath))) {
console.log(`Could not find ${codegenJavaPath}. Skipping patching codegen java files.`);
return;
}

const javaFiles = await fs.readdir(codegenJavaPath);

await Promise.all(
javaFiles.map(async file => {
const filePath = path.resolve(codegenJavaPath, file);
const fileContent = await fs.readFile(filePath, 'utf8');

const newFileContent = fileContent.replace(
'package com.facebook.fbreact.specs',
`package ${codegenJavaPackageName}`,
);

await fs.writeFile(filePath, newFileContent);
}),
);

const newPackagePath = path.resolve(
codegenAndroidPath,
`java/${codegenJavaPackageName.replace(/\./g, '/')}`,
);

if (!(await fs.pathExists(newPackagePath))) {
await fs.mkdir(newPackagePath, { recursive: true });
}

await Promise.all(
javaFiles.map(async file => {
const filePath = path.resolve(codegenJavaPath, file);
const newFilePath = path.resolve(newPackagePath, file);

await fs.rename(filePath, newFilePath);
}),
);

await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook/fbreact'), {
recursive: true,
});
}

patchCodegen();
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6537,7 +6537,7 @@ fs-extra@^10.1.0:
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-extra@^11.0.0:
fs-extra@^11.0.0, fs-extra@^11.2.0:
version "11.2.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.2.0.tgz#e70e17dfad64232287d01929399e0ea7c86b0e5b"
integrity sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==
Expand Down

0 comments on commit 94c8d3b

Please sign in to comment.