【问题标题】:Getting ERROR: Manifest merger failed after creation of Google Maps Activity project出现错误:创建 Google Maps Activity 项目后清单合并失败
【发布时间】:2019-07-07 22:46:44
【问题描述】:

我想了解更多关于 Google Maps API 的信息,所以我决定创建一个 Google Maps Activity 类型的项目。创建项目后,无需更改代码中的任何内容,我立即收到消息:

错误:清单合并失败:属性 应用程序@appComponentFactory 值=(android.support.v4.app.CoreComponentFactory)来自 [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 也出现在 [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 值=(androidx.core.app.CoreComponentFactory)。建议:加 'tools:replace="android:appComponentFactory"' 到元素 在 AndroidManifest.xml:12:5-41:19 处覆盖。

我已按照 https://developers.google.com/maps/documentation/android-sdk/start 的教程进行操作,因此我使用最新的可用 SDK 更新了环境并生成了 API 密钥(我已将其插入 google_maps_api.xml)。

我从您的网站上找到的可能解决方案是在 app 模块 build.gradle 的末尾添加以下代码段

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.0'
            }
        }
    }
}

但在那之后,我得到了错误:

不可转换的类型;无法将“android.support.v4.app.Fragment”转换为 'com.google.android.gms.maps.SupportMapFragment'

以下行:

SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

这是我的 activity_maps.xml 和 MapsActivity.java 的样子:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />
package com.example.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

此刻我真的很绝望,因为我已经尝试了我所知道的一切。我本来希望从 Android studio 提供的这个 Hello world 项目开始,构建一些更复杂的东西,但它似乎不起作用。拜托,我真的需要帮助。提前致谢

【问题讨论】:

  • 让我看看你的 AndroidManifest.xml 文件
  • Felipe,这里是 Google Drive 共享的链接。由于AndroidManifest.xml内容太大,无法评论,我已经上传到Google Drive:drive.google.com/open?id=1HuNRFfGP5nkpaoOOMDzaViZbw0XmhX0m
  • 您可以编辑您的问题以包含更多信息。我没有看到你的清单有什么问题,让我看看你的活动和片段布局
  • 或许本教程能帮到你:code.luasoftware.com/tutorials/android/…
  • Felipe,我已经编辑了问题,因此您可能会发现活动和片段布局的样子。我也会看看教程:)

标签: android


【解决方案1】:

这只是一种解决方法,但我通过以下方式使其工作。在 Android Studio 中 Gradle Scripts 下的 build.gradle(Module:app) 文件中,代码如下:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

implementation 'com.google.android.gms:play-services-maps:17.0.0'这行,我把版本从17.0.0改成了16.0.0,所以这行变成了

implementation 'com.google.android.gms:play-services-maps:16.0.0'

它奏效了。错误不再存在。

我在 2019 年 6 月 19 日使用 Google 地图活动创建了一个应用程序,它工作得很好,但是当我在 2019 年 6 月 20 日尝试这样做时,我遇到了同样的错误。所以我尝试比较这两个项目并遇到了这个问题。我对所有这些都很陌生,所以我不太了解它,但它确实有效。

【讨论】:

    【解决方案2】:

    为了使用play-services-maps17.0.0,您必须针对androidx 依赖项进行构建。

    android.support.v4.app.FragmentActivity -> androidx.fragment.app.FragmentActivity.

    为了migrate,需要替换每个com.android.support 依赖项。

    release notes 确认这一点 - 降级到 16.0.0 是另一个可用选项。

    【讨论】:

    • 这是最有意义的答案,imo
    【解决方案3】:

    也使用 16 版的谷歌地图服务和 16 版的位置服务。 这在我的情况下有效 实施 'com.google.android.gms:play-services-maps:16.0.0' 实施 'com.google.android.gms:play-services-location:16.0.0'

    【讨论】:

      【解决方案4】:

      如果您使用的是旧版本的 Android Studio,那么您必须将其更新到 3.4.1。
      你必须更新 sdk [编译 SdkVersion 29] gradle [到 gradle-5.1.1]。

      如果您对更新 IDE 不感兴趣,请使用以前版本的地图库,即
      implementation 'com.google.android.gms:play-services-maps:16.1.0'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        • 2020-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多