微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

react-native-camera:Android 应用程序崩溃没有错误

如何解决react-native-camera:Android 应用程序崩溃没有错误

我已将 react-native-camera 添加到我的应用程序中。创建使用 RNCamera 的组件后,每次我尝试运行我的应用程序(使用 react-native run-android)时,它都会请求必要的权限(相机和音频),然后立即崩溃而不输出任何错误消息。

这是我的 package.json:

{
  "main": "index.js","scripts": {
    "android": "react-native run-android","ios": "react-native run-ios","web": "expo start --web","start": "react-native start","test": "jest"
  },"dependencies": {
    "@react-native-community/async-storage": "^1.12.1","@tensorflow-models/mobilenet": "^2.0.4","@tensorflow/tfjs": "^3.3.0","@tensorflow/tfjs-react-native": "^0.5.0","expo": "~40.0.0","expo-camera": "~9.1.0","expo-constants": "^10.0.1","expo-gl": "~9.2.0","expo-gl-cpp": "~9.2.0","expo-splash-screen": "~0.8.0","expo-status-bar": "~1.0.3","expo-updates": "~0.4.0","mobx": "^6.1.8","react": "16.13.1","react-dom": "16.13.1","react-native": "~0.63.4","react-native-camera": "^3.43.0","react-native-fs": "^2.16.6","react-native-gesture-handler": "~1.8.0","react-native-reanimated": "~1.13.0","react-native-screens": "~2.15.0","react-native-unimodules": "~0.12.0","react-native-web": "~0.13.12"
  },"devDependencies": {
    "@babel/core": "~7.9.0","babel-jest": "~25.2.6","jest": "~25.2.6","react-test-renderer": "~16.13.1"
  },"jest": {
    "preset": "react-native"
  },"private": true
}

这是我的 build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        buildToolsversion = "29.0.2"
        minSdkVersion = 21
        compileSdkVersion = 29
        targetSdkVersion = 29
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.5.3")
        classpath("com.google.gms:google-services:4.3.5")

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS,Obj-C sources,Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        maven {
            url("$rootDir/../node_modules/expo-camera/android/maven")
        }
        maven {
            url 'https://www.jitpack.io'
        }



        google()
        jcenter()
    }
}

这是我的/app/build.gradle:

apply plugin: "com.android.application"
apply plugin: "com.google.gms.google-services"

import com.android.build.OutputFile

project.ext.react = [
    enableHermes: false
]

apply from: '../../node_modules/react-native-unimodules/gradle.groovy'
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/expo-constants/scripts/get-app-config-android.gradle"
apply from: "../../node_modules/expo-updates/scripts/create-manifest-android.gradle"


def enableSeparateBuildPercpuArchitecture = false


def enableProguardInReleaseBuilds = false

def jscFlavor = 'org.webkit:android-jsc:+'

def enableHermes = project.ext.react.get("enableHermes",false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    ndkVersion "22.0.7026061"
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.chowtime"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        missingDimensionStrategy 'react-native-camera','mlkit'
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPercpuArchitecture
            universalApk false  // If true,also generate a universal APK
            include "armeabi-v7a","x86","arm64-v8a","x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production,you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"),"proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug,release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture,set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            def versionCodes = ["armeabi-v7a": 1,"x86": 2,"arm64-v8a": 3,"x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug,universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }

        }
    }
}

dependencies {
    implementation platform('com.google.firebase:firebase-bom:26.7.0')
    implementation 'com.google.firebase:firebase-analytics'
    
    implementation (project(':react-native-camera')) {
        exclude group: "com.google.android.gms"
        exclude group: "com.google.android.gms",module: "play-services-vision"
        exclude group: "com.google.firebase",module: "firebase-ml-vision-face-model"
    }

    implementation ("com.google.firebase:firebase-ml-vision:19.0.3") {
        force = true;
    }
    implementation ("com.google.firebase:firebase-ml-vision-face-model:17.0.2") {
        force = true;
    }

    implementation filetree(dir: "libs",include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }
    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3',module:'okhttp'
    }
    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }
    addUnimodulesDependencies()

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

task copyDownloadableDepsToLibs(type: copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

编辑: 这是我的 AndroidManifest.xml 的样子:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chowtime">
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.SYstem_ALERT_WINDOW"/>
  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
  <uses-permission android:name="android.permission.USE_BIOMETRIC"/>
  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="android.permission.MODIFY_AUdio_SETTINGS"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.RECORD_AUdio"/>
  <uses-permission android:name="android.permission.READ_CONTACTS"/>
  <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
  <uses-permission android:name="android.permission.READ_CALENDAR"/>
  <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme">
    <Meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="https://exp.host/@jjwilkin/test_bare"/>
    <Meta-data android:name="expo.modules.updates.EXPO_SDK_VERSION" android:value="40.0.0"/>
    <Meta-data android:name="expo.modules.updates.ENABLED" android:value="true"/>
    <Meta-data android:name="expo.modules.updates.EXPO_UPDATES_CHECK_ON_LAUNCH" android:value="ALWAYS"/>
    <Meta-data android:name="expo.modules.updates.EXPO_UPDATES_LAUNCH_WAIT_MS" android:value="0"/>
    <Meta-data android:name="com.google.firebase.ml.vision.DEPENDENCIES" android:value="ocr,text" />
    <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
  </application>
</manifest>

这是我的 android 项目目录的样子:

enter image description here

有关为什么会发生这种情况或如何解决此问题的任何信息将不胜感激!谢谢:)

更新:我试过在同一个 repo 中使用 expo-camera(我读到 react-native-camera 是建立在 expo-camera 上的),而且效果很好。

编辑:这是运行 adb logcat输出:pastebin.com/yWM5Mat8。

解决方法

<uses-permission android:name="android.permission.CAMERA" />

在 AndroidManifest.xml 文件中的相机权限上方添加。检查一次后。

,

仔细阅读粘贴图,这似乎是罪魁祸首:

03-31 21:51:16.152 13039 13039 E unknown:ViewManager: Error while updating prop playSoundOnCapture
03-31 21:51:16.152 13039 13039 E unknown:ViewManager: java.lang.reflect.InvocationTargetException
... a bunch of traceback lines
03-31 21:51:16.152 13039 13039 E unknown:ViewManager: Caused by: java.lang.NoSuchMethodError: No virtual method setPlaySoundOnCapture(Z)V in class Lorg/reactnative/camera/RNCameraView; or its super classes (declaration of 'org.reactnative.camera.RNCameraView' appears in /data/app/~~2TIdBhLTuyzCkV7CYFT2Mg==/com.chowtime-uS9KR34-elnZ0EmL_ovT3w==/base.apk!classes4.dex)

这个 github 问题可能是相关的:https://github.com/react-native-camera/react-native-camera/issues/2938#issuecomment-671436341 它似乎没有很好的解决方案,但表明如果 react-native-camera 和 expo-camera 都安装,就像它们看起来一样,就会发生冲突你的 package.json。如果它们依赖于不同版本的 com.google.android.cameraview.CameraView,您可能需要选择其中一种。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。