|
| 1 | +import { |
| 2 | + AndroidConfig, |
| 3 | + ConfigPlugin, |
| 4 | + withGradleProperties, |
| 5 | + withProjectBuildGradle, |
| 6 | + withSettingsGradle, |
| 7 | + withStringsXml, |
| 8 | +} from 'expo/config-plugins'; |
| 9 | + |
| 10 | +const withUnity: ConfigPlugin<{ name?: string }> = ( |
| 11 | + config, |
| 12 | + { name = 'react-native-unity' } = {} |
| 13 | +) => { |
| 14 | + config.name = name; |
| 15 | + config = withProjectBuildGradleMod(config); |
| 16 | + config = withSettingsGradleMod(config); |
| 17 | + config = withGradlePropertiesMod(config); |
| 18 | + config = withStringsXMLMod(config); |
| 19 | + return config; |
| 20 | +}; |
| 21 | + |
| 22 | +const REPOSITORIES_END_LINE = `maven { url 'https://www.jitpack.io' }`; |
| 23 | + |
| 24 | +const withProjectBuildGradleMod: ConfigPlugin = (config) => |
| 25 | + withProjectBuildGradle(config, (modConfig) => { |
| 26 | + if (modConfig.modResults.contents.includes(REPOSITORIES_END_LINE)) { |
| 27 | + // use the last known line in expo's build.gradle file to append the newline after |
| 28 | + modConfig.modResults.contents = modConfig.modResults.contents.replace( |
| 29 | + REPOSITORIES_END_LINE, |
| 30 | + REPOSITORIES_END_LINE + |
| 31 | + '\nflatDir { dirs "${project(\':unityLibrary\').projectDir}/libs" }\n' |
| 32 | + ); |
| 33 | + } else { |
| 34 | + throw new Error( |
| 35 | + 'Failed to find the end of repositories in the android/build.gradle file`' |
| 36 | + ); |
| 37 | + } |
| 38 | + return modConfig; |
| 39 | + }); |
| 40 | + |
| 41 | +const withSettingsGradleMod: ConfigPlugin = (config) => |
| 42 | + withSettingsGradle(config, (modConfig) => { |
| 43 | + modConfig.modResults.contents += ` |
| 44 | +include ':unityLibrary' |
| 45 | +project(':unityLibrary').projectDir=new File('../unity/builds/android/unityLibrary') |
| 46 | + `; |
| 47 | + return modConfig; |
| 48 | + }); |
| 49 | + |
| 50 | +const withGradlePropertiesMod: ConfigPlugin = (config) => |
| 51 | + withGradleProperties(config, (modConfig) => { |
| 52 | + modConfig.modResults.push({ |
| 53 | + type: 'property', |
| 54 | + key: 'unityStreamingAssets', |
| 55 | + value: '.unity3d', |
| 56 | + }); |
| 57 | + return modConfig; |
| 58 | + }); |
| 59 | + |
| 60 | +// add string |
| 61 | +const withStringsXMLMod: ConfigPlugin = (config) => |
| 62 | + withStringsXml(config, (config) => { |
| 63 | + config.modResults = AndroidConfig.Strings.setStringItem( |
| 64 | + [ |
| 65 | + { |
| 66 | + _: 'Game View', |
| 67 | + $: { |
| 68 | + name: 'game_view_content_description', |
| 69 | + }, |
| 70 | + }, |
| 71 | + ], |
| 72 | + config.modResults |
| 73 | + ); |
| 74 | + return config; |
| 75 | + }); |
| 76 | + |
| 77 | +export default withUnity; |
0 commit comments