-
Notifications
You must be signed in to change notification settings - Fork 906
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read main activity from
AndroidManifest.xml
automatically
- Loading branch information
1 parent
4650a8d
commit 3113c8b
Showing
7 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/cli-platform-android/src/config/readAndroidManifest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import fs from 'fs'; | ||
import {XMLParser, XMLValidator} from 'fast-xml-parser'; | ||
|
||
const MAIN_ACTION = 'android.intent.action.MAIN'; | ||
const LAUNCHER = 'android.intent.category.LAUNCHER'; | ||
|
||
interface Activity { | ||
[x: string]: any; | ||
} | ||
|
||
interface IntentFilter { | ||
action: | ||
| { | ||
'@_android:name': string; | ||
} | ||
| { | ||
'@_android:name': string; | ||
}[]; | ||
category: | ||
| { | ||
'@_android:name': string; | ||
} | ||
| { | ||
'@_android:name': string; | ||
}[]; | ||
} | ||
|
||
/** | ||
* Reads the AndroidManifest.xml file and returns the name of the main activity. | ||
*/ | ||
|
||
export default function readAndroidManifest( | ||
manifestPath: string, | ||
): string | null { | ||
try { | ||
const xmlParser = new XMLParser({ignoreAttributes: false}); | ||
const manifestContent = fs.readFileSync(manifestPath, {encoding: 'utf8'}); | ||
|
||
if (XMLValidator.validate(manifestContent)) { | ||
const {manifest} = xmlParser.parse(manifestContent); | ||
|
||
const application = manifest.application || {}; | ||
const activity = application.activity || {}; | ||
|
||
let activities: Activity[] = []; | ||
|
||
if (!Array.isArray(activity)) { | ||
activities = [activity]; | ||
} else { | ||
activities = activity; | ||
} | ||
|
||
const mainActivity = activities.find((act: Activity) => { | ||
let intentFilters = act['intent-filter']; | ||
|
||
if (!intentFilters) { | ||
return false; | ||
} | ||
|
||
if (!Array.isArray(intentFilters)) { | ||
intentFilters = [intentFilters]; | ||
} | ||
|
||
return intentFilters.find((intentFilter: IntentFilter) => { | ||
const {action, category} = intentFilter; | ||
|
||
let actions; | ||
let categories; | ||
|
||
if (!Array.isArray(action)) { | ||
actions = [action]; | ||
} else { | ||
actions = action; | ||
} | ||
|
||
if (!Array.isArray(category)) { | ||
categories = [category]; | ||
} else { | ||
categories = category; | ||
} | ||
|
||
if (actions && categories) { | ||
const parsedActions: string[] = actions.map( | ||
({'@_android:name': name}) => name, | ||
); | ||
|
||
const parsedCategories: string[] = categories.map( | ||
({'@_android:name': name}) => name, | ||
); | ||
|
||
return ( | ||
parsedActions.includes(MAIN_ACTION) && | ||
parsedCategories.includes(LAUNCHER) | ||
); | ||
} | ||
|
||
return false; | ||
}); | ||
}); | ||
|
||
return mainActivity | ||
? mainActivity['@_android:name'].replace('.', '') | ||
: null; | ||
} else { | ||
return null; | ||
} | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters