Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use custom app name when getting build.gradle #2524

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ function generateValidFileStructureForApp() {
};
}

function generateValidFileStructureForAppWithCustomAppName(
customAppName: string,
) {
return {
'build.gradle': buildGradle,
[customAppName]: {
'build.gradle': appBuildGradle,
},
src: {
'AndroidManifest.xml': manifest,
},
};
}

export const valid = generateValidFileStructureForLib('ReactPackage.java');

export const validKotlin = generateValidFileStructureForLib('ReactPackage.kt');
Expand All @@ -83,6 +97,9 @@ export const validKotlinWithDifferentFileName =

export const validApp = generateValidFileStructureForApp();

export const validAppWithCustomAppName =
generateValidFileStructureForAppWithCustomAppName('custom');

export const userConfigManifest = {
src: {
main: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ describe('findBuildGradle for apps', () => {
flat: {
android: mocks.validApp,
},
customPath: {
android: mocks.validAppWithCustomAppName,
},
});
});

Expand All @@ -33,6 +36,12 @@ describe('findBuildGradle for apps', () => {
it('returns `null` if there is no gradle in the app folder', () => {
expect(findBuildGradle('/empty', false)).toBeNull();
});

it('returns the app build.gradle with custom app name', () => {
expect(findBuildGradle('/customPath/android', false, 'custom')).toBe(
'/customPath/android/custom/build.gradle',
);
});
});

describe('findBuildGradle for libraries', () => {
Expand Down
10 changes: 7 additions & 3 deletions packages/cli-platform-android/src/config/findBuildGradle.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import fs from 'fs';
import path from 'path';

export function findBuildGradle(sourceDir: string, isLibrary: boolean) {
export function findBuildGradle(
sourceDir: string,
isLibrary: boolean,
appName = 'app',
) {
const buildGradlePath = path.join(
sourceDir,
isLibrary ? 'build.gradle' : 'app/build.gradle',
isLibrary ? 'build.gradle' : `${appName}/build.gradle`,
);
const buildGradleKtsPath = path.join(
sourceDir,
isLibrary ? 'build.gradle.kts' : 'app/build.gradle.kts',
isLibrary ? 'build.gradle.kts' : `${appName}/build.gradle.kts`,
);

if (fs.existsSync(buildGradlePath)) {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli-platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export function projectConfig(
const manifestPath = userConfig.manifestPath
? path.join(sourceDir, userConfig.manifestPath)
: findManifest(path.join(sourceDir, appName));
const buildGradlePath = findBuildGradle(sourceDir, false);
const buildGradlePath = findBuildGradle(
sourceDir,
false,
appName === '' ? 'app' : appName,
);

if (!manifestPath && !buildGradlePath) {
return null;
Expand Down
Loading