I need to integrate the SDK of a barcode scanner into my react-native app but I keep getting an error:
Couldn't load XXX from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.mobile-1.apk"],nativeLibraryDirectories=[/data/aoo-lib/com.mobile-1, /system/lib]]]: findLibrary returned null
What I did:
I downloaded the SDK the
.jar
file and I put inapp/libs
I added this line in
the build.gradle
to get the path of the SDK
dependencies {
...
implementation fileTree(include: ['*.jar'], dir: 'libs')
}
- I created the
native module
to be able to load the SDK
Here is my native module code in CodeBarre.java
:
package com.myApp;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import java.util.Map;
import java.util.HashMap;
import android.util.Log;
import com.zebra.adc.decoder.BarCodeReader; // that's the scanner SDK
public class CodeBarre extends ReactContextBaseJavaModule {
public CodeBarre(ReactApplicationContext reactContext) {
super(reactContext);
}
private static final String TAG = "BarcodeSdkDemo";
@Override
public String getName() {
return "CodeBarre";
}
// here's how i'm trying to load the SDK
@ReactMethod
public void initScanner(Callback cb) {
try {
System.loadLibrary("IAL");
System.loadLibrary("SDL");
System.loadLibrary("barcodereader44"); // Android 4.4
Log.i(TAG, "Barcode scanner drivers loaded");
cb.invoke("Barcode scanner drivers loaded");
} catch (Exception e) {
Log.e(TAG, "error static initialization block", e);
cb.invoke(e.toString(), null);
}
}
}
- Here's the
react-native
side:
...
componentDidMount() {
// Access the function created in the java module
NativeModules.CodeBarre.initScanner((err ,name) => {
console.log(err, name);
});
}
The
build.gradle
in the app folder:apply plugin: "com.android.application" import com.android.build.OutputFile project.ext.react = [ entryFile: "index.js" ] apply from: "../../node_modules/react-native/react.gradle" android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { applicationId "com.mobile" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 4 versionName "0.1" } signingConfigs { release { if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) { storeFile file(MYAPP_RELEASE_STORE_FILE) storePassword MYAPP_RELEASE_STORE_PASSWORD keyAlias MYAPP_RELEASE_KEY_ALIAS keyPassword MYAPP_RELEASE_KEY_PASSWORD } } } splits { abi { reset() enable enableSeparateBuildPerCPUArchitecture universalApk false // If true, also generate a universal APK include "armeabi-v7a", "x86", "arm64-v8a" } } buildTypes { release { minifyEnabled enableProguardInReleaseBuilds proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" signingConfig signingConfigs.release } } // 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: // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3] 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 project(':react-native-device-info') implementation project(':react-native-vector-icons') implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" implementation "com.facebook.react:react-native:+" // From node_modules } // Run this once to be able to run the application with BUCK // puts all compile dependencies into folder libs for BUCK to use task copyDownloadableDepsToLibs(type: Copy) { from configurations.compile into 'libs' }