Skip to content

Commit

Permalink
feat: add plugin withAndroidMainActivityAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
achorein committed Feb 23, 2024
1 parent bbf0b7d commit 372d620
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
71 changes: 71 additions & 0 deletions plugin/src/android/withAndroidMainActivityAttributes.ts
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;
});
};
3 changes: 2 additions & 1 deletion plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

// Android
import { withAndroidIntentFilters } from "./android/withAndroidIntentFilters";

import { withAndroidMainActivityAttributes } from "./android/withAndroidMainActivityAttributes";
// iOS
import { withAppEntitlements } from "./ios/withIosAppEntitlements";
import { withShareExtensionConfig } from "./ios/withIosShareExtensionConfig";
Expand All @@ -27,6 +27,7 @@ const withShareMenu: ConfigPlugin<Parameters> = createRunOncePlugin(
() => withShareExtensionXcodeTarget(config, params),
// Android
() => withAndroidIntentFilters(config, params),
() => withAndroidMainActivityAttributes(config, params),
]);
},
pkg.name,
Expand Down

0 comments on commit 372d620

Please sign in to comment.