【问题标题】:Android Studio Error when Creating a Project with a Google Maps Activity使用 Google Maps Activity 创建项目时出现 Android Studio 错误
【发布时间】:2019-07-20 00:03:11
【问题描述】:

当我使用 Google Maps Activity 在 Android Studio 中创建一个新项目时,我在进行项目的初始构建时收到以下错误:

清单合并失败:属性 application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 也存在于 [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory)。 建议:将 'tools:replace="android:appComponentFactory"' 添加到 AndroidManifest.xml:12:5-41:19 的元素以覆盖。

当我添加时:

tools:replace="android:appComponentFactory

到我的AndroidManifest.xml 文件,我收到以下错误:

清单合并失败并出现多个错误,请参阅日志

我在 StackOverflow 上找到了几个潜在的解决方案,但没有一个可行。

我尝试了几种在 StackOverflow 上找到的潜在解决方案,包括:

  • 迁移到 AndroidX

  • 将以下行添加到我的 清单

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

  • 删除实现com.android.support:appcompat-v7:28.0.0-rc01

主要活动代码

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 清单:

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

【问题讨论】:

    标签: android maps fragment manifest jave


    【解决方案1】:

    迁移到 androidX 应该可以解决问题,将这两个标志添加到您的 gradle.properties 文件中。通过转到 android studio 菜单栏中的 Refactor &gt; Migrate to AndroidX 进行重构

    android.useAndroidX=true
    android.enableJetifier=true
    

    【讨论】:

      【解决方案2】:

      答案应该与 AndroidX 相关联,并确保您已将所有支持库迁移到 AndroidX。这将包括设计库。我有同样的问题,一旦我迁移它们就可以了。在这里查看所有可能的库https://developer.android.com/jetpack/androidx/migrate

      【讨论】:

      • 其实我宁愿不用androidx。这只是对另一个我认为值得尝试的问题的建议。
      【解决方案3】:

      您需要做的就是更新您的 android studio,然后一切顺利。我创建了一个新项目,它运行良好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-30
        • 1970-01-01
        • 2018-12-01
        • 1970-01-01
        相关资源
        最近更新 更多