-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathwithViroAndroid.ts
354 lines (332 loc) · 12.8 KB
/
withViroAndroid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import {
ConfigPlugin,
ExportedConfigWithProps,
withAndroidManifest,
withAppBuildGradle,
withDangerousMod,
withPlugins,
withProjectBuildGradle,
withSettingsGradle,
} from "@expo/config-plugins";
import { ExpoConfig } from "@expo/config-types";
import fs from "fs";
import path from "path";
import { insertLinesHelper } from "./util/insertLinesHelper";
import { ViroConfigurationOptions, XrMode } from "./withViro";
let viroPluginConfig = ["AR", "GVR"];
const withBranchAndroid: ConfigPlugin<ViroConfigurationOptions> = (config) => {
// Directly edit MainApplication.java
return withDangerousMod(config, [
"android",
async (config) => {
let mainApplicationPath = "";
let isJava: boolean;
const mainApplicationPrefix = path.join(
config.modRequest.platformProjectRoot,
"app",
"src",
"main",
"java",
...(config?.android?.package?.split?.(".") || [])
);
const mainApplicationPathJava = path.join(
mainApplicationPrefix,
"MainApplication.java"
);
const mainApplicationPathKotlin = path.join(
mainApplicationPrefix,
"MainApplication.kt"
);
if (fs.existsSync(mainApplicationPathJava)) {
isJava = true;
mainApplicationPath = mainApplicationPathJava;
} else if (fs.existsSync(mainApplicationPathKotlin)) {
isJava = false;
mainApplicationPath = mainApplicationPathKotlin;
} else {
throw new Error(
"MainApplication.kt or MainApplication.java file not found."
);
}
fs.readFile(mainApplicationPath, "utf-8", (err, data) => {
if (isJava) {
data = insertLinesHelper(
"import com.viromedia.bridge.ReactViroPackage;",
`package ${config?.android?.package};`,
data
);
} else {
data = insertLinesHelper(
"import com.viromedia.bridge.ReactViroPackage",
`package ${config?.android?.package}`,
data
);
}
const viroPlugin = config?.plugins?.find(
(plugin) =>
Array.isArray(plugin) && plugin[0] === "@reactvision/react-viro"
);
if (Array.isArray(viroPlugin)) {
if (Array.isArray(viroPlugin[1].android?.xRMode)) {
viroPluginConfig = (
viroPlugin[1].android?.xRMode as XrMode[]
).filter((mode) => ["AR", "GVR", "OVR_MOBILE"].includes(mode));
} else if (
["AR", "GVR", "OVR_MOBILE"].includes(viroPlugin[1]?.android?.xRMode)
) {
viroPluginConfig = [viroPlugin[1]?.android.xRMode];
}
}
let target = "";
for (const viroConfig of viroPluginConfig) {
if (isJava) {
target =
target +
` packages.add(new ReactViroPackage(ReactViroPackage.ViroPlatform.${viroConfig}))\n`;
} else {
target =
target +
` packages.add(ReactViroPackage(ReactViroPackage.ViroPlatform.${viroConfig}))\n`;
}
}
if (isJava) {
data = insertLinesHelper(
target,
"List<ReactPackage> packages = new PackageList(this).getPackages();",
data
);
} else {
if (data.includes("// packages.add(new MyReactNativePackage());")) {
// console.log("[VIRO]: \n" + target);
/**
* ================================================================
* HACK/EXPO PREBIULD BUG
* ================================================================
*
* ```
* // DOESN'T WORK - EXPO SDK 50 `npx expo prebuild` RESULT
* override fun getPackages(): List<ReactPackage> {
* // Packages that cannot be autolinked yet can be added manually here, for example:
* // packages.add(new MyReactNativePackage());
* PackageList(this).packages.apply {
* add(ReactViroPackage(ReactViroPackage.ViroPlatform.GVR))
* }
* return PackageList(this).packages
* }
*
* // DOES WORK - REACT NATIVE STARTER KIT 0.73.4
* override fun getPackages(): List<ReactPackage> =
* PackageList(this).packages.apply {
* // Packages that cannot be autolinked yet can be added manually here, for example:
* // add(MyReactNativePackage())
* // https://viro-community.readme.io/docs/installation-instructions#5-now-add-the-viro-package-to-your-mainapplication
* // add(ReactViroPackage(ReactViroPackage.ViroPlatform.OVR_MOBILE))
* add(ReactViroPackage(ReactViroPackage.ViroPlatform.GVR))
* // add(ReactViroPackage(ReactViroPackage.ViroPlatform.AR))
* }
* ```
*/
/*
data = data.replace(
`override fun getPackages(): List<ReactPackage> {
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return PackageList(this).packages
}`,
`override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
}`
);*/
data = insertLinesHelper(
target,
"// packages.add(new MyReactNativePackage());",
data
);
} else if (data.includes("// add(MyReactNativePackage())")) {
// console.log("[VIRO]: \n" + target);
data = insertLinesHelper(
target,
"// add(MyReactNativePackage())",
data
);
} else {
throw new Error(
"Unable to insert Android packages into package list. Please create a new issue on GitHub and reference this message!"
);
}
// data = insertLinesHelper(
// target,
// "// packages.add(new MyReactNativePackage());",
// data
// );
}
fs.writeFile(mainApplicationPath, data, "utf-8", function (err) {
if (err) console.log("Error writing MainApplication.java");
});
});
return config;
},
]);
};
const withViroProjectBuildGradle = (config: ExpoConfig) =>
withProjectBuildGradle(config, async (newConfig) => {
newConfig.modResults.contents = newConfig.modResults.contents.replace(
/minSdkVersion.*/,
`minSdkVersion = 24`
);
newConfig.modResults.contents = newConfig.modResults.contents.replace(
/classpath\("com.android.tools.build:gradle.*/,
`classpath('com.android.tools.build:gradle:4.1.1')`
);
return newConfig;
});
const withViroAppBuildGradle = (config: ExpoConfig) =>
withAppBuildGradle(config, async (config) => {
config.modResults.contents = config.modResults.contents.replace(
/implementation "com.facebook.react:react-native:\+" \/\/ From node_modules/,
`implementation "com.facebook.react:react-native:+" // From node_modules
// ========================================================================
// https://viro-community.readme.io/docs/installation-instructions#2-in-your-androidappbuildgradle-add-the-following-lines-to-the-dependencies-section
implementation project(':gvr_common')
implementation project(':arcore_client')
implementation project(path: ':react_viro')
implementation project(path: ':viro_renderer')
implementation 'androidx.media3:media3-exoplayer:1.1.1'
implementation 'androidx.media3:media3-exoplayer-dash:1.1.1'
implementation 'androidx.media3:media3-exoplayer-hls:1.1.1'
implementation 'androidx.media3:media3-exoplayer-smoothstreaming:1.1.1'
implementation 'com.google.protobuf.nano:protobuf-javanano:3.1.0'
// ========================================================================`
);
config.modResults.contents = config.modResults.contents.replace(
/implementation\("com.facebook.react:react-android"\)/,
`implementation("com.facebook.react:react-android")
// =====================================r===================================
// https://viro-community.readme.io/docs/installation-instructions#2-in-your-androidappbuildgradle-add-the-following-lines-to-the-dependencies-section
implementation project(':gvr_common')
implementation project(':arcore_client')
implementation project(path: ':react_viro')
implementation project(path: ':viro_renderer')
implementation 'androidx.media3:media3-exoplayer:1.1.1'
implementation 'androidx.media3:media3-exoplayer-dash:1.1.1'
implementation 'androidx.media3:media3-exoplayer-hls:1.1.1'
implementation 'androidx.media3:media3-exoplayer-smoothstreaming:1.1.1'
implementation 'com.google.protobuf.nano:protobuf-javanano:3.1.0'
// ========================================================================
`
);
return config;
});
const withViroSettingsGradle = (config: ExpoConfig) =>
withSettingsGradle(config, async (config) => {
config.modResults.contents += `
include ':react_viro', ':arcore_client', ':gvr_common', ':viro_renderer'
project(':arcore_client').projectDir = new File('../node_modules/@reactvision/react-viro/android/arcore_client')
project(':gvr_common').projectDir = new File('../node_modules/@reactvision/react-viro/android/gvr_common')
project(':viro_renderer').projectDir = new File('../node_modules/@reactvision/react-viro/android/viro_renderer')
project(':react_viro').projectDir = new File('../node_modules/@reactvision/react-viro/android/react_viro')
`;
return config;
});
const withViroManifest = (config: ExpoConfig) =>
withAndroidManifest(
config,
async (newConfig: ExportedConfigWithProps<any>) => {
const contents = newConfig.modResults;
contents.manifest.$["xmlns:tools"] = "http://schemas.android.com/tools";
contents?.manifest?.application?.[0]["meta-data"]?.push({
$: {
"android:name": "com.google.ar.core",
"android:value": "optional",
},
});
if (
viroPluginConfig.includes("GVR") ||
viroPluginConfig.includes("OVR_MOBILE")
) {
// <!-- Add the following line for cardboard -->
// <category android:name="com.google.intent.category.CARDBOARD" />
contents?.manifest?.application?.[0]?.activity[0][
"intent-filter"
][0].category.push({
$: {
"android:name": "com.google.intent.category.CARDBOARD",
},
});
// <!-- Add the following line for daydream -->
// <category android:name="com.google.intent.category.DAYDREAM" />
contents?.manifest?.application?.[0]?.activity[0][
"intent-filter"
][0].category.push({
$: {
"android:name": "com.google.intent.category.DAYDREAM",
},
});
}
contents.manifest.queries = [
{
package: [
{
$: {
"android:name": "com.google.ar.core",
},
},
],
},
];
contents.manifest["uses-feature"] = [];
contents.manifest["uses-permission"].push({
$: {
"android:name": "android.permission.CAMERA",
},
});
contents.manifest["uses-feature"].push({
$: {
"android:name": "android.hardware.camera",
},
});
contents.manifest["uses-feature"].push({
$: {
"android:name": "android.hardware.camera.autofocus",
"android:required": "false",
"tools:replace": "required",
},
});
contents.manifest["uses-feature"].push({
$: {
"android:glEsVersion": "0x00030000",
"android:required": "false",
"tools:node": "remove",
"tools:replace": "required",
},
});
contents.manifest["uses-feature"].push({
$: {
"android:name": "android.hardware.sensor.accelerometer",
"android:required": "false",
"tools:replace": "required",
},
});
contents.manifest["uses-feature"].push({
$: {
"android:name": "android.hardware.sensor.gyroscope",
"android:required": "false",
"tools:replace": "required",
},
});
return newConfig;
}
);
export const withViroAndroid: ConfigPlugin<ViroConfigurationOptions> = (
config,
props
) => {
withPlugins(config, [[withBranchAndroid, props]]);
withViroProjectBuildGradle(config);
withViroManifest(config);
withViroSettingsGradle(config);
withViroAppBuildGradle(config);
return config;
};