Getting ERROR: Manifest merger failed after creation of Google Maps Activity project

Viewed 2872

I want to learn more about Google Maps API, so I decided to create a Google Maps Activity type of project. Immediately after the creation of project, without changing anything in code, I've got message:

ERROR: Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:12:5-41:19 to override.

I've followed tutorial from https://developers.google.com/maps/documentation/android-sdk/start, so I've updated environment with the newest available SDK and generated API key (which I've inserted in google_maps_api.xml).

What I've found from your website as possible solution is addition of following code segment at the end of app module 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'
            }
        }
    }
}

but after that, I've got error:

Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment'

for following line:

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

Here is how my activity_maps.xml and MapsActivity.java look like:

<?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));
    }
}

I am really desperate at this moment, because I've literally tried everything that I know. I've expected to start from this Hello world project provided by Android studio and to build something more complex, but it seems it doesn't work. Please, I really need help. Thanks in advance

4 Answers

It's just a workaround but I made it work in the following way. In build.gradle(Module:app) file under Gradle Scripts in the Android Studio, in the following lines of code:

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'
}

In the line implementation 'com.google.android.gms:play-services-maps:17.0.0', I changed the version from 17.0.0 to 16.0.0 and so the line became

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

And it worked. The error was not there anymore.

I had created an app using the Google Map Activity on 19-Jun-2019 and it worked just fine but when I tried to do so on 20-Jun-2019 I got the same error. So I tried comparing the two projects and ran into this. I am very new to all these so I don't know much about it, but it worked.

In order to use play-services-maps 17.0.0, you have to build against androidx dependencies.

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

In order to migrate, every single com.android.support dependency needs to be substituted.

The release notes confirm this - downgrading to 16.0.0 is the other option available.

use version 16 of google maps service and version 16 of location service too . This worked in my case implementation 'com.google.android.gms:play-services-maps:16.0.0' implementation 'com.google.android.gms:play-services-location:16.0.0'

If u are using older version of Android Studio then u must have to update it to 3.4.1.
u have to update sdk [to compileSdkVersion 29] gradle [to gradle-5.1.1].
or
if u are not interested to update your IDE then use previous version of maps library and that is
implementation 'com.google.android.gms:play-services-maps:16.1.0'

Related