Skip to content

Commit

Permalink
feat: extract android applicationId
Browse files Browse the repository at this point in the history
  • Loading branch information
adamTrz committed Jun 2, 2023
1 parent 4356f57 commit 6ae39f6
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Path to a custom `AndroidManifest.xml`

Custom package name to override one from `AndroidManifest.xml`

#### platforms.android.applicationId

Custom applicationId to be launched after app is installed.

#### platforms.android.packageImportPath

Custom package import. For example: `import com.acme.AwesomePackage;`.
Expand Down
5 changes: 5 additions & 0 deletions docs/projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type AndroidProjectParams = {
appName?: string;
manifestPath?: string;
packageName?: string;
applicationId?: string;
dependencyConfiguration?: string;
watchModeCommandParams?: string[];
unstable_reactLegacyComponentNames?: string[] | null;
Expand Down Expand Up @@ -116,6 +117,10 @@ See [`dependency.platforms.android.manifestPath`](dependencies.md#platformsandro

See [`dependency.platforms.android.packageName`](dependencies.md#platformsandroidpackagename)

#### project.android.applicationId

See [`dependency.platforms.android.applicationId`](dependencies.md#platformsandroidapplicationId)

#### project.android.dependencyConfiguration

See [`dependency.platforms.android.configuration`](dependencies.md#platformsandroiddependencyconfiguration)
Expand Down
1 change: 1 addition & 0 deletions packages/cli-config/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const projectConfig = t
appName: t.string(),
manifestPath: t.string(),
packageName: t.string(),
applicationId: t.string(),
dependencyConfiguration: t.string(),
watchModeCommandParams: t.array().items(t.string()),
unstable_reactLegacyComponentNames: t
Expand Down
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]
.filter(Boolean)
.join('.');

Expand Down
14 changes: 14 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,17 @@ 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(buildGradle: string) {
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;
}
}
30 changes: 29 additions & 1 deletion packages/cli-platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ 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';
import {CLIError} from '@react-native-community/cli-tools';
import chalk from 'chalk';

/**
* Gets android project config by analyzing given folder and taking some
Expand Down Expand Up @@ -59,17 +63,41 @@ export function projectConfig(
);
}

const applicationId =
userConfig.applicationId || getApplicationId(buildGradlePath);

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

function getApplicationId(buildGradlePath: string | null) {
if (!buildGradlePath) {
throw new CLIError(
`Failed to build the app: build.gradle file not found.
We couldn't parse applicationId of the project from your build.gradle[.kts] file at ${chalk.underline.dim(
`${buildGradlePath}`,
)}`,
);
}

let appId = '';
const buildGradle = fs.readFileSync(buildGradlePath, 'utf8');

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

function getAppName(sourceDir: string, userConfigAppName: string | undefined) {
let appName = '';
if (
Expand Down
2 changes: 2 additions & 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 All @@ -12,6 +13,7 @@ export type AndroidProjectParams = {
appName?: string;
manifestPath?: string;
packageName?: string;
applicationId?: string;
dependencyConfiguration?: string;
watchModeCommandParams?: string[];
unstable_reactLegacyComponentNames?: string[] | null;
Expand Down

0 comments on commit 6ae39f6

Please sign in to comment.