Skip to content

Commit d685901

Browse files
authored
fix(android): hardcoded appName when finding build.gradle (#2652)
* fix(android): hardcoded appName when finding build.gradle * add extra tests * update snapshot * update expectation to reflect error change * Revert "update expectation to reflect error change" This reverts commit c45afea.
1 parent 3e2f7ed commit d685901

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

packages/cli-config-android/src/config/__fixtures__/android.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ function generateValidFileStructureForApp() {
7171
};
7272
}
7373

74+
function generateValidFileStructureForAppWithCustomAppName(
75+
customAppName: string,
76+
) {
77+
return {
78+
'build.gradle': buildGradle,
79+
[customAppName]: {
80+
'build.gradle': appBuildGradle,
81+
},
82+
src: {
83+
'AndroidManifest.xml': manifest,
84+
},
85+
};
86+
}
7487
export const valid = generateValidFileStructureForLib('ReactPackage.java');
7588

7689
export const validKotlin = generateValidFileStructureForLib('ReactPackage.kt');
@@ -83,6 +96,9 @@ export const validKotlinWithDifferentFileName =
8396

8497
export const validApp = generateValidFileStructureForApp();
8598

99+
export const validAppWithCustomAppName =
100+
generateValidFileStructureForAppWithCustomAppName('custom');
101+
86102
export const userConfigManifest = {
87103
src: {
88104
main: {

packages/cli-config-android/src/config/__tests__/__snapshots__/getProjectConfig.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`android::getProjectConfig returns an object with android project configuration for flat structure 1`] = `
44
Object {
55
"appName": "",
6-
"applicationId": "com.some.example",
6+
"applicationId": "com.example",
77
"assets": Array [],
88
"dependencyConfiguration": undefined,
99
"mainActivity": ".MainActivity",

packages/cli-config-android/src/config/__tests__/findBuildGradle.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,26 @@ describe('findBuildGradle for apps', () => {
2121
flat: {
2222
android: mocks.validApp,
2323
},
24+
customPath: {
25+
android: mocks.validAppWithCustomAppName,
26+
},
2427
});
2528
});
2629

2730
it('returns the app gradle path if file exists in the folder', () => {
28-
expect(findBuildGradle('/flat/android', false)).toBe(
31+
expect(findBuildGradle('/flat/android', 'app')).toBe(
2932
'/flat/android/app/build.gradle',
3033
);
3134
});
3235

3336
it('returns `null` if there is no gradle in the app folder', () => {
34-
expect(findBuildGradle('/empty', false)).toBeNull();
37+
expect(findBuildGradle('/empty', 'app')).toBeNull();
38+
});
39+
40+
it('returns the app build.gradle with custom app name', () => {
41+
expect(findBuildGradle('/customPath/android', 'custom')).toBe(
42+
'/customPath/android/custom/build.gradle',
43+
);
3544
});
3645
});
3746

@@ -46,12 +55,12 @@ describe('findBuildGradle for libraries', () => {
4655
});
4756

4857
it('returns the app gradle path if file exists in the folder', () => {
49-
expect(findBuildGradle('/flat/android', true)).toBe(
58+
expect(findBuildGradle('/flat/android', '')).toBe(
5059
'/flat/android/build.gradle',
5160
);
5261
});
5362

5463
it('returns `null` if there is no gradle in the app folder', () => {
55-
expect(findBuildGradle('/empty', true)).toBeNull();
64+
expect(findBuildGradle('/empty', '')).toBeNull();
5665
});
5766
});

packages/cli-config-android/src/config/findBuildGradle.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import fs from 'fs';
22
import path from 'path';
33

4-
export function findBuildGradle(sourceDir: string, isLibrary: boolean) {
4+
/**
5+
* Find the build.gradle file for the given app name.
6+
* This helper is used to find build.gradle file in both apps and libraries.
7+
* For libraries, the appName is empty string.
8+
*/
9+
export function findBuildGradle(sourceDir: string, appName: string) {
510
const buildGradlePath = path.join(
611
sourceDir,
7-
isLibrary ? 'build.gradle' : 'app/build.gradle',
12+
path.join(appName, 'build.gradle'),
813
);
914
const buildGradleKtsPath = path.join(
1015
sourceDir,
11-
isLibrary ? 'build.gradle.kts' : 'app/build.gradle.kts',
16+
path.join(appName, 'build.gradle.kts'),
1217
);
1318

1419
if (fs.existsSync(buildGradlePath)) {

packages/cli-config-android/src/config/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function projectConfig(
4848
const manifestPath = userConfig.manifestPath
4949
? path.join(sourceDir, userConfig.manifestPath)
5050
: findManifest(path.join(sourceDir, appName));
51-
const buildGradlePath = findBuildGradle(sourceDir, false);
51+
const buildGradlePath = findBuildGradle(sourceDir, appName);
5252

5353
if (!manifestPath && !buildGradlePath) {
5454
return null;
@@ -125,7 +125,7 @@ export function dependencyConfig(
125125
const manifestPath = userConfig.manifestPath
126126
? path.join(sourceDir, userConfig.manifestPath)
127127
: findManifest(sourceDir);
128-
const buildGradlePath = findBuildGradle(sourceDir, true);
128+
const buildGradlePath = findBuildGradle(sourceDir, '');
129129
const isPureCxxDependency =
130130
userConfig.cxxModuleCMakeListsModuleName != null &&
131131
userConfig.cxxModuleCMakeListsPath != null &&

0 commit comments

Comments
 (0)