如何配置React Native Android库以使用npm安装的react-native?

杰基·李

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

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,导致了一些错误:

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

我尝试找到已安装的react-native库:

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安装了[email protected],此版本的react-native没有onHostPause方法。

编辑:

在链接了某些库的另一个项目中找到库,它们都是链接的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文件夹中

npm安装[email protected]

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

这样,您无需将库保留在主项目中即可构建和检查语法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何配置React Native Android库以使用npm安装的react-native?

来自分类Dev

React Native 的 Webpack 配置

来自分类Dev

如何为 React Native 配置 Linter?

来自分类Dev

React Native 0.60.5 react-native-splash-screen配置Android

来自分类Dev

如何配置 react-native-router-flux android back history?

来自分类Dev

如何安装 react-native?

来自分类Dev

如何在React Native中安装React Native Maps?

来自分类Dev

使用 npm 安装 react-native-gesture-handler 时出错

来自分类Dev

React Native,Android日志。

来自分类Dev

React Native Android Navigator

来自分类Dev

React Native Android模块

来自分类Dev

android native和react native之间的通信

来自分类Dev

AVA + React-Native的测试配置

来自分类Dev

如何加载场景React Native

来自分类Dev

如何隐藏React Native NavigationBar

来自分类Dev

React Native:如何实现DatePickerAndroid?

来自分类Dev

如何覆盖React Native组件?

来自分类Dev

React Native:如何实现DatePickerAndroid?

来自分类Dev

React Native:如何专注于ListView?

来自分类Dev

React Native:如何重定向

来自分类Dev

React Native 如何绘制曲线

来自分类Dev

React Native:如何在React Native中发送TextInput的值?

来自分类Dev

在 Android 上安装 React Native Fabric Digits

来自分类Dev

React-native安装问题

来自分类Dev

react-native android发布密钥库

来自分类Dev

react-native android发布密钥库

来自分类Dev

react-native:如何结合 react-native-router-flux 和 react-native-drawer?

来自分类Dev

react-native-elements安装问题与react-native

来自分类Dev

在React Native中使用ObjectiveC库