Skip to content

Instantly share code, notes, and snippets.

@bryanltobing
Last active April 28, 2024 12:27
Show Gist options
  • Save bryanltobing/43c89d37bdc6a7bfe0dcf637a5cb9eef to your computer and use it in GitHub Desktop.
Save bryanltobing/43c89d37bdc6a7bfe0dcf637a5cb9eef to your computer and use it in GitHub Desktop.
expo config plugin for react-native-moengage. tested in v8.5.3
const {
withAppDelegate,
withInfoPlist,
withEntitlementsPlist,
withAppBuildGradle,
withMainApplication,
withStringsXml,
AndroidConfig,
} = require('@expo/config-plugins');
const { mergeContents } = require('@expo/config-plugins/build/utils/generateCode');
const withMoEngagePlugin = (config, { appId, sdkVersion = '12.5.04' }) => {
// iOS
config = withAppDelegate(config, (config) => {
config.modResults.contents = config.modResults.contents.replace(
/@implementation AppDelegate/,
`#import <ReactNativeMoEngage/MoEngageInitializer.h>
#import <MoEngageSDK/MoEngageSDK.h>
@implementation AppDelegate`,
);
config.modResults.contents = config.modResults.contents.replace(
/self\.initialProps = @{};/,
`self.initialProps = @{};
[[MoEngageInitializer sharedInstance] initializeDefaultInstance: launchOptions];`,
);
return config;
});
config = withInfoPlist(config, (config) => {
config.modResults.MoEngage = {
APP_GROUP_ID: 'GROUP_ID',
DATA_CENTER: 'DATA_CENTER_01',
MoEngage_APP_ID: appId,
};
return config;
});
config = withEntitlementsPlist(config, (config) => {
const bundleIdentifier = config.ios.bundleIdentifier;
config.modResults['com.apple.security.application-groups'] = [
`group.${bundleIdentifier}.MoEngage`,
];
return config;
});
// android
config = withAppBuildGradle(config, (config) => {
const appBuildGradle = config.modResults;
if (appBuildGradle.language === 'groovy') {
appBuildGradle.contents = mergeContents({
tag: 'react-native-moengage',
src: appBuildGradle.contents,
anchor: /dependencies/,
newSrc: [
'',
` implementation("com.moengage:moe-android-sdk:${sdkVersion}")`,
' implementation("androidx.core:core:1.9.0")',
' implementation("androidx.appcompat:appcompat:1.4.2")',
' implementation("androidx.lifecycle:lifecycle-process:2.7.0")',
'',
].join('\n'),
offset: 1,
comment: '//',
}).contents;
}
return config;
});
config = withMainApplication(config, (config) => {
if (config.modResults.language === 'kt') {
const mainApplicationContents = config.modResults.contents;
const moengageImports = `import com.moengage.core.DataCenter
import com.moengage.core.MoEngage
import com.moengage.react.MoEInitializer`;
const moengageInitialization = `val moEngage = MoEngage.Builder(this, resources.getString(R.string.moengage_app_id))
MoEInitializer.initializeDefaultInstance(applicationContext, moEngage)`;
// Check if the necessary imports and initialization code are already present
if (
!mainApplicationContents.includes(moengageImports) ||
!mainApplicationContents.includes(moengageInitialization)
) {
// Add the necessary imports
config.modResults.contents = mainApplicationContents.replace(
/import com\.facebook\.react\.ReactApplication/g,
`import com.facebook.react.ReactApplication
${moengageImports}`,
);
// Add the initialization code
config.modResults.contents = config.modResults.contents.replace(
/super\.onCreate\(\)/g,
`super.onCreate()
${moengageInitialization}`,
);
}
}
return config;
});
function setStrings(strings, { name = 'expo_custom_value', value }) {
// Helper to add string.xml JSON items or overwrite existing items with the same name.
return AndroidConfig.Strings.setStringItem(
[
// XML represented as JSON
// <string name="expo_custom_value" translatable="false">value</string>
{ $: { name, translatable: 'false' }, _: value },
],
strings,
);
}
config = withStringsXml(config, (config) => {
config.modResults = setStrings(config.modResults, { name: 'moengage_app_id', value: appId });
return config;
});
return config;
};
module.exports = withMoEngagePlugin;
@bryanltobing
Copy link
Author

Usage

in app.config.js import the plugin

const withMoEngagePlugin = require('./plugins/withMoEngagePlugin');

then apply it to plugins

module.exports = {
    plugins: [
        [withMoEngagePlugin, 
            { 
                appId: process.env.EXPO_PUBLIC_MOENGAGE_APP_ID, 
                sdkVersion: process.env.EXPO_PUBLIC_MOENGAGE_SDK_VERSION 
          }]
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment