getAndroidPackageName method
Extracts the Android package name (applicationId/namespace) from Manifest, MainActivity file, or app level build.gradle.
Implementation
String getAndroidPackageName() {
// 1. Try reading package attribute from AndroidManifest.xml
try {
final manifestContent = FileUtils.readAsString(androidManifestPath);
final match = RegExp(r'package="([^"]+)"').firstMatch(manifestContent);
if (match != null && match.group(1) != null) {
return match.group(1)!;
}
} catch (_) {}
// 2. Try package statement in MainActivity
try {
final info = findMainActivity();
final content = FileUtils.readAsString(info.path);
final match = RegExp(r'package\s+([a-zA-Z0-9_.]+);?').firstMatch(content);
if (match != null && match.group(1) != null) {
return match.group(1)!;
}
} catch (_) {}
// 3. Try reading build.gradle (namespace or applicationId)
try {
final buildGradlePath = PathUtils.join(rootPath, 'android', 'app', 'build.gradle');
if (FileUtils.exists(buildGradlePath)) {
final content = FileUtils.readAsString(buildGradlePath);
final match = RegExp(r"""namespace\s+["']([^"']+)["']""").firstMatch(content);
if (match != null && match.group(1) != null) {
return match.group(1)!;
}
final match2 = RegExp(r"""applicationId\s+["']([^"']+)["']""").firstMatch(content);
if (match2 != null && match2.group(1) != null) {
return match2.group(1)!;
}
}
} catch (_) {}
throw DeepLinkException('Could not resolve Android package name/applicationId.');
}