Home » Java » java – React Native: Override Error on Package without Duplicate-Exceptionshub

java – React Native: Override Error on Package without Duplicate-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to use the react-native-camera package but when I added this to my project, I wasn’t able to see the camera, instead it was loading and a warning. Then I search on the internet for this warning and saw that I had to link this library manually and so I did. But when I try to build I got this:

error

Here is my MainApplication.java

import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.reactnative.camera.RNCameraPackage;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        public boolean canOverrideExistingModule() {        
          return true;    
        }   

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          packages.add(new RNCameraPackage());
          return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }
      };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this); // Remove this line if you don't want Flipper enabled
  }

  /**
   * Loads Flipper in React Native templates.
   *
   * @param context
   */
  private static void initializeFlipper(Context context) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
        aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.app">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <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">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <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>

Getting this when trying to access the camera:

Possible Unhandled Promise Rejection (id: 0):
TypeError: CameraManager.checkVideoAuthorizationStatus is not a function
TypeError: CameraManager.checkVideoAuthorizationStatus is not a function
    at requestPermissions$ (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:129653:61)
    at tryCatch (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25682:19)
    at Generator.invoke [as _invoke] (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25857:24)
    at Generator.prototype.<computed> [as next] (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25725:23)
    at tryCatch (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25682:19)
    at invoke (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25758:22)
    at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25788:13
    at tryCallTwo (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27046:7)
    at doResolve (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27210:15)
    at new Promise (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27069:5)
How to&Answers:

Well, the message sais it pretty clear in my opinion. Rect-Native 0.60+ uses autlinking to link native packages, so you are not require to use react-native link react-native-camera anymore.

Which means you don’t need to import and have packages.add(new RNCameraPackage()); in your MainApplication.java

protected List<ReactPackage> getPackages() {
    @SuppressWarnings("UnnecessaryLocalVariable")
    List<ReactPackage> packages = new PackageList(this).getPackages();
    packages.add(new RNCameraPackage()); // Remove this line and the import statement
    return packages;
}

You also need to remove all the other things that were added when running the react-native link command. Reinstall and your app should open now.

LE:

As backup, you can turn off autolinking for specific packages like below. Edit or create react-native.config.js add have the following.

module.exports = {
    dependencies: {
        'react-native-camera': {
            platforms: {
                android: null, // disable Android platform, other platforms will still autolink if provided
            },
        },
    },
};