react-native-maps 构建失败

Posted

技术标签:

【中文标题】react-native-maps 构建失败【英文标题】:react-native-maps build failed 【发布时间】:2019-12-30 16:09:48 【问题描述】:

我安装了 react-native-maps,但是当我尝试构建它时失败并给出以下错误,尽管 javac 可以工作

> Task :react-native-maps:compileDebugJavaWithJavac
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:12: e
rror: package androidx.core.view does not exist
import androidx.core.view.GestureDetectorCompat;
                         ^
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:13: e
rror: package androidx.core.view does not exist
import androidx.core.view.MotionEventCompat;
                         ^
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:73: e
rror: package androidx.core.content does not exist
import static androidx.core.content.PermissionChecker.checkSelfPermission;
                                   ^
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:73: e
rror: static import only from classes and interfaces
import static androidx.core.content.PermissionChecker.checkSelfPermission;
^
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:108:
error: cannot find symbol
  private final GestureDetectorCompat gestureDetector;
                ^
  symbol:   class GestureDetectorCompat
  location: class AirMapView
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:165:
error: cannot find symbol
        new GestureDetectorCompat(reactContext, new GestureDetector.SimpleOnGest
ureListener() 
            ^
  symbol:   class GestureDetectorCompat
  location: class AirMapView
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:421:
error: cannot find symbol
    return checkSelfPermission(getContext(), PERMISSIONS[0]) == PackageManager.P
ERMISSION_GRANTED ||
           ^
  symbol:   method checkSelfPermission(Context,String)
  location: class AirMapView
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:422:
error: cannot find symbol
        checkSelfPermission(getContext(), PERMISSIONS[1]) == PackageManager.PERM
ISSION_GRANTED;
        ^
  symbol:   method checkSelfPermission(Context,String)
  location: class AirMapView
D:\COURSES\ReactNative-ThePracticalGuide\myFirstApp\node_modules\react-native-ma
ps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMapView.java:949:
error: cannot find symbol
    int action = MotionEventCompat.getActionMasked(ev);
                 ^
  symbol:   variable MotionEventCompat
  location: class AirMapView
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
9 errors

> Task :react-native-maps:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native-maps:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我的包的版本 “反应原生”:“0.59.10”, "react-native-maps": "0.26.1",

app/build.gradle 文件中的依赖项

dependencies 
    implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibVersion"
    implementation 'com.android.support:design:27.1.0' 
    implementation "com.facebook.react:react-native:+" // From node_modules
    compile fileTree(dir: "libs", include: ["*.jar"]) 
    compile project(':react-native-vector-icons') 
    compile project(':react-native-navigation')
    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:10.0.1'
   implementation 'com.google.android.gms:play-services-maps:10.0.1'

我仔细按照安装说明进行操作,但构建仍然失败。 这个问题有什么解决办法吗?

【问题讨论】:

【参考方案1】:

我找到了解决问题的方法。 首先,我将以下行添加到 ./android/gradle.properties

android.useAndroidX=true
android.enableJetifier=true

然后添加jetifier会修复它

npm install --save-dev jetifier
npx jetify
npx react-native run-android

这为我解决了问题。

【讨论】:

【参考方案2】:

尝试将 react native 升级到 0.60+ 版本,不要忘记取消链接 react native 地图

【讨论】:

以上是关于react-native-maps 构建失败的主要内容,如果未能解决你的问题,请参考以下文章

React Native AwesomeProject 构建失败

如何在 react-native-map 中放大/缩小?

react-native-maps 中的当前位置

React-Native-Maps:地图为空。仅显示 Google 徽标和标记

React-Native-Maps:如何动画来协调?

缩放到指定的标记 react-native-maps