-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin withAndroidMainActivityAttributes
- Loading branch information
Showing
2 changed files
with
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*! | ||
* Plugin created for Expo Share Intent Demo (https://github.com/achorein/expo-share-intent-demo) | ||
* author: achorein (https://github.com/achorein) | ||
* | ||
* inspired by: https://forums.expo.dev/t/how-to-edit-android-manifest-was-build/65663/4 | ||
*/ | ||
import { | ||
AndroidManifest, | ||
ConfigPlugin, | ||
withAndroidManifest, | ||
} from "@expo/config-plugins"; | ||
|
||
import { Parameters } from "../types"; | ||
|
||
function addAttributesToMainActivity( | ||
androidManifest: AndroidManifest, | ||
attributes: Parameters["androidMainActivityAttributes"], | ||
) { | ||
const { manifest } = androidManifest; | ||
|
||
if (!Array.isArray(manifest["application"])) { | ||
console.warn( | ||
"withAndroidMainActivityAttributes: No application array in manifest?", | ||
); | ||
return androidManifest; | ||
} | ||
|
||
const application = manifest["application"].find( | ||
(item) => item.$["android:name"] === ".MainApplication", | ||
); | ||
if (!application) { | ||
console.warn("withAndroidMainActivityAttributes: No .MainApplication?"); | ||
return androidManifest; | ||
} | ||
|
||
if (!Array.isArray(application["activity"])) { | ||
console.warn( | ||
"withAndroidMainActivityAttributes: No activity array in .MainApplication?", | ||
); | ||
return androidManifest; | ||
} | ||
|
||
const activity = application["activity"].find( | ||
(item) => item.$["android:name"] === ".MainActivity", | ||
); | ||
if (!activity) { | ||
console.warn("withAndroidMainActivityAttributes: No .MainActivity?"); | ||
return androidManifest; | ||
} | ||
|
||
const newAttributes = attributes || { | ||
"android:launchMode": "singleTask", | ||
}; | ||
|
||
activity.$ = { ...activity.$, ...newAttributes }; | ||
|
||
return androidManifest; | ||
} | ||
|
||
export const withAndroidMainActivityAttributes: ConfigPlugin<Parameters> = ( | ||
config, | ||
parameters, | ||
) => { | ||
return withAndroidManifest(config, (config) => { | ||
config.modResults = addAttributesToMainActivity( | ||
config.modResults, | ||
parameters?.androidMainActivityAttributes, | ||
); | ||
return config; | ||
}); | ||
}; |
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