react-native-map 使应用程序崩溃

react-native-map crashing the application

我尝试使用 React Native 应用程序并在 gradle 文件中包含以下代码段

dependencies {
    implementation project(':react-native-maps')
    implementation project(':react-native-geolocation-service')
    implementation project(':react-native-background-timer')
    implementation project(':react-native-mauron85-background-geolocation')
    implementation project(':react-native-contacts')
    implementation project(':react-native-gesture-handler')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
}

我用谷歌搜索并找到以下解决问题的行

compile(project(':react-native-maps')) {
        exclude group: 'com.google.android.gms', module: 'play-services-base'
        exclude group: 'com.google.android.gms', module: 'play-services-maps'
    }
    compile 'com.google.android.gms:play-services-base:11.+'
    compile 'com.google.android.gms:play-services-maps:11.+'
    compile 'com.google.android.gms:play-services-location:+'

但是我的 gradle 文件中没有编译(项目....

我正在使用

 "react": "16.6.3",
 "react-native": "0.58.6",
 "react-native-geolocation-service": "^2.0.0",
 "react-native-gesture-handler": "^1.1.0",
 "react-native-maps": "^0.23.0",

如何解决这个问题

您打开了错误的文件。进入 Project/android/app/build.gradle 打开构建文件并粘贴依赖项

compile 已弃用,取而代之的是 implementation。您可以轻松地将单词 compile 的所有实例替换为单词 implemenation。所以你的依赖会变成这样:

dependencies {
    implementation(project(':react-native-maps')) {
      exclude group: 'com.google.android.gms', module: 'play-services-base'
      exclude group: 'com.google.android.gms', module: 'play-services-maps'
    }
    implementation 'com.google.android.gms:play-services-base:11.+'
    implementation 'com.google.android.gms:play-services-maps:11.+'
    implementation 'com.google.android.gms:play-services-location:+'


    // implementation project(':react-native-maps') // <- you can remove this as you are using it above
    implementation project(':react-native-geolocation-service')
    implementation project(':react-native-background-timer')
    implementation project(':react-native-mauron85-background-geolocation')
    implementation project(':react-native-contacts')
    implementation project(':react-native-gesture-handler')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
}

我在这上面花了很长时间,终于偶然发现了两件事。这进入你的 build.gradle 项目(不是 app/build.gradle 项目):

allprojects {
    repositories {
        configurations.all {
            resolutionStrategy.eachDependency { DependencyResolveDetails details ->
                def requested = details.requested
                if (requested.group == 'com.google.android.gms') {
                    details.useVersion '12.0.1'
                }
                if (requested.group == 'com.google.firebase') {
                    details.useVersion '12.0.1'
                }
            }
        }
        // ... whatever else you have here already
    }
}

只需在 app/build.gradle 中使用标准 react-native 设置,不要尝试排除此处的组 - 这对我不起作用。

这使得 Android <9.0 工作 - 太棒了。然而9.0+崩溃了,不爽。我偶然发现了这个信息:

If you are using com.google.android.gms:play-services-maps:16.0.0 
or below and your app is targeting API level 28 (Android 9.0) or 
above, you must include the following declaration within the 
<application> element of AndroidManifest.xml.

<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" 
/>

https://developers.google.com/maps/documentation/android-sdk/config#specify_the_google_play_services_version_number

我真的希望这个图书馆能尽快升级。我希望这对某人有所帮助。

+1 来自@hsearle 的回答 在我们的例子中,我们使用的是 react-native-maps,我假设它们以某种方式 link(猜猜谁不是真正的 react-native 专家);)

无论如何我们更新到 SDK 28 并且地图开始使应用程序崩溃。

下面的代码在 AndroidManifest.xml 的应用程序元素中获胜!!

    <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" 
    />

这是底层地图库的问题。另请参阅 Google 中的文档:-https://developers.google.com/maps/documentation/android-sdk/config#specify_requirement_for_apache_http_legacy_library

解决方案是:- 将这行代码放在 Manifest 文件中的 <application> 标记下:

`<uses-library android:name="org.apache.http.legacy" android:required="false"/>`

世博会:

"android": {
   "package": "com.codingmechanic.mapcuny",
   "config": {
      "googleMaps": { "apiKey": "<Your API Key>" }   
   }
}

Create a React Native App with Google Map using Expo.io