Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure react native android library to use npm installed react-native?

I'm writing an android library, need to call ReactInstanceManager.onHostResume, my build.gradle file content is:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.facebook.react:react-native:+"
}

I installed it into an example project with npm install --save react-native-mylib, and then compile the project with react-native run-android, some errors caused:

MyModuleActivity.java:107: error: cannot find symbol
      mReactInstanceManager.onHostPause();
                           ^
  symbol:   method onHostPause()
  location: variable mReactInstanceManager of type ReactInstanceManager

I try to find installed react-native libraries:

find . -iname "*.jar"
...
./node_modules/react-native-mylib/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/classes.jar
./node_modules/react-native-mylib/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/libs/infer-annotations-1.5.jar
...

Seems it installed [email protected] from maven, this version of react-native hasn't onHostPause method.

EDIT:

Find libraries in another project that linked some libraries, they are all linked old version of react-native:

$ find . -iname "*.jar" | grep com.facebook.react
./android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.25.1/jars/classes.jar
./android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.25.1/jars/libs/infer-annotations-1.5.jar
./android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.26.0/jars/classes.jar
./android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.26.0/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native/android/com/facebook/react/react-native/0.26.0/react-native-0.26.0-javadoc.jar
./node_modules/react-native/android/com/facebook/react/react-native/0.26.0/react-native-0.26.0-sources.jar
./node_modules/react-native-android-blurryoverlay/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.16.1/jars/classes.jar
./node_modules/react-native-android-blurryoverlay/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.16.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-applean/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/classes.jar
./node_modules/react-native-applean/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-applean-appview/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/classes.jar
./node_modules/react-native-applean-appview/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-barcodescanner/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.19.1/jars/classes.jar
./node_modules/react-native-barcodescanner/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.19.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-camera/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.19.1/jars/classes.jar
./node_modules/react-native-camera/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.19.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-code-push/android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.19.1/jars/classes.jar
./node_modules/react-native-code-push/android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.19.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-device-info/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.11.0/jars/classes.jar
./node_modules/react-native-device-info/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.11.0/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-fs/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.12.0/jars/classes.jar
./node_modules/react-native-fs/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.12.0/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-mipush/android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/classes.jar
./node_modules/react-native-mipush/android/app/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-orientation/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.11.0/jars/classes.jar
./node_modules/react-native-orientation/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.11.0/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-sound/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.11.0/jars/classes.jar
./node_modules/react-native-sound/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.11.0/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-sound/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.12.0/jars/classes.jar
./node_modules/react-native-sound/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.12.0/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-vector-icons/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/classes.jar
./node_modules/react-native-vector-icons/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars/libs/infer-annotations-1.5.jar
./node_modules/react-native-vibration/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.12.0/jars/classes.jar
./node_modules/react-native-vibration/android/build/intermediates/exploded-aar/com.facebook.react/react-native/0.12.0/jars/libs/infer-annotations-1.5.jar

Question: How to configure it to use node_modules/react-native/android correctly?

EDIT: Resolved, just set correct repositories to:

repositories {
    mavenLocal()
    jcenter()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$projectDir/../../../node_modules/react-native/android"
    }
}
like image 530
Jacky Lee Avatar asked May 21 '16 09:05

Jacky Lee


People also ask

Can I use Android library in react native?

The JavaScript spec for your native module or component will be used to generate native interfacenative interfaceIn software design, the Java Native Interface (JNI) is a foreign function interface programming framework that enables Java code running in a Java virtual machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages ...https://en.wikipedia.org › wiki › Java_Native_InterfaceJava Native Interface - Wikipedia code for each supported platform (i.e. Android and iOS). These native interface files will be generated when a React Native application that depends on your library is built.

Can I use npm packages in react native?

React Native libraries are typically installed from the npm registry using a Node. js package manager such as npm CLI or Yarn Classic. If you have Node. js installed on your computer then you already have the npm CLI installed.

How do I connect react native app to Android?

Running your React Native applicationInstall the Expo Go app on your iOS or Android phone and connect to the same wireless network as your computer. On Android, use the Expo Go app to scan the QR code from your terminal to open your project. On iOS, use the built-in QR code scanner of the default iOS Camera app.


1 Answers

Resolved a while ago as well... I created a library using react-native-create-library. The latest version maven provides is 0.20.1, which is a little old for today's react-native versions. So I installed the RN version I needed into the node_modules folder of my library

npm install [email protected]

And in build.gradle

repositories {
    maven {
        // All of React Native (JS, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    jcenter()
}
//
dependencies {
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile "com.facebook.react:react-native:+"
}

This way you don't need to keep your library inside of your master project in order to build and check your syntax.

like image 196
DerpyNerd Avatar answered Oct 24 '22 00:10

DerpyNerd