在Android Studio中运行应用程序时com.google.android.gms实现错误

时间:2018-10-14 04:14:19

标签: android admob

运行Android App时出现以下错误:

Error: Program type already present: com.google.android.gms.common.api.zza

这是下面的build.gradle模块文件:

apply plugin: 'com.android.application'

android {
  compileSdkVersion 28
  buildToolsVersion '28.0.3'
  defaultConfig {
    applicationId 'com.example.photoeditor'
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    manifestPlaceholders = [appPackageName: "${applicationId}"]
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/DEPENDENCIES'
    pickFirst 'AndroidManifest.xml'
  }

  dexOptions {
    jumboMode true
  }

  productFlavors {}
}

dependencies {
  implementation fileTree(include: ['*.jar'], dir: 'libs')
  implementation 'com.google.android.gms:play-services-ads:17.0.0'
  implementation 'com.adobe.creativesdk.foundation:auth:0.9.1251'
  implementation 'com.adobe.creativesdk:image:4.8.4'
  implementation 'com.localytics.android:library:4.0.1'
  testImplementation 'junit:junit:4.12'
}

这是下面的build.gradle项目文件:

buildscript {
  repositories {
    jcenter()
    google()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    classpath 'me.tatarka:gradle-retrolambda:3.6.1'
  }
}

allprojects {
  repositories {
    jcenter()
    google()
    mavenCentral()

    maven {
      url 'https://repo.adobe.com/nexus/content/repositories/releases/'
    }

    maven {
      url 'http://maven.localytics.com/public'
    }
  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

这是下面的MainActivity.java文件:

package com.example.photoeditor;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import com.adobe.creativesdk.aviary.AdobeImageIntent;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
  private AdView mAdView;
  public static final String IMAGE_URI = "IMAGE_URI_KEY";

  private static final String TAG = "MainActivity";
  private static final int IMAGE_EDITOR_RESULT = 1;

  private ImageView mEditedImageView;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAdView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    mEditedImageView = (ImageView) findViewById(R.id.edited_image_view);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      Uri imageUri = Uri.parse(getIntent().getExtras().getString(IMAGE_URI));
      Intent imageEditorIntent = new AdobeImageIntent.Builder(this).setData(imageUri).build();
      startActivityForResult(imageEditorIntent, IMAGE_EDITOR_RESULT);
      finish(); // Comment this out to receive edited image
    }
  }

  // Do something with the edited image
  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case IMAGE_EDITOR_RESULT:
          Uri editedImageUri = data.getParcelableExtra(AdobeImageIntent.EXTRA_OUTPUT_URI);
          Log.d(TAG, "editedImageUri: " + editedImageUri.toString());
          Bundle extra = data.getExtras();
          if (extra != null) {
            boolean changed = extra.getBoolean(AdobeImageIntent.EXTRA_OUT_BITMAP_CHANGED);
            Log.d(TAG, "Image edited: " + changed);
            if (changed) {
              mEditedImageView.setImageURI(editedImageUri);
            }
          }
          break;

        default:
          throw new IllegalArgumentException("Unexpected request code");
      }
    }
  }

  public static Intent getIntent(Context context, Bundle bundle) {
    Intent intent = new Intent(context, MainActivity.class);
    if (bundle != null) {
      intent.putExtras(bundle);
    }
    return intent;
  }
}

这是下面的activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context="com.example.photoeditor.MainActivity">

  <com.google.android.gms.ads.AdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      ads:adSize="BANNER"
      ads:adUnitId="@string/banner_id">
  </com.google.android.gms.ads.AdView>

  <ImageView
      android:id="@+id/edited_image_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:adjustViewBounds="true"
      android:contentDescription="@string/edited_image_content_desc"/>

</LinearLayout>

我尝试添加用于实施Google gms的不同版本,但仍然无法正常工作。我要去哪里错了?

是否需要在此处放置其他文件以找到解决方案?

有人可以帮忙解决它吗?

预先感谢

2 个答案:

答案 0 :(得分:0)

尝试

<com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>

在Java代码中

MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
mAdView.loadAd(adRequest);

答案 1 :(得分:0)

如果您使用的是Google Mobile Ads SDK版本17. +,则需要添加
清单文件中广告的元数据。

有关更多详细信息,请参见https://developers.google.com/admob/android/quick-start