2016-12-05 33 views
0

我是Android開發新手,想知道如何在Android應用中添加廣告。我通過各種Firebase和Android Developers鏈接進行了研究,最後添加了代碼。在Android Studio中添加廣告

ActivityMain.XML:

<com.google.android.gms.ads.AdView 
    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="@string/banner_ad_unit_id"> 
</com.google.android.gms.ads.AdView> 

的AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="my.package.name"> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

應用級別的gradle:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "my.ID" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 2 
     versionName "2" 
     multiDexEnabled true 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

    dexOptions { 
     javaMaxHeapSize "4g" 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.3.0' 
    compile 'com.android.support:support-v4:23.3.0' 
    compile 'com.android.support:design:23.3.0' 
    compile 'com.google.firebase:firebase-core:10.0.1' 
    compile 'com.google.android.gms:play-services:10.0.1' 
    compile 'com.google.android.gms:play-services-ads:10.0.1' 
    compile 'com.android.support:multidex:1.0.0' 
} 
apply plugin: 'com.google.gms.google-services' 

項目級的build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.1.3' 
     classpath 'com.google.gms:google-services:3.0.0' 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
    } 
} 

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

的strings.xml:

<string name="banner_ad_unit_id">AppID from AdMob</string> 

MainActivity.JAVA:

MobileAds.initialize(getApplicationContext(), "ca-app-pub-APPID from ADMob"); 

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

有哪些我的東西失蹤?當我將其作爲Play商店中已發佈應用的更新發布時,是否可以看到廣告?另外,Admob將如何支付我?

運行應用程序後沒有錯誤。

謝謝, Kvaibhav01。

更新:我試圖發佈應用,release.apk文件在開發者控制檯,它說:

您上傳的用不同的證書籤署 你以前的APK的APK。您必須使用相同的證書。

如何解決這個問題?

+1

你的問題是因爲你使用不同的密鑰簽署的apk。不是因爲admob。 –

+0

Okay @ישואוהבאותך。但如何糾正鑰匙? – Kvaibhav01

+0

請在這裏閱讀https://guides.codepath.com/android/Publishing-to-the-Play-Store –

回答

0

您上傳了使用不同證書籤名的APK到以前的APK。您必須使用相同的證書。 如何解決這個問題?

我搜索了other StackOverflow questions並得知上述查詢的答案只有一個。也就是,APK發佈文件必須使用相同的私鑰或您稱之爲KeyStore的內容進行簽名。(Java KeyStore更具體。)

我犯的錯誤是我不小心刪除了原來的KeyStore文件。現在,如果我使用新的KeyStore上傳更新後的APK,Google開發者控制檯將無法將其識別爲已有應用的更新。相反,它假定您正在上傳新應用的APK。這就是爲什麼你會得到上述錯誤。即使更新versionCode錯誤仍然存​​在。

您可以在這裏閱讀詳細的解決方案:https://stackoverflow.com/a/26296942/3647180