Skip to content

Commit

Permalink
fix: Correctly autolink libraries without manifest (#1800)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico authored and thymikee committed Jul 17, 2023
1 parent c2017c1 commit 8e22a78
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
apply package: "com.android.application"
apply package: "com.facebook.react"

android {
namespace 'com.some.example'
}

react {
libraryName = "justalibrary"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
72 changes: 38 additions & 34 deletions packages/cli-platform-android/src/config/getAndroidProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)}`,
);
Expand Down
8 changes: 5 additions & 3 deletions packages/cli-platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ export function projectConfig(
: findManifest(path.join(sourceDir, appName));
const buildGradlePath = findBuildGradle(sourceDir, false);

if (!manifestPath) {
if (!manifestPath && !buildGradlePath) {
return null;
}

const packageName =
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 {
Expand Down Expand Up @@ -101,7 +103,7 @@ export function dependencyConfig(
: findManifest(sourceDir);
const buildGradlePath = findBuildGradle(sourceDir, true);

if (!manifestPath) {
if (!manifestPath && !buildGradlePath) {
return null;
}

Expand Down

0 comments on commit 8e22a78

Please sign in to comment.