I've noticed a behavior when running expo prebuild in my React Native project using Expo. For the iOS platform, it completely replaces the AppDelegate.mm file and regenerates new icons. However, on Android, I don’t see as many changes.
The issue I'm facing is that whenever I modify AppDelegate.mm (e.g., for setting up a native module) and then run expo prebuild again, all my changes are lost.
Is this the expected behavior? If so, what’s the recommended approach to persist changes in AppDelegate.mm while using Expo prebuild? Also, why doesn’t Android seem to have as many changes compared to iOS?
Would appreciate any insights on this. Thanks!
I've noticed a behavior when running expo prebuild in my React Native project using Expo. For the iOS platform, it completely replaces the AppDelegate.mm file and regenerates new icons. However, on Android, I don’t see as many changes.
The issue I'm facing is that whenever I modify AppDelegate.mm (e.g., for setting up a native module) and then run expo prebuild again, all my changes are lost.
Is this the expected behavior? If so, what’s the recommended approach to persist changes in AppDelegate.mm while using Expo prebuild? Also, why doesn’t Android seem to have as many changes compared to iOS?
Would appreciate any insights on this. Thanks!
Share Improve this question edited 6 hours ago Samarth Kadam asked yesterday Samarth KadamSamarth Kadam 111 bronze badge1 Answer
Reset to default 0Create a custom Expo config plugin (plugin.js)
This ensures that your modifications are applied every time expo prebuild
runs.
const { withDangerousMod } = require('@expo/config-plugins');
const fs = require('fs');
const path = require('path');
module.exports = function withCustomAppDelegate(config) {
return withDangerousMod(config, ['ios', (config) => {
const appDelegatePath = path.join(config.modRequest.platformProjectRoot, 'MyApp', 'AppDelegate.mm');
let content = fs.readFileSync(appDelegatePath, 'utf-8');
// Example: Add a custom import or native module setup
if (!content.includes('#import "MyNativeModule.h"')) {
content = content.replace(
'#import <React/RCTBridgeModule.h>',
`#import <React/RCTBridgeModule.h>\n#import "MyNativeModule.h"`
);
}
fs.writeFileSync(appDelegatePath, content);
return config;
}]);
};