diff --git a/src/expo-plugin/withDisableFrameProcessorsAndroid.ts b/src/expo-plugin/withDisableFrameProcessorsAndroid.ts new file mode 100644 index 0000000..caed43d --- /dev/null +++ b/src/expo-plugin/withDisableFrameProcessorsAndroid.ts @@ -0,0 +1,23 @@ +import { ConfigPlugin, withGradleProperties } from '@expo/config-plugins'; + +/** + * Set the `disableFrameProcessors` value in the static `gradle.properties` file. + * This is used to disable frame processors if you don't need it for android. + */ +export const withDisableFrameProcessorsAndroid: ConfigPlugin = (c) => { + const disableFrameProcessorsKey = 'disableFrameProcessors'; + return withGradleProperties(c, (config) => { + config.modResults = config.modResults.filter((item) => { + if (item.type === 'property' && item.key === disableFrameProcessorsKey) return false; + return true; + }); + + config.modResults.push({ + type: 'property', + key: disableFrameProcessorsKey, + value: 'true', + }); + + return config; + }); +}; diff --git a/src/expo-plugin/withDisableFrameProcessorsIOS.ts b/src/expo-plugin/withDisableFrameProcessorsIOS.ts new file mode 100644 index 0000000..ec9eefd --- /dev/null +++ b/src/expo-plugin/withDisableFrameProcessorsIOS.ts @@ -0,0 +1,33 @@ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ +import { ConfigPlugin, withXcodeProject, XcodeProject } from '@expo/config-plugins'; + +/** + * Set the `disableFrameProcessors` inside of the XcodeProject. + * This is used to disable frame processors if you don't need it on iOS. (will save CPU and Memory) + */ +export const withDisableFrameProcessorsIOS: ConfigPlugin = (c) => { + return withXcodeProject(c, (config) => { + const xcodeProject: XcodeProject = config.modResults; + + const configurations = xcodeProject.pbxXCBuildConfigurationSection(); + let key; + let buildSettings; + + const inheritKey = '"$(inherited)"'; + const valueKey = '"VISION_CAMERA_DISABLE_FRAME_PROCESSORS=1"'; + + for (key in configurations) { + buildSettings = configurations[key].buildSettings; + if (typeof buildSettings?.GCC_PREPROCESSOR_DEFINITIONS !== 'undefined') { + // alright, this is the DEBUG config, push our setting to it + if (buildSettings.GCC_PREPROCESSOR_DEFINITIONS.includes(valueKey) === false) + buildSettings.GCC_PREPROCESSOR_DEFINITIONS.push(valueKey); + } else if (typeof buildSettings !== 'undefined') { + // new projects do not have GCC_PREPROCESSOR_DEFINITIONS for "Release", lets create it + // we need to add the inheritKey or it will break the release build. + buildSettings.GCC_PREPROCESSOR_DEFINITIONS = [inheritKey, valueKey]; + } + } + return config; + }); +}; diff --git a/src/expo-plugin/withVisionCamera.ts b/src/expo-plugin/withVisionCamera.ts index 093b555..f3813a2 100644 --- a/src/expo-plugin/withVisionCamera.ts +++ b/src/expo-plugin/withVisionCamera.ts @@ -1,4 +1,6 @@ import { withPlugins, AndroidConfig, ConfigPlugin, createRunOncePlugin } from '@expo/config-plugins'; +import { withDisableFrameProcessorsAndroid } from './withDisableFrameProcessorsAndroid'; +import { withDisableFrameProcessorsIOS } from './withDisableFrameProcessorsIOS'; // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment const pkg = require('../../../package.json'); @@ -9,6 +11,7 @@ type Props = { cameraPermissionText?: string; enableMicrophonePermission?: boolean; microphonePermissionText?: string; + disableFrameProcessors?: boolean; }; const withCamera: ConfigPlugin = (config, props = {}) => { @@ -23,6 +26,11 @@ const withCamera: ConfigPlugin = (config, props = {}) => { const androidPermissions = ['android.permission.CAMERA']; if (props.enableMicrophonePermission) androidPermissions.push('android.permission.RECORD_AUDIO'); + if (props.disableFrameProcessors) { + config = withDisableFrameProcessorsAndroid(config); + config = withDisableFrameProcessorsIOS(config); + } + return withPlugins(config, [[AndroidConfig.Permissions.withPermissions, androidPermissions]]); };