【问题标题】:Android: AIRMap was not found in the UIManagerAndroid:在 UIManager 中找不到 AIRMap
【发布时间】:2019-07-05 14:52:39
【问题描述】:

所以我正在尝试使用 react-native-maps 在我的 react native 应用程序中实现地图视图。当我使用 npm 安装它时,一切都很好,并且链接它不会出错。问题是当我尝试运行它时,我得到了

不变违规:requireNativeComponent:UIManager 中没有找到“AIRMap”。

我已将其范围缩小到 Android 端,因为那是我正在运行的模拟器。出于某种原因,gradle 文件似乎已损坏,我不知道它可能是什么。我已经尝试了我发现的大多数指南,但似乎没有一个可以解决这个问题。我试图手动链接它,删除链接等等。我也尝试添加到 GitHub 的直接链接,但也没有解决。

一些构建信息:

反应版本:0.60.0

Gradle 版本:5.4.1

模拟器版本:Pie API 28

settings.gradle:

rootProject.name = 'ProjectName'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle");     
applyNativeModulesSettingsGradle(settings)
include ':app'
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/lib/android')

build.gradle:

implementation 'com.facebook.react:react-native:+
implementation project(':react-native-maps')

地图视图:

import MapView from 'react-native-maps'
 <MapView
          region={{
            latitude: 42.882004,
            longitude: 74.582748,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
          ></MapView>

最后,我还在 android 项目中创建了一个用于 Google 地图的 API。 通过启动一个新的 react-native 项目并实施 react-native-maps 来重新创建该项目是可管理的

【问题讨论】:

  • 从 React Native 0.60.0 开始,将强制使用 AndroidX。您的所有 Android 代码和库也需要更新才能与 AndroidX 一起使用。我查了一下,似乎react-native-maps 还没有准备好。请参阅this issue 并尝试建议的解决方法,也许会有所帮助。

标签: react-native react-native-android react-native-maps


【解决方案1】:

安卓版

AndroidManifest.xml


    <application
        android:usesCleartextTraffic="true"
        tools:targetApi="28"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      <!-- You will only need to add this meta-data tag, but make sure it's a child of application -->
      <meta-data
              android:name="com.google.android.geo.API_KEY"
              android:value="AIzDDDBBLn-PhCtBM1AnC_h66yH8Lw2DDD14WW0"/>
    </application>

MainApplication.java


import java.util.List;
import com.airbnb.android.react.maps.MapsPackage;  // <-- Add this line

...


        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          packages.add(new MapsPackage()); // <-  ADD THIS LINE
          return packages;
        }

android/settings.gradle

如果你没有使用monorepo 技术

include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../../../node_modules/react-native-maps/lib/android')

android/build.gradle


buildscript {
    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 21
        compileSdkVersion = 29
        targetSdkVersion = 29
        ndkVersion = "20.1.5948944"
        supportLibVersion = '28.0.0'
        playServicesVersion = "17.0.0" // or find latest version
        googlePlayServicesVersion = "17.0.0" // or find latest version
        androidMapsUtilsVersion = "2.2.3"
    }
    repositories {
        jcenter()
        google() // keep google() at last
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.1.0")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../../../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../../../node_modules/jsc-android/dist")
        }

        jcenter()
        maven { url 'https://www.jitpack.io' }
        google() // keep google() at last
    }
}

android/app/build.gradle


dependencies {

    ...


    implementation project(':react-native-maps') // at last
}

注意事项

  1. 我使用 ../../../node_modules 是因为我使用 Monorepo 技术在一个 git repo 中维护 2 个应用程序
  2. ../node_modules 可能对你有用
  3. 在代码块的最后保留google()

IOS

在 IOS 中查看此解决方案https://stackoverflow.com/a/67945288/3437900

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2018-12-21
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多