Skip to content

Commit 7513d16

Browse files
committed
fix: kotlin support
1 parent f5e28ac commit 7513d16

6 files changed

Lines changed: 62 additions & 34 deletions

build/android/mainApplicationDependency.js

Lines changed: 19 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/android/mainApplicationDependency.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/utils/addBelowAnchorIfNotFound.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/utils/addBelowAnchorIfNotFound.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/android/mainApplicationDependency.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,38 @@ export const withAndroidMainApplicationDependency: ConfigPlugin<
1212
> = (config) => {
1313
return withMainApplication(config, (mainApplicationProps) => {
1414
// Import the plugin class.
15-
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
16-
mainApplicationProps.modResults.contents,
17-
"import expo.modules.ReactNativeHostWrapper;",
18-
"import com.microsoft.codepush.react.CodePush;"
19-
);
15+
const hostWrapperClass = "import expo.modules.ReactNativeHostWrapper";
16+
const codePushClass = "import com.microsoft.codepush.react.CodePush";
17+
18+
// Expo 50 uses Kotlin and does not require the ;
19+
if (mainApplicationProps.modResults.contents.includes(hostWrapperClass)) {
20+
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
21+
mainApplicationProps.modResults.contents,
22+
hostWrapperClass,
23+
codePushClass
24+
);
25+
}
26+
27+
// Expo 49 uses Java and requires the ;
28+
if (
29+
mainApplicationProps.modResults.contents.includes(`${hostWrapperClass};`)
30+
) {
31+
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
32+
mainApplicationProps.modResults.contents,
33+
`${hostWrapperClass};`,
34+
`${codePushClass};`
35+
);
36+
}
37+
38+
/**
39+
* Override the getJSBundleFile method in order to let
40+
* the CodePush runtime determine where to get the JS
41+
* bundle location from on each app start
42+
*/
2043

2144
// The default on Expo 50, which uses kotlin
2245
const kotlinAnchor = `override fun getJSMainModuleName(): String = ".expo/.virtual-metro-entry"`;
2346
if (mainApplicationProps.modResults.contents.includes(kotlinAnchor)) {
24-
/**
25-
* Override the getJSBundleFile method in order to let
26-
* the CodePush runtime determine where to get the JS
27-
* bundle location from on each app start
28-
*/
2947
const kotlinJSBundleFileOverride = `
3048
override fun getJSBundleFile(): String? {
3149
return CodePush.getJSBundleFile()
@@ -36,14 +54,10 @@ export const withAndroidMainApplicationDependency: ConfigPlugin<
3654
kotlinAnchor,
3755
kotlinJSBundleFileOverride
3856
);
57+
return mainApplicationProps;
3958
}
4059

41-
/**
42-
* Override the getJSBundleFile method in order to let
43-
* the CodePush runtime determine where to get the JS
44-
* bundle location from on each app start
45-
*/
46-
const getJSBundleFileOverride = `
60+
const javaJSBundleFileOverride = `
4761
@Override
4862
protected String getJSBundleFile() {
4963
return CodePush.getJSBundleFile();
@@ -59,7 +73,7 @@ export const withAndroidMainApplicationDependency: ConfigPlugin<
5973
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
6074
mainApplicationProps.modResults.contents,
6175
defaultReactNativeAnchor,
62-
getJSBundleFileOverride
76+
javaJSBundleFileOverride
6377
);
6478

6579
return mainApplicationProps;
@@ -73,7 +87,7 @@ export const withAndroidMainApplicationDependency: ConfigPlugin<
7387
mainApplicationProps.modResults.contents = addBelowAnchorIfNotFound(
7488
mainApplicationProps.modResults.contents,
7589
reactNativeHostAnchor,
76-
getJSBundleFileOverride
90+
javaJSBundleFileOverride
7791
);
7892

7993
return mainApplicationProps;

src/utils/addBelowAnchorIfNotFound.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@ export function addBelowAnchorIfNotFound(
88
return originalString.replace(anchor, `${anchor}\n${stringToBeAdded}`);
99
}
1010

11+
if (!originalString.includes(anchor)) {
12+
throw new Error(
13+
`The anchor string "${anchor}" was not found in the original string.`
14+
);
15+
}
16+
1117
return originalString;
1218
}

0 commit comments

Comments
 (0)