diff --git a/packages/cli-platform-android/src/config/__fixtures__/files/build.gradle b/packages/cli-platform-android/src/config/__fixtures__/files/build.gradle index 42e8b0483..577429333 100644 --- a/packages/cli-platform-android/src/config/__fixtures__/files/build.gradle +++ b/packages/cli-platform-android/src/config/__fixtures__/files/build.gradle @@ -1,6 +1,10 @@ apply package: "com.android.application" apply package: "com.facebook.react" +android { + namespace 'com.some.example' +} + react { libraryName = "justalibrary" } \ No newline at end of file diff --git a/packages/cli-platform-android/src/config/__tests__/getProjectConfig.test.ts b/packages/cli-platform-android/src/config/__tests__/getProjectConfig.test.ts index 20037a6b5..108ed0865 100644 --- a/packages/cli-platform-android/src/config/__tests__/getProjectConfig.test.ts +++ b/packages/cli-platform-android/src/config/__tests__/getProjectConfig.test.ts @@ -30,22 +30,22 @@ describe('android::getProjectConfig', () => { multiple: { android: mocks.userConfigManifest, }, - noManifest: { + noManifestNoGradle: { android: {}, }, }); }); - it("returns `null` if manifest file hasn't been found and userConfig is not defined", () => { + it("returns `null` if neither manifest nor gradle file hasn't been found and userConfig is not defined", () => { const userConfig = undefined; - const folder = '/noManifest'; + const folder = '/noManifestNoGradle'; expect(getProjectConfig(folder, userConfig)).toBeNull(); }); - it("returns `null` if manifest file hasn't been found", () => { + it("returns `null` if neither manifest nor gradle file hasn't been found", () => { const userConfig = {}; - const folder = '/noManifest'; + const folder = '/noManifestNoGradle'; expect(getProjectConfig(folder, userConfig)).toBeNull(); }); diff --git a/packages/cli-platform-android/src/config/getAndroidProject.ts b/packages/cli-platform-android/src/config/getAndroidProject.ts index f30596c1b..a920989c8 100644 --- a/packages/cli-platform-android/src/config/getAndroidProject.ts +++ b/packages/cli-platform-android/src/config/getAndroidProject.ts @@ -17,59 +17,63 @@ export function getAndroidProject(config: Config) { } /** - * Get the package name/namespace of the running React Native app + * Util function to discover the package name from either the Manifest file or the build.gradle file. * @param manifestPath The path to the AndroidManifest.xml * @param buildGradlePath The path to the build.gradle[.kts] file. */ -export function getPackageName( - manifestPath: string, +function discoverPackageName( + manifestPath: string | null, buildGradlePath: string | null, ) { - const androidManifest = fs.readFileSync(manifestPath, 'utf8'); - - const packageNameFromManifest = parsePackageNameFromAndroidManifestFile( - androidManifest, - ); - let packageName; - if (packageNameFromManifest) { + if (manifestPath) { + const androidManifest = fs.readFileSync(manifestPath, 'utf8'); + const packageNameFromManifest = parsePackageNameFromAndroidManifestFile( + androidManifest, + ); // We got the package from the AndroidManifest.xml - packageName = packageNameFromManifest; - } else if (buildGradlePath) { + if (packageNameFromManifest) { + return packageNameFromManifest; + } + } + + if (buildGradlePath) { // We didn't get the package from the AndroidManifest.xml, // so we'll try to get it from the build.gradle[.kts] file // via the namespace field. const buildGradle = fs.readFileSync(buildGradlePath, 'utf8'); const namespace = parseNamespaceFromBuildGradleFile(buildGradle); if (namespace) { - packageName = namespace; - } else { - throw new CLIError( - `Failed to build the app: No package name found. - We couldn't parse the namespace from your build.gradle[.kts] file at ${chalk.underline.dim( - `${buildGradlePath}`, - )} - and nor your package in the AndroidManifest at ${chalk.underline.dim( - `${manifestPath}`, - )} - `, - ); + return namespace; } - } else { - throw new CLIError( - `Failed to build the app: No package name found. - We failed to parse your AndroidManifest at ${chalk.underline.dim( - `${manifestPath}`, - )} - and we couldn't find your build.gradle[.kts] file. - `, - ); } + throw new CLIError( + `Failed to build the app: No package name found. + We couldn't parse the namespace from neither your build.gradle[.kts] file at ${chalk.underline.dim( + `${buildGradlePath}`, + )} + nor your package in the AndroidManifest at ${chalk.underline.dim( + `${manifestPath}`, + )} + `, + ); +} + +/** + * Get the package name/namespace of the running React Native app + * @param manifestPath The path to the AndroidManifest.xml + * @param buildGradlePath The path to the build.gradle[.kts] file. + */ +export function getPackageName( + manifestPath: string | null, + buildGradlePath: string | null, +) { + let packageName = discoverPackageName(manifestPath, buildGradlePath); if (!validatePackageName(packageName)) { logger.warn( `Invalid application's package name "${chalk.bgRed( packageName, - )}" in 'AndroidManifest.xml'. Read guidelines for setting the package name here: ${chalk.underline.dim( + )}" in either 'AndroidManifest.xml' or 'build.gradle'. Read guidelines for setting the package name here: ${chalk.underline.dim( 'https://developer.android.com/studio/build/application-id', )}`, ); diff --git a/packages/cli-platform-android/src/config/index.ts b/packages/cli-platform-android/src/config/index.ts index 3ee3aac9f..10d09074e 100644 --- a/packages/cli-platform-android/src/config/index.ts +++ b/packages/cli-platform-android/src/config/index.ts @@ -45,7 +45,7 @@ export function projectConfig( : findManifest(path.join(sourceDir, appName)); const buildGradlePath = findBuildGradle(sourceDir, false); - if (!manifestPath) { + if (!manifestPath && !buildGradlePath) { return null; } @@ -53,7 +53,9 @@ export function projectConfig( userConfig.packageName || getPackageName(manifestPath, buildGradlePath); if (!packageName) { - throw new Error(`Package name not found in ${manifestPath}`); + throw new Error( + `Package name not found in neither ${manifestPath} nor ${buildGradlePath}`, + ); } return { @@ -101,7 +103,7 @@ export function dependencyConfig( : findManifest(sourceDir); const buildGradlePath = findBuildGradle(sourceDir, true); - if (!manifestPath) { + if (!manifestPath && !buildGradlePath) { return null; }