Skip to content

Commit

Permalink
fix(android): launching app on simulator with different appId than pa…
Browse files Browse the repository at this point in the history
…ckageName (#2195)
  • Loading branch information
thymikee committed Dec 11, 2023
1 parent 9688cf4 commit 5862d7a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('--appFolder', () => {
const androidProject: AndroidProjectConfig = {
appName: 'app',
packageName: 'com.test',
applicationId: 'com.test',
sourceDir: '/android',
mainActivity: '.MainActivity',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {AndroidProjectConfig} from '@react-native-community/cli-types';
import tryLaunchAppOnDevice from '../tryLaunchAppOnDevice';
import {Flags} from '..';
import execa from 'execa';

jest.mock('execa');
jest.mock('../getAdbPath');
jest.mock('../tryLaunchEmulator');

const adbPath = 'path/to/adb';
const device = 'emulator-5554';
let args: Flags = {
activeArchOnly: false,
packager: true,
port: 8081,
terminal: 'iTerm.app',
appId: '',
appIdSuffix: '',
listDevices: false,
};

let androidProject: AndroidProjectConfig = {
sourceDir: '/Users/thymikee/Developer/tmp/App73/android',
appName: 'app',
packageName: 'com.myapp',
applicationId: 'com.myapp.custom',
mainActivity: '.MainActivity',
dependencyConfiguration: undefined,
watchModeCommandParams: undefined,
unstable_reactLegacyComponentNames: undefined,
};

const shellStartCommand = ['shell', 'am', 'start'];
const actionCategoryFlags = [
'-a',
'android.intent.action.MAIN',
'-c',
'android.intent.category.LAUNCHER',
];

beforeEach(() => {
jest.clearAllMocks();
});

test('launches adb shell with intent to launch com.myapp.MainActivity with different appId than packageName on a simulator', () => {
tryLaunchAppOnDevice(device, androidProject, adbPath, args);

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
'-s',
'emulator-5554',
...shellStartCommand,
'-n',
'com.myapp.custom/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('launches adb shell with intent to launch com.myapp.MainActivity with same appId as packageName on a simulator', () => {
tryLaunchAppOnDevice(
device,
{...androidProject, applicationId: 'com.myapp'},
adbPath,
args,
);

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
'-s',
'emulator-5554',
...shellStartCommand,
'-n',
'com.myapp/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('launches adb shell with intent to launch com.myapp.MainActivity with different appId than packageName on a device (without calling simulator)', () => {
tryLaunchAppOnDevice(undefined, androidProject, adbPath, args);

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
...shellStartCommand,
'-n',
'com.myapp.custom/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('--appId flag overwrites applicationId setting in androidProject', () => {
tryLaunchAppOnDevice(undefined, androidProject, adbPath, {
...args,
appId: 'my.app.id',
});

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
...shellStartCommand,
'-n',
'my.app.id/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});

test('appIdSuffix Staging is appended to applicationId', () => {
tryLaunchAppOnDevice(undefined, androidProject, adbPath, {
...args,
appIdSuffix: 'Staging',
});

expect(execa.sync).toHaveBeenCalledWith(
'path/to/adb',
[
...shellStartCommand,
'-n',
'com.myapp.custom.Staging/com.myapp.MainActivity',
...actionCategoryFlags,
],
{stdio: 'inherit'},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {checkUsers, promptForUser} from './listAndroidUsers';
export interface Flags extends BuildFlags {
appId: string;
appIdSuffix: string;
mainActivity: string;
mainActivity?: string;
port: number;
terminal?: string;
packager?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function tryLaunchAppOnDevice(
.join('.');

const activityToLaunch = mainActivity.includes('.')
? mainActivity
? [packageName, mainActivity].join('')
: [packageName, mainActivity].filter(Boolean).join('.');

try {
Expand Down

0 comments on commit 5862d7a

Please sign in to comment.