From 17a3430c98559c3188da8667642fa4985b215fb8 Mon Sep 17 00:00:00 2001 From: Hirbod Date: Tue, 11 Jan 2022 12:31:24 +0100 Subject: [PATCH] feat: Add `disableFrameProcessors` flag to Expo Config Plugin (#708) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: disableFrameProcessors for android via expo-config-plugin prop * chore: naming * feat: fix shared library issues with expo config plug prop flag * fix: use a glob pattern instead of listing every single shared lib * fix: use wildcard since libc++ is not enough (libhermes, libjni, libjsi etc) * fix: use wildcard since libc++ is not enough (libhermes, libjni, libjsi etc) * feat: 🎉 disable frame processors for iOS as well * chore: comments * chore: make eslint/ts happy * chore: cleanup * refactor: no need to pass a param here. We just want to disbale it * chore: remove withDangerouslyHandleAndroidSharedLibrary * chore: remove danger plugin --- .../withDisableFrameProcessorsAndroid.ts | 23 +++++++++++++ .../withDisableFrameProcessorsIOS.ts | 33 +++++++++++++++++++ src/expo-plugin/withVisionCamera.ts | 8 +++++ 3 files changed, 64 insertions(+) create mode 100644 src/expo-plugin/withDisableFrameProcessorsAndroid.ts create mode 100644 src/expo-plugin/withDisableFrameProcessorsIOS.ts 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]]); };