如何配置React Native Android库以使用npm安装的react-native? [英] How to configure react native android library to use npm installed react-native?

查看:70
本文介绍了如何配置React Native Android库以使用npm安装的react-native?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个android库,需要调用ReactInstanceManager.onHostResume,我的build.gradle文件内容是:

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:+"
}

我使用 npm install --save react-native-mylib 将其安装到示例项目中,然后使用 react-native run-android 编译该项目导致的错误:

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
...

似乎它从maven安装了react-native@0.20.1,此版本的react-native尚未启用HostPause方法.

Seems it installed react-native@0.20.1 from maven, this version of react-native hasn't onHostPause method.

在另一个链接了某些库的项目中找到库,它们都链接了react-native的旧版本:

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

问题:如何配置它以正确使用node_modules/react-native/android?

已解决,只需将正确的存储库设置为:

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"
    }
}

推荐答案

不久前也已解决...我使用 react-native-create-library 创建了一个库.maven提供的最新版本是0.20.1,对于今天的react-native版本而言,它有些旧.所以我将所需的RN版本安装到了库的node_modules文件夹中

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 react-native@0.46.1

npm install react-native@0.46.1

在build.gradle中

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.

这篇关于如何配置React Native Android库以使用npm安装的react-native?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆