feat: Add new enableCodeScanner
prop to build.gradle
to make sure CodeScanner
always works (#2355)
* feat: Always download model instead of relying on Google Play Services * feat: Use `VisionCamera_enableCodeScanner` flag instead of unsafely replacing * Update CODE_SCANNING.mdx
This commit is contained in:
@@ -22,13 +22,15 @@ export type ConfigProps = {
|
||||
*/
|
||||
disableFrameProcessors?: boolean
|
||||
/**
|
||||
* Whether to enable the QR/Barcode Scanner Model. If true, the MLKit Model will
|
||||
* automatically be downloaded on app startup. If false, it will be downloaded
|
||||
* once the Camera is created with a `CodeScanner`.
|
||||
* Whether to enable the QR/Barcode Scanner Model.
|
||||
*
|
||||
* - When set to `true`, the MLKit Model will be bundled alongside
|
||||
* with your app. (~2.4 MB in size).
|
||||
* - When set to `false`, it can still be downloaded on-the-fly on devices with Google Play Services installed if you are using the `CodeScanner`.
|
||||
*
|
||||
* See [QR/Barcode Scanning](https://react-native-vision-camera.com/docs/guides/code-scanning)
|
||||
*
|
||||
* If set to true, it fallbacks to `android-manifest`.
|
||||
* @default false
|
||||
*/
|
||||
enableCodeScanner?: boolean | 'android-manifest' | 'gradle-implementation'
|
||||
enableCodeScanner?: boolean
|
||||
}
|
||||
|
@@ -1,36 +1,24 @@
|
||||
import { AndroidConfig, ConfigPlugin, withAndroidManifest, withAppBuildGradle } from '@expo/config-plugins'
|
||||
import { ConfigPlugin, withGradleProperties } from '@expo/config-plugins'
|
||||
import { ConfigProps } from './@types'
|
||||
|
||||
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = AndroidConfig.Manifest
|
||||
|
||||
export const withAndroidMLKitVisionModel: ConfigPlugin<ConfigProps> = (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<ConfigProps> = (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
|
||||
})
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ const withCamera: ConfigPlugin<ConfigProps> = (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]])
|
||||
}
|
||||
|
Reference in New Issue
Block a user