Skip to content

Commit 5eb1409

Browse files
refactor: read firebase-messaging version from native module at prebuild time
Instead of hardcoding the firebase-messaging version, read it from the native module's android/build.gradle so the app dependency stays in sync automatically when the SDK is updated. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 38dcf13 commit 5eb1409

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

__tests__/withAndroidPushNotifications.test.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ describe('withAndroidPushNotifications', () => {
4242
let writeFileSyncSpy: jest.SpyInstance;
4343
let readFileSyncSpy: jest.SpyInstance;
4444

45-
const fakeBuildGradle = `
45+
const fakeNativeBuildGradle = `
46+
dependencies {
47+
implementation "com.google.firebase:firebase-messaging:24.1.2"
48+
implementation 'io.intercom.android:intercom-sdk:17.4.5'
49+
}
50+
`;
51+
52+
const fakeAppBuildGradle = `
4653
android {
4754
compileSdkVersion 34
4855
}
@@ -59,7 +66,13 @@ dependencies {
5966
.mockReturnValue(undefined);
6067
readFileSyncSpy = jest
6168
.spyOn(fs, 'readFileSync')
62-
.mockReturnValue(fakeBuildGradle);
69+
.mockImplementation((filePath: any) => {
70+
const p = String(filePath);
71+
if (p.includes(path.join('app', 'build.gradle'))) {
72+
return fakeAppBuildGradle;
73+
}
74+
return fakeNativeBuildGradle;
75+
});
6376
});
6477

6578
afterEach(() => {
@@ -154,21 +167,25 @@ dependencies {
154167
});
155168

156169
describe('Gradle dependency', () => {
157-
test('adds firebase-messaging when not present', () => {
170+
test('adds firebase-messaging with version from native module', () => {
158171
const config = createMockConfig('com.example.myapp');
159172
withAndroidPushNotifications(config as any, {} as any);
160173

161174
const gradleWriteCall = writeFileSyncSpy.mock.calls.find((call: any[]) =>
162175
(call[0] as string).includes('build.gradle')
163176
);
164177
expect(gradleWriteCall).toBeDefined();
165-
expect(gradleWriteCall[1]).toContain('firebase-messaging');
178+
expect(gradleWriteCall[1]).toContain('firebase-messaging:24.1.2');
166179
});
167180

168181
test('skips adding firebase-messaging when already present', () => {
169-
readFileSyncSpy.mockReturnValue(
170-
'dependencies {\n implementation("com.google.firebase:firebase-messaging:23.0.0")\n}'
171-
);
182+
readFileSyncSpy.mockImplementation((filePath: any) => {
183+
const p = String(filePath);
184+
if (p.includes(path.join('app', 'build.gradle'))) {
185+
return 'dependencies {\n implementation("com.google.firebase:firebase-messaging:23.0.0")\n}';
186+
}
187+
return fakeNativeBuildGradle;
188+
});
172189
const config = createMockConfig('com.example.myapp');
173190
withAndroidPushNotifications(config as any, {} as any);
174191

src/expo-plugins/withAndroidPushNotifications.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,19 @@ const writeFirebaseService: ConfigPlugin<IntercomPluginProps> = (_config) =>
7777
// The native module declares firebase-messaging as an `implementation`
7878
// dependency, which keeps it private to the library. Since our generated
7979
// service lives in the app module, we need firebase-messaging on the
80-
// app's compile classpath too.
80+
// app's compile classpath too. We read the version from the native
81+
// module's build.gradle so it stays in sync automatically.
82+
const nativeBuildGradle = fs.readFileSync(
83+
path.join(__dirname, '..', '..', 'android', 'build.gradle'),
84+
'utf-8'
85+
);
86+
const versionMatch = nativeBuildGradle.match(
87+
/com\.google\.firebase:firebase-messaging:([\d.]+)/
88+
);
89+
const firebaseMessagingVersion = versionMatch
90+
? versionMatch[1]
91+
: '24.1.2';
92+
8193
const buildGradlePath = path.join(
8294
projectRoot,
8395
'android',
@@ -88,7 +100,7 @@ const writeFirebaseService: ConfigPlugin<IntercomPluginProps> = (_config) =>
88100
if (!buildGradle.includes('firebase-messaging')) {
89101
const updatedBuildGradle = buildGradle.replace(
90102
/dependencies\s*\{/,
91-
`dependencies {\n implementation("com.google.firebase:firebase-messaging:24.1.2")`
103+
`dependencies {\n implementation("com.google.firebase:firebase-messaging:${firebaseMessagingVersion}")`
92104
);
93105
fs.writeFileSync(buildGradlePath, updatedBuildGradle, 'utf-8');
94106
}

0 commit comments

Comments
 (0)