diff --git a/docs/docs/guides/CODE_SCANNING.mdx b/docs/docs/guides/CODE_SCANNING.mdx index fad97a9..01f8c96 100644 --- a/docs/docs/guides/CODE_SCANNING.mdx +++ b/docs/docs/guides/CODE_SCANNING.mdx @@ -30,7 +30,7 @@ A Code Scanner is a separate Camera output (just like photo or video) that can d On iOS, the Code Scanner uses the platform-native APIs and can be used out of the box. -On Android, the [MLKit Vision Barcode Scanning](https://developers.google.com/ml-kit/vision/barcode-scanning) API will be used, and the model (2.2MB) needs to be downloaded first. +On Android, the [MLKit Vision Barcode Scanning](https://developers.google.com/ml-kit/vision/barcode-scanning) API will be used, and the model (2.2MB) needs to be included into your app first. -To download the model when the user installs your app, add this to your `AndroidManifest.xml` file: +Inside your `gradle.properties` file, add the `enableCodeScanner` flag: -```xml - - ... - - +```groovy +VisionCamera_enableCodeScanner=true ``` -To download the model when the user installs your app, add the `enableCodeScanner` flag to your Expo config (`app.json`, `app.config.json` or `app.config.js`): +Add the `enableCodeScanner` flag to your Expo config (`app.json`, `app.config.json` or `app.config.js`): ```json { @@ -77,7 +72,7 @@ To download the model when the user installs your app, add the `enableCodeScanne ## Usage -To use a codescanner, simply create a [`CodeScanner`](/docs/api/interfaces/CodeScanner) and pass it to the ``: +To scan codes, simply create a [`CodeScanner`](/docs/api/interfaces/CodeScanner) instance and pass it to the ``: = (config, props) => { - if (props.enableCodeScanner === 'gradle-implementation') { - return withAppBuildGradle(config, (conf) => { - const buildGradle = conf.modResults - const implementation = "implementation 'com.google.mlkit:barcode-scanning:17.2.0'" - - if (buildGradle.contents.includes(implementation) === false) { - // Inspired by https://github.com/invertase/react-native-firebase/blob/main/packages/app/plugin/src/android/buildscriptDependency.ts - // TODO: Find a better way to do this - buildGradle.contents = buildGradle.contents.replace( - /dependencies\s?{/, - `dependencies { - ${implementation}`, - ) - } - - return conf +/** + * Set the `VisionCamera_enableCodeScanner` value in the static `gradle.properties` file. + * This is used to add the full MLKit dependency to the project. + */ +export const withAndroidMLKitVisionModel: ConfigPlugin = (c) => { + const key = 'VisionCamera_enableCodeScanner' + return withGradleProperties(c, (config) => { + config.modResults = config.modResults.filter((item) => { + if (item.type === 'property' && item.key === key) return false + return true }) - } - return withAndroidManifest(config, (conf) => { - const androidManifest = conf.modResults - const mainApplication = getMainApplicationOrThrow(androidManifest) + config.modResults.push({ + type: 'property', + key: key, + value: 'true', + }) - addMetaDataItemToMainApplication(mainApplication, 'com.google.mlkit.vision.DEPENDENCIES', 'barcode') - - conf.modResults = androidManifest - - return conf + return config }) } diff --git a/package/src/expo-plugin/withVisionCamera.ts b/package/src/expo-plugin/withVisionCamera.ts index 015b053..3f2a9d2 100644 --- a/package/src/expo-plugin/withVisionCamera.ts +++ b/package/src/expo-plugin/withVisionCamera.ts @@ -26,7 +26,7 @@ const withCamera: ConfigPlugin = (config, props = {}) => { config = withDisableFrameProcessorsIOS(config) } - if (props.enableCodeScanner !== false) config = withAndroidMLKitVisionModel(config, props) + if (props.enableCodeScanner) config = withAndroidMLKitVisionModel(config, props) return withPlugins(config, [[AndroidConfig.Permissions.withPermissions, androidPermissions]]) }