4

i am trying to add Ads to my current activity but the activity is not showing any Ad . i have also add internet permission to Android Manifest.xml

Here is my

.Gradle File

 apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.0"

        defaultConfig {
            applicationId "sms.and.call.alert.flashlight.lpa.com.myapplication"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.google.android.gms:play-services:9.6.0'

        compile 'com.google.firebase:firebase-ads:9.6.0'
    }

This is MainActivity

  public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest request = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
                  // An example device ID
                .build();
        mAdView.loadAd(request);

    }
}

This is MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="sms.and.call.alert.flashlight.lpa.com.myapplication.MainActivity">
    <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>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>
Kumail Hussain
  • 869
  • 2
  • 26
  • 49
  • you need this: apply plugin: 'com.google.gms.google-services' at the end of your app build.gradle instead of compile 'com.google.android.gms:play-services:9.6.0' and you need to put the google-services.json file in the correct place – MatPag Oct 04 '16 at 10:40
  • i have put the json file in app folder seems fine? but giving me error pugin with id not found – Kumail Hussain Oct 04 '16 at 10:46
  • @KumailHussain you have to copy it in the app folder. Here the instructions for the plugin: https://firebase.google.com/docs/admob/android/quick-start#integrate_firebase_and_the_mobile_ads_sdk – MatPag Oct 04 '16 at 10:47
  • yes copied it in app folder – Kumail Hussain Oct 04 '16 at 10:47
  • @ShrenikShah i have mentioned that in the description i have added the permission too – Kumail Hussain Oct 04 '16 at 10:48
  • Follow the link i provided you in the previous comment to check if you have added the plugin correctly – MatPag Oct 04 '16 at 10:48
  • yes, i have exactly followed the tutorial but receiving error i have mentioned above – Kumail Hussain Oct 04 '16 at 10:51
  • if you have followed exactly the tutorial why you have a different build.gradle file? the one you posted in the question it's not the same showing in the tutorial i linked you – MatPag Oct 04 '16 at 10:53
  • because i was receiving error in plugin error "plugin with id not found" then changed the line to compile – Kumail Hussain Oct 04 '16 at 10:54
  • also check whether your unit ID is for banner only. if it is for interstitial than it won't load ad into adView – Shrenik Shah Oct 04 '16 at 10:54
  • but you need the plugin to let it work. Have you updated the google repository to the lastest version in the SDK manager? then retry using the plugin like the tutorial do – MatPag Oct 04 '16 at 10:55
  • what's the exact error the compiler give you when you try to build with the plugin? – MatPag Oct 04 '16 at 10:57
  • Error:(28, 0) Plugin with id 'com.google.gms.google-services' not found. Open File – Kumail Hussain Oct 04 '16 at 10:58
  • Ok, have you added the classpath 'com.google.gms:google-services:3.0.0' in the project build.gradle dependency? check this if you dont know how to do it https://firebase.google.com/docs/android/setup#add_the_sdk – MatPag Oct 04 '16 at 10:59
  • after adding it i receive error – Kumail Hussain Oct 04 '16 at 11:03
  • Error:Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package name 'sms.and.call.alert.flashlight.lpa.com.myapplication' – Kumail Hussain Oct 04 '16 at 11:03
  • @KumailHussain have you re-added the plugin at the end of the gradle.build? – MatPag Oct 04 '16 at 12:13
  • @KumailHussain and you should check that your applicationId value it's the same of your package tag in the manifest. – MatPag Oct 04 '16 at 12:22
  • I've provided you with a more exhaustive answer below which could be usefull to many others. Hope you will be able to get it works ;) – MatPag Oct 04 '16 at 12:58

2 Answers2

7

Admob-Firebase initial configuration guide

updated 10/04/2016 - MM/dd/yyyy

Step 1 : Be sure to have the latest version of Google Play Services and Google Repository SDK versions

Step 2 : Put the google-services.json in the app folder after you had created the project on Firebase console
More Info:https://firebase.google.com/docs/android/setup#add_firebase_to_your_app

Step 3 : Set up your project level build.gradle as below.
More Info: https://firebase.google.com/docs/android/setup#add_the_sdk

buildscript {
    // ...
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

Step 4 : Setup your app level build.gradle as below:
More info: https://firebase.google.com/docs/admob/android/quick-start#integrate_firebase_and_the_mobile_ads_sdk

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "your.package.app"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')

    compile 'com.google.firebase:firebase-ads:9.0.0'
}

apply plugin: 'com.google.gms.google-services'

Step 5 : Add those permissions to the AndroidManifest if they are not present yet

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Moreover be sure that your package name in the AndroidManifest match the applicationId of you app build.gradle or you will get a compilation error.

Step 6 : Continue to follow the Quick-Start tutorial and learn how to create Ad-Units to show ads on your views: https://firebase.google.com/docs/admob/android/quick-start

Step 7 : If you still have compile time problems, check those really usefull and common-case threads and examples:
Manifest and applicationId problems: https://stackoverflow.com/a/37177173/2910520,
Multiple flavors problems: https://stackoverflow.com/a/34990999/2910520
Google Admob examples:https://github.com/googleads/googleads-mobile-android-examples

Community
  • 1
  • 1
MatPag
  • 41,742
  • 14
  • 105
  • 114
0

Maybe you forgot to load the Ad by calling:

AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(request);
Community
  • 1
  • 1