我是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。您必須使用相同的證書。
如何解決這個問題?
你的問題是因爲你使用不同的密鑰簽署的apk。不是因爲admob。 –
Okay @ישואוהבאותך。但如何糾正鑰匙? – Kvaibhav01
請在這裏閱讀https://guides.codepath.com/android/Publishing-to-the-Play-Store –