113

I'm using Eclipse to develop applications for android, and I want to integrate Admob to make money. The tutorial says I should watch the LogCat to find ID, but where is it? alt text

When I run in either the test mode or the real mode, sometimes the eclipse will notify that Ads returned, yet it does not show in the emu... can anyone explain?

Sheldon Rong
  • 1,506
  • 2
  • 14
  • 19

18 Answers18

114

The accepted answers will work if you are only testing on the Emulator or on a few devices, but if you are testing on a plethora of devices, you may need some means of prorammatically adding the running device's device ID.

The following code will make the current running device into an adview test device programmatically

...
    if(YourApplication.debugEnabled(this)) //debug flag from somewhere that you set
    {

        String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        String deviceId = md5(android_id).toUpperCase();
        mAdRequest.addTestDevice(deviceId);
        boolean isTestDevice = mAdRequest.isTestDevice(this);

        Log.v(TAG, "is Admob Test Device ? "+deviceId+" "+isTestDevice); //to confirm it worked
    }

You need to use the md5 of the Android ID, and it needs to be upper case. Here is the md5 code I used

public static final String md5(final String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest
                .getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        Logger.logStackTrace(TAG,e);
    }
    return "";
}

EDIT: Apparently that MD5 method isnt perfect, and it was suggested to try https://stackoverflow.com/a/21333739/2662474 I no longer need this feature so I havent tested.

starball
  • 20,030
  • 7
  • 43
  • 238
joseph
  • 1,165
  • 1
  • 7
  • 16
  • 1
    This is probably better than calling `com.google.android.gms.internal.cm.l(Context ctx)` which is the internal obfuscated method (in version 4.0.30) that returns returns the ID in question – Amr Mostafa Dec 07 '13 at 15:05
  • Thank you for this! Just to put it into Python, `import hashlib; hashlib.md5(android_id.lower()).hexdigest().upper()`. This way you can just download an app to show your Android ID and take the MD5 of that. – douggard Jan 27 '14 at 00:34
  • and the Settings class comes from what package? – dev4life Jul 23 '14 at 15:06
  • @joseph this is not working correctly. it misses some characters. – 5er Feb 21 '15 at 15:23
  • @5er What characters am I missing? – joseph Mar 10 '15 at 06:16
  • @joseph , it missed char 0 "zero". I didnt go deep into code, because I found another solution. I wrote you just to let you know. If you will dig into bug, I can send you the both phone serial numbers, and you will see where the error is. – 5er Mar 10 '15 at 08:31
  • 5
    See this answer: http://stackoverflow.com/a/21333739/4019544 This implementation is not handling some zeros, similiar to the accepted answer on the post linked here. – MaxJ Sep 15 '15 at 13:17
  • Why isn't this one the right answer? it works flawlessly on my device, but I replaced the md5 method with the one from the link MaxJ suggested as a precaution. – mike.adc Jul 29 '17 at 18:29
107

If you are running admob ads on an emulator then there is no ID. just use the AdManager method and set it to TEST_EMULATOR like the logcat says. If you run on an actual device with usb debugging and watch the logcat, the ID will appear in there.

Phobos
  • 9,447
  • 3
  • 25
  • 30
21
  • Is your app published on Play store -- with live ads:

If your app is on Play store showing live ads -- you can't use live ads for testing -- add your device ID in code to get test ads from Admob on your real device. Never use live ads during development or testing.

To get real device ID in logcat,

  1. Connect your device in USB debug mode to Android Studio

USB debug mode(Developer option)

  1. Open any app on your device which shows live ads from Admob: On the connected device, if you have your app downloaded from play store(showing live ads) open that app or else open any other app that shows live Admob ads. Your device should have an internet connection.

  2. Filter the logcat with 'device' as shown below to get test device

Test Device ID in logcat

Read Admob ad testing on device - device IDs can change for more

AndroidLearner
  • 741
  • 6
  • 11
17

Something similar to Google Ads, from the documentation:

public AdRequest.Builder addTestDevice (String deviceId)

Causes a device to receive test ads. The deviceId can be obtained by viewing the logcat output after creating a new ad. For emulators, use DEVICE_ID_EMULATOR.

for example my Test Device id displayed in LogCat is "B86BC9402A69B031A516BC57F7D3063F":

AdRequest adRequest = new AdRequest.Builder() 
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("B86BC9402A69B031A516BC57F7D3063F")
        .build();
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
15

To get the Hash Device ID

inside the oncreate

String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        String deviceId = md5(android_id).toUpperCase();
Log.i("device id=",deviceId);

then add this class for md5 ()

public String md5(String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i=0; i<messageDigest.length; i++)
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
  • Thanks, but there is actually a subtle bug in your md5() function. The line where you append to hexString should be this: `hexString.append(String.format("%02X", 0xFF & messageDigest[i]));` Your implemention will give incorrect output on byte values of 15 or less since the leading zero will not be output. – Ant Aug 09 '17 at 00:07
  • your md5() generate wrong ID, expected 561600E037F6E9EEBE405B487459D786 but 56160E037F6E9EEBE405B487459D786 – Foobnix Jan 19 '18 at 14:21
10

To get the device id, connect your phone to USB and open logcat in android studio Use the code below (make sure you have USB debugging enabled in your device). Then open any app (download any random app from play store) which has google Ad. In the Logcat type "set" as shown in the image. Your device id is shown highlighted in the image as

setTestDeviceIds(Arrays.asList("CC9DW7W7R4H0NM3LT9OLOF7455F8800D")).

enter image description here

Use the Test Device in your code as shown

val adRequest = AdRequest
        .Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("CC9DW7W7R4H0NM3LT9OLOF7455F8800D")
        .build()
Ahmed Raza
  • 1,343
  • 1
  • 15
  • 18
7

If you are displaying ads using XML layout and if you already have "ads:testDevices=" in your layout XML file, AdMob will NOT print the "To get test ads on this device..." message in the LogCat output. Take that out and then you will see the LogCat message.

Here is a nice tutorial on how to find device id in LogCat: http://webhole.net/2011/12/02/android-sdk-tutorial-get-admob-test-device-id/

Oto Zars
  • 623
  • 10
  • 17
7

If you are testing your app on actual device then you can try this small android app which gives you the device id:

https://play.google.com/store/apps/details?id=pe.go_com.admobdeviceidfinder&hl=en

You will get the hashed device id directly. Hope this helps.

Sanath Bharadwaj
  • 733
  • 1
  • 8
  • 21
  • I am not sure this is what the question was about. As I remember from the emulator you cannot install from the Play store. Probably a more programmatical solution (or the relevant parts of the source of this app) would be better. – Gábor Bakos Mar 06 '15 at 06:48
  • 1
    Well so many of them have posted programmatical solutions. I just posted this because its a little easy and straight forward way to get the id though it does not work on emulator. Anyways i edited the answer. – Sanath Bharadwaj Mar 06 '15 at 19:00
6

Another easiest way to show test ads is to use test device id for banner to show admob test ads for all devices. "ca-app-pub-3940256099942544/6300978111" . This admob test ads id was noted in the admob tutorial of google: link. This is the quote from the above link: enter image description here

  • This is the test device id for interstitial "ca-app-pub-3940256099942544/1033173712" . This also was used in interstitial tutorial
huu duy
  • 2,049
  • 21
  • 31
5

If you don't get it in the logcat just put any device id and load you Ads and run your app then go to the log you will get it like that I/Ads: Use AdRequest.Builder.addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXX") to get test ads on this device. after that put it and run your application again.

Context
  • 1,873
  • 1
  • 20
  • 24
4

I have a few devices I was testing on, and didn't want to manually get the DeviceID for each one. The answers here to programmatically get the DeviceIDs were not working for me (Missing zeros) which caused real ads to be shown instead of test ads.

I put this in my Application class onCreate, and then exposed deviceId using a getter method so that it can be accessed throughout.

@Override
public void onCreate() {        
    super.onCreate();

    String androidId =  Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
    deviceId = MD5(androidId).toUpperCase();        
}  

public static String getDeviceId() {
    return deviceId;
}

private static String deviceId;

And the MD5 method;

public String MD5(String md5) {
   try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

Then using this whenever I create an AdRequest object:

if(BuildConfig.DEBUG) {
     AdRequest adRequest = new AdRequest.Builder()
          .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
          .addTestDevice(Application.getDeviceId())
          .build();
     adView.loadAd(adRequest);
} else {
     AdRequest adRequest = new AdRequest.Builder()
          .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
          .build();
     adView.loadAd(adRequest);
}
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
MaxJ
  • 1,945
  • 2
  • 12
  • 19
4

Works to this way:

InterstitialAd mInterstitial = new InterstitialAd(this);
    mInterstitial.setAdUnitId("your id");
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("some words")
    .build();
    mInterstitial.loadAd(adRequest);

After run the app... Go in Logcat put in Verbose put in the search field AdRequest, so the id device shows donw.

enter image description here

.addTestDevice("put the id here");

I hope have helped;

Douglas
  • 479
  • 4
  • 7
4

Add this class to your project

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

import com.google.android.gms.ads.AdRequest;
import java.io.UnsupportedEncodingException;

public class AdsHelper {
public static AdRequest createRequest(Context context) {
    AdRequest.Builder adRequest = new AdRequest.Builder();
    adRequest.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    if (BuildConfig.DEBUG) {
        String deviceId = MD5(getDeviceId(context));
        if (!TextUtils.isEmpty(deviceId)) {
            adRequest.addTestDevice(deviceId.toUpperCase());
        }
    }

    return adRequest.build();
}



    private static String MD5(String md5) {
        if (TextUtils.isEmpty(md5)) return null;
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(md5.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte anArray : array) {
                sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException ignored) {
        } catch(UnsupportedEncodingException ignored){
        }
        return null;
    }

    private static String getDeviceId(Context context) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } catch (Exception e) {
            return "";
        }

    }
}

Usage:

AdRequest adRequest = AdsHelper.createRequest(this);
Gil SH
  • 3,789
  • 1
  • 27
  • 25
0

app: build.gradle

dependencies {
...
compile 'com.google.firebase:firebase-ads:10.0.1'
...
}

Your Activity:

 AdRequest.Builder builder = new AdRequest.Builder();
        if(BuildConfig.DEBUG){

            String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            String deviceId = io.fabric.sdk.android.services.common.CommonUtils.md5(android_id).toUpperCase();
            builder.addTestDevice(deviceId);
        }
        AdRequest adRequest = builder.build();
    adView.loadAd(adRequest);
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
0

For Emulator, there is no need to add device in "Testing Device"

For Real Device, Find Advertising ID as below and add into Admob Account enter image description here

amoljdv06
  • 2,646
  • 1
  • 13
  • 18
0

I've used this code for one of my project. Hope it helps everyone :) Rajji

public String getDeviceId(){
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    String androidId = 
  Settings.Secure.getString((ContentResolver)this.getContentResolver(),"android_id");
    messageDigest.update(androidId.getBytes());
    byte[] arrby = messageDigest.digest();
    StringBuffer sb = new StringBuffer();
    int n = arrby.length;
    for(int i=0; i<n; ++i){
        String oseamiya = Integer.toHexString((int)(255 & arrby[i])));
        while(oseamiya.length() < 2){
            oseamiya = "0" + oseamiya;
        }
        sb.append(oseamiya);
    }
    try{
        String result = sb.toString();
        return result;
    }catch(NoSuchAlgorithmException e){
        e.printStackTrace();
        return "";
    }

}
Oseamiya
  • 67
  • 9
0

Kotlin solution:

To get the current device ID:

@SuppressLint("HardwareIds")
private fun getDeviceIdForAdMobTestAds(context: Context): String? {
    val md5 = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
    try {
        val md = MessageDigest.getInstance("MD5")
        val array = md.digest(md5.toByteArray())
        val sb = StringBuilder()
        for (i in array.indices)
            sb.append(Integer.toHexString(array[i].toInt() and 0xFF or 0x100).substring(1, 3))
        return sb.toString()
    } catch (e: NoSuchAlgorithmException) {
    }
    return null
}

Usage:

val deviceIds = arrayListOf(AdRequest.DEVICE_ID_EMULATOR)
getDeviceIdForAdMobTestAds(context)?.let { deviceIds.add(it.uppercase(Locale.ROOT)) }
MobileAds.setRequestConfiguration(RequestConfiguration.Builder().setTestDeviceIds(deviceIds).build())
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

For Real Device type TestDevice in logs enter image description here

Moeenuddin178
  • 39
  • 1
  • 9