It makes a lot of sense that Android ad SDKs will use Android's the new advertiser id.
It seems that you can only get the id by using the google services sdk, as mentioned here: http://developer.android.com/google/play-services/id.html.
Using the google play services sdk, requires referencing the google-play-services_lib project, which causes several problems:
- A lot of SDKs are jars, meaning they can't use google-play-services_lib as is (because they can't include resources).
- If I only want the advertiser ID, I need to add google-play-services_lib to my project, which weights almost 1 MB.
Is there a way to only get the advertiser id, without using resources?
Adrian's solution is excellent, and I use it myself.
However, today I discovered that it has a bug when Google Play Services is not installed on the device. You will get a message about leaking a
ServiceConnection
when your activity/service is stopped. This is actually a bug inContext.bindService
: when binding to the service fails (in this case because Google Play Services is not installed),Context.bindService
returns false, but it doesn't clear the reference to theServiceConnection
, and expects you to callContext.unbindService
even though the service doesn't exist!The workaround is to change the code of
getAdvertisingIdInfo
like this:That way
Context.unbindService
will be called even ifContext.bindService
returnsfalse
.The only supported method of accessing Advertising ID is by directly linking to the Play Services SDK and accessing Advertising ID via those APIs. Google does not recommend or support any workaround that avoids direct access to the Play Services APIs because it breaks user facing functionality (such as error handling in cases where the Play Services app on the device is outdated) and its behavior will be unpredictable with future Play Services releases.
The Google Play Developer Program Policies require that you access the Google Play APIs only in an authorized manner.
MoPub and a few other big players are not including GPS into their SDKs. From MoPub's help page:
Check this link for a lot more detail:
http://help.mopub.com/customer/portal/articles/1523610-google-advertising-id-faqs
Hope this helps.
NOTE: My answer is outdated for Gradle since now you can choose which parts of the GooglePlayServices library you want to include in your project
I ran into the same problem lately when the project I was working on reached the 65k dex limit.
Here's how i solved it:
Go to https://code.google.com/p/jarjar/downloads/list and download the latest Jar jar Links in .jar format. Put the file in a work folder. For this example I'll use the desktop.
Go to [Android SDK Path]\extras\google\google_play_services\libproject\google-play-services_lib\libs and copy google-play-services.jar to the same work folder.
In the same folder make a text file named rules.txt (the name doesn't really matter).
Inside the rules.txt paste the text (without the quotes):
If you want other classes you want to keep, you can add them here.
Open a command prompt file and change the path to your working folder. On Windows use the [cd] command.
Write the following command:
You can find more details about the JarJar Links commands and rules here: https://code.google.com/p/jarjar/wiki/CommandLineDocs
Just to give an example, the command I had to write looked like this (change yours according to your file names):
WHAT IT DOES:
HOW TO USE IT:
Import google play services from the sdk into your project as usual, make sure to copy it into your workspace. In the libs folder, replace the google-play-services.jar with the jar you generated earlier.
If you're there, you can delete the resources too to free another 0.5 mb. Make sure to keep values/common_strings.xml and values/version.xml.
Don't forget to add manifest metadata for google play services.
This helped me to reduce the project size with more than 2.5 mb and to stay under the 65k dex classes and methods limit while being able to access the Google advertiser id.
Hope it'll help you too.
I ran into the same issue, if you just need the advertiserId you could interact with the Google Play Service directly using an Intent. Example of custom class:
Make sure that you are not calling this from the main UI thread. For example, use something like: