我正在尝试实现适用于Android的React NativeMapView
。我确实遵循了 github 安装说明,但是当我尝试使用react-native run-android
命令运行时,我遇到了编译错误。
任务:react-native-maps:compileDebugRenderscript FAILED
失败:生成失败,出现异常。
- 出了什么问题: 无法解析配置":react-native-maps:debugCompileClasspath"的所有文件。 无法解析 com.android.support:support-compat:26.1.0。 需要满足以下条件: 项目 :反应原生地图 找不到满足版本约束的"com.android.support:support-compat"版本: 依赖路径 'MapViewDemo:react-native-maps:undesignated ' --> 'com.facebook.react:react-native:0.59.0' --> 'com.android.support:appcompat-v7:28.0.0' - -> 'com.android.support:support-compat:28.0.0' 依赖路径 'MapViewDemo:react-native-maps:undesignated ' --> 'com.facebook.react:react-native:0.59.0' --> 'com.android.support:appcompat-v7:28.0.0' - -> 'com.android.support:support-core-utils:28.0.0' --> 'com.android.support:support-compat:28.0.0' 依赖路径 'MapViewDemo:react-native-maps:undesignated ' --> 'com.facebook.react:react-native:0.59.0' --> 'com.android.support:appcompat-v7:28.0.0' - -> 'com.android.support:support-fragment:28.0.0' --> 'com.android.support:support-compat:28.0.0' 依赖路径 'MapViewDemo:react-native-maps:undesignated ' --> 'com.facebook.react:react-native:0.59.0' --> 'com.android.support:appcompat-v7:28.0.0' - -> 'com.android.support:support-vector-drawable:28.0.0' --> 'com.android.support:support-compat:28.0.0' 依赖路径 'MapViewDemo:react-native-maps:undesignated ' --> 'com.google.android.gms:play-services-base:16.0.1' --> 'com.google.android.gms:play-serv ICES-basement:16.0.1' --> 'com.android.support:support-v4:26.1.0' --> 'com.android.support:support-compat:26.1.0' 依赖路径 'MapViewDemo:react-native-maps:undesignated ' --> 'com.facebook.react:react-native:0.59.0' --> 'com.android.support:appcompat-v7:28.0.0' - -> 'com.android.support:support-core-utils:28.0.0' --> 'com.android.support:loader:28.0.0' --> 'com.android.support:support-compat:28.0.0' 'com.android.support:support-annotations' 严格来说 '26.1.0' 因为以下 rea 子:debugRuntimeClasspath 使用版本 26.1.0 约束路径 'MapViewDemo:react-native-maps:unspecific' --> 'com.android.support:support-annotations' 严格来说 '26.1.0' 因为以下 rea 子:debugRuntimeClasspath 使用版本 26.1.0
-
尝试: 使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获取更多日志输出。使用 --scan 运行以获取完整的见解。
-
在 https://help.gradle.org 获取更多帮助
构建在 5 秒内失败 8 个可操作任务:8 个已执行 错误 无法在设备上安装该应用程序,有关详细信息,请阅读上面的错误。 确保您正在运行 Android 模拟器或连接了设备并具有 设置您的安卓开发环境: https://facebook.github.io/react-native/docs/getting-started.html 错误 命令失败:gradlew.bat app:installDebug
应用级别分级
apply plugin: "com.android.application"
import com.android.build.OutputFile
project.ext.react = [
entryFile: "index.js"
]
apply from: "../../node_modules/react-native/react.gradle"
def enableSeparateBuildPerCPUArchitecture = false
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false
android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.mapviewdemo"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86-64"
}
}
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86-64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
}
dependencies {
implementation project(':react-native-maps')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
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:16.1.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-v4:28.0.0'
force 'com.android.support:appcompat-v7:28.0.0'
}
}
项目级分级
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 20
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "28.0.0"
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
}
Package.json
{
"name": "MapViewDemo",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.8.3",
"react-native": "0.59.0",
"react-native-maps": "^0.23.0"
},
"devDependencies": {
"@babel/core": "7.3.4",
"@babel/runtime": "7.3.4",
"babel-jest": "24.5.0",
"jest": "24.5.0",
"metro-react-native-babel-preset": "0.53.0",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}
经过两天的挣扎,我自己找到了解决方案。真正的问题是我的项目使用compileSdkVersion = 28
和react-native-maps
node_module使用不同版本的compileSdkVersion
.
要解决此问题,需要更新react-native-maps
node_modulecompileSdkVersion
版本。
project_name>node_modules>react-native-maps>lib>android>build.gradle
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 20
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "28.0.0"
}
模块编译 SDK 版本和支持库版本应与项目编译 SDK 版本和支持库版本相同。
这也对我有用。
修改 android/build.gradle 如下
1) 在存储库中添加 google()
buildscript {
repositories {
jcenter()
// add google() here
google()
2) 将 com.android.tools.build.gradle 更新到 3.1.0
buildscript {
repositories {
jcenter()
google()
}
dependencies {
// classpath 'com.android.tools.build:gradle:2.2.3'
// update from 2.2.3 to 3.1.0
classpath 'com.android.tools.build:gradle:3.1.0'
3) 在依赖项之后的存储库中添加 google():
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
}
allprojects {
repositories {
mavenLocal()
jcenter()
// add googgle() here
google()
4) 将android.enableAapt2=false
添加到 android/gradle.properties
android.enableAapt2=false // < --- add here
android.useDeprecatedNdk=true
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
5) 在 android/gradle/wrapper/gradle-wrapper.properties 中更新 gradle 版本:
// from version 2.14.1
distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip
// change to 4.10.1
distributionUrl=https://services.gradle.org/distributions/gradle-4.10.1-all.zip
6) 运行react-native run-android
你能让它工作吗? 我找到了另一个解决方案。 就我而言,发生这种情况是因为 React-native (0.59.0) 的版本。 所以我将我的 package.json 文件格式中的 React-native 版本0.59.0
更改为^0.58.6
. 并将 React-native-maps 版本更改为^0.23.0
. 以下是其他步骤。
1) 在应用级别设置中,包括此内容
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/lib/android')
2)在项目级别gradle中,删除或注释
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 20
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "28.0.0"
}
(在构建脚本{}内)
3)也在项目级的毕业典礼上, 更改类路径。build.gradle 版本 to 3.1.1
classpath 'com.android.tools.build:gradle:3.1.1'
还要在 Maven{} 中添加另一个 URL(在 allProjects {} 中)
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "https://maven.google.com"
url "$rootDir/../node_modules/react-native/android"
}
4)现在更改应用程序级别等级如下
首先,将 compileSdkVersion 更改为 28 并添加新的 buildToolVersion
compileSdkVersion 28
buildToolsVersion "27.0.3"
然后,更改最小值和目标 Sdk版本,如下所示(在 defaultConfig 中)
defaultConfig {
applicationId "com.mapviewdemo"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
5) 在您的 AndroidManifest.xml 中,
首先添加此代码。 这是为了授予访问您设备上的位置的权限。 在标记之前添加<aplication>
此项。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
然后,您必须在</application>
标签之前添加元数据。
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBTSDBtI7_dfdfBeYkNAtdQ8sT67HiOuO0VU"/>
(添加您自己的 API 密钥用于android:value
)
6)然后运行npm install
并尝试这是否有效。 如果这不起作用,您必须从Android Studio下载Android SDK。就我而言,我下载了Android 7.0和6.0
我使用此 npm 安装解决了这个问题 — 保存"react-native-maps@jerolimov/react-native-maps#fix-rn59rc-compile-issues">
来自: https://github.com/react-native-community/react-native-maps/pull/2702
问题出在使用 compileSdkVersion 28 编译且映射使用 26 编译的 React 本机 0.59+
2 天后,我在我的项目中发现了问题。 我更改了compileSdkVersion、supportLibVersion和playServicesVersion的值android/build.gradle
和android/app/build.gradle
文件的值与node_modules/react-native-maps/lib/android/build.gradle
和node_modules/react-native-maps/build.gradle
文件中的值相同,这对我有用。
编译SdkVersion,supportLibVersion和playServicesVersion的值在以下所有文件中都是相同的。
在android/build.gradle文件中:
...
ext {
compileSdkVersion = 28
targetSdkVersion = 27
buildToolsVersion = "28.0.3"
minSdkVersion = 16
supportLibVersion = "28.0.0"
playServicesVersion = "16.1.0" // or set latest version
androidMapsUtilsVersion = "0.5+"
}
...
在android/app/build.gradle文件中:
...
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.reactnativeapp2"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
...
...
}
...
dependencies {
...
...
implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
}
...
在node_modules/react-native-maps/lib/android/build.gradle文件中:
...
android {
compileSdkVersion safeExtGet('compileSdkVersion', 28)
defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
targetSdkVersion safeExtGet('targetSdkVersion', 27)
}
}
dependencies {
...
implementation "com.android.support:appcompat-v7:${safeExtGet('supportLibVersion', '28.0.0')}"
implementation "com.google.android.gms:play-services-base:${safeExtGet('playServicesVersion', '16.1.0')}"
implementation "com.google.android.gms:play-services-maps:${safeExtGet('playServicesVersion', '16.1.0')}"
implementation 'com.google.maps.android:android-maps-utils:0.5'
}
...
在node_modules/react-native-maps/build.gradle文件中:
...
ext {
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = '28.0.0'
playServicesVersion = '16.1.0'
}
...