'Android Studio Error when Creating a Project with a Google Maps Activity

When I create a new project in Android Studio with a Google Maps Activity I get the following error when I do the initial build of the project:

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.

When I add:

tools:replace="android:appComponentFactory

to my AndroidManifest.xml file I get the error below:

Manifest merger failed with multiple errors, see logs

I've found several potential solutions on StackOverflow but none work.

I've tried several potential solutions that I've found on StackOverflow including:

  • migrating to AndroidX

  • Adding the following lines to my manifest

tools:replace="android:appComponentFactory android:appComponentFactory="whateverString"

  • Removing implementation com.android.support:appcompat-v7:28.0.0-rc01

Main Activity code

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

Android Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/.
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


Solution 1:[1]

Migrating to androidX should do the trick, add these two flags to your gradle.properties file. Refactor by going to Refactor > Migrate to AndroidX in android studio menu bar

android.useAndroidX=true
android.enableJetifier=true

Solution 2:[2]

The answer should be tied to AndroidX and making sure you have all support libraries migrated to AndroidX. This would include design libraries. I had the same issue and once I migrated them over it worked. Check out all possible libraries here https://developer.android.com/jetpack/androidx/migrate

Solution 3:[3]

All you need to do is to update your android studio and you are good to go. I created a new project and it ran fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Krista Marie
Solution 2 Brad
Solution 3 eeobryanxx