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

feat: extract android applicationId #1951

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -257,6 +257,7 @@ function installAndLaunchOnDevice(
tryLaunchAppOnDevice(
selectedDevice,
androidProject.packageName,
androidProject.applicationId,
adbPath,
args,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ async function runOnAllDevices(
if (args.binaryPath && device) {
tryInstallAppOnDevice(args, adbPath, device, androidProject);
}
tryLaunchAppOnDevice(device, androidProject.packageName, adbPath, args);
tryLaunchAppOnDevice(
device,
androidProject.packageName,
androidProject.applicationId,
adbPath,
args,
);
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {logger, CLIError} from '@react-native-community/cli-tools';
function tryLaunchAppOnDevice(
device: string | void,
packageName: string,
applicationId: string,
adbPath: string,
args: Flags,
) {
const {appId, appIdSuffix} = args;
const packageNameWithSuffix = [appId || packageName, appIdSuffix]
const packageNameWithSuffix = [appId || applicationId, appIdSuffix]
TMisiukiewicz marked this conversation as resolved.
Show resolved Hide resolved
.filter(Boolean)
.join('.');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ apply package: "com.facebook.react"

android {
namespace 'com.some.example'
adamTrz marked this conversation as resolved.
Show resolved Hide resolved
defaultConfig {
applicationId "com.example"
}
}

react {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Object {
exports[`android::getProjectConfig returns an object with android project configuration for nested structure 1`] = `
Object {
"appName": "app",
"applicationId": "com.example",
"dependencyConfiguration": undefined,
"packageName": "com.some.example",
"sourceDir": "/nested/android",
Expand Down
16 changes: 16 additions & 0 deletions packages/cli-platform-android/src/config/getAndroidProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ export function parseNamespaceFromBuildGradleFile(buildGradle: string) {
export function validatePackageName(packageName: string) {
return /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/i.test(packageName);
}

// Search for applicationId at defaultConfig object
export function parseApplicationIdFromBuildGradleFile(buildGradlePath: string) {
const buildGradle = fs.readFileSync(buildGradlePath, 'utf8');

const matchArray = buildGradle.match(/defaultConfig\s*{([\s\S]*?)}/);

if (matchArray && matchArray.length > 0) {
const appIdMatchArray = matchArray[1].match(
/applicationId\s*[=]*\s*["'](.+?)["']/,
);
return appIdMatchArray?.[1] ?? '';
} else {
return null;
}
}
19 changes: 18 additions & 1 deletion packages/cli-platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
AndroidDependencyParams,
AndroidDependencyConfig,
} from '@react-native-community/cli-types';
import {getPackageName} from './getAndroidProject';
import {
getPackageName,
parseApplicationIdFromBuildGradleFile,
} from './getAndroidProject';
import {findLibraryName} from './findLibraryName';
import {findComponentDescriptors} from './findComponentDescriptors';
import {findBuildGradle} from './findBuildGradle';
Expand Down Expand Up @@ -59,17 +62,31 @@ export function projectConfig(
);
}

const applicationId =
getApplicationId(buildGradlePath as string) || packageName;

return {
sourceDir,
appName,
packageName,
applicationId,
dependencyConfiguration: userConfig.dependencyConfiguration,
watchModeCommandParams: userConfig.watchModeCommandParams,
unstable_reactLegacyComponentNames:
userConfig.unstable_reactLegacyComponentNames,
};
}

function getApplicationId(buildGradlePath: string) {
let appId = '';

const applicationId = parseApplicationIdFromBuildGradleFile(buildGradlePath);
if (applicationId) {
appId = applicationId;
}
return appId;
}

function getAppName(sourceDir: string, userConfigAppName: string | undefined) {
let appName = '';
if (
Expand Down
1 change: 1 addition & 0 deletions packages/cli-types/src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface AndroidProjectConfig {
sourceDir: string;
appName: string;
packageName: string;
applicationId: string;
dependencyConfiguration?: string;
watchModeCommandParams?: string[];
unstable_reactLegacyComponentNames?: string[] | null;
Expand Down