Skip to content

Commit

Permalink
Don't set package name in manifest if it wasn't already set. #188
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed May 9, 2023
1 parent c7e7be4 commit 1ab56ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/project/src/android/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ export class AndroidProject extends PlatformProject {
*/
async setPackageName(packageName: string) {
const sourceDir = join(this.getAppRoot()!, 'src', 'main', 'java');
let hadPackageAttr = false;
let oldPackageName = await this.manifest
.getDocumentElement()
?.getAttribute('package');


if (!oldPackageName) {
oldPackageName = await this.appBuildGradle?.getApplicationId();
} else {
hadPackageAttr = true;
}

const oldPackageParts = oldPackageName?.split('.') ?? [];
Expand All @@ -173,12 +175,17 @@ export class AndroidProject extends PlatformProject {
);
}

this.manifest.getDocumentElement()?.setAttribute('package', packageName);
let activityName = '.MainActivity';
if (hadPackageAttr) {
this.manifest.getDocumentElement()?.setAttribute('package', packageName);
activityName = `${packageName}.MainActivity`;
}

await this.appBuildGradle?.setApplicationId(packageName);
await this.appBuildGradle?.setNamespace(packageName);
Logger.v('android', 'setPackageName', `set manifest package attribute and applicationId to ${packageName}`);
this.manifest.setAttrs('manifest/application/activity', {
'android:name': `${packageName}.MainActivity`,
'android:name': activityName
});
Logger.v('android', 'setPackageName', `set <activity android:name="${packageName}.MainActivity"`);

Expand Down
2 changes: 1 addition & 1 deletion packages/project/test/project.android.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,6 @@ describe('project - android - capacitor v5', () => {
expect(newSource.indexOf('package com.ionicframework.awesome;')).toBe(0);
expect(!(await pathExists(join(project.config.android?.path!, 'app/src/main/java/io')))).toBe(true);
const activity = project.android?.getAndroidManifest().find('manifest/application/activity');
expect(activity?.[0].getAttribute('android:name')).toBe('com.ionicframework.awesome.MainActivity');
expect(activity?.[0].getAttribute('android:name')).toBe('.MainActivity');
});
});

0 comments on commit 1ab56ea

Please sign in to comment.