gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:32 26 4
gpt4 key购买 nike

我在写一个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安装的react-native@0.20.1,这个版本的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 install react-native@0.46.1

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

这样您就不需要将您的库保留在您的主项目中以构建和检查您的语法。

关于android - 如何配置 react native android 库以使用 npm 安装的 react-native?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37361004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com