Skip to main content

Google Play Services with Android Studio

Edit: This post is extremely deprecated -- with prejudice! It was written in an era when Google Play Services were not well integrated with Android Studio project work, and Android Studio itself was in its infancy.

This is a very quick guide to incorporating Google Play Services with your new Android Studio project.

Edit: [16:20 22/05/2013] I'll investigate the runtime NoClassDefFound error reported in the comments, and follow up later!

Edit: [23:17 27/05/2013] I'm coming to the conclusion that - as many have already pointed out - you really do need to include the entire library project in your solution. I'll post an update once I've fully tested this. 


In the meantime, please consider the advice below to be deprecated!


The first thing to say is: I fully expect the advice and guidance about how to work in Android Studio to change over time. Android Studio is in early access preview right now, and I'll bet my bottom dollar (is that a thing?) that over time it becomes much much quicker and easier to add existing and understood services to an Android project.

That said, Android Studio is brilliant. I've been longing for an IntelliJ-like solution for some time now, and it's great to see how well developed it is, even in the early access preview.



Getting hold of the library

The library is distributed through the Android SDK manager. If you have that, you can find Google Play Services. In Android Studio, there's a little button along the top button bar that launches the Android SDK. Here's the SDK, and note you'll find Google Play Services under Extras.


Select and install Google Play Services. This will pull a small library project into the Location controlled by the SDK manager. Most of the project is safe to ignore - it's just a shell that contains the jar you need.

Copy google-play-services.jar from the libs/ folder of the project into the libs/ folder of your project.

Now allow Android Studio to notice the new file and update the project structure.

Right click the jar, and choose Add as Library. This will insert the library into your project in such a way that Android Studio will treat it as a library. Great! Now if you start to reference objects from Google Play Services (try the GooglePlayServicesUtil class), Android Studio knows what you're referring to.

However, the job's not quite complete. It added the Library, but didn't insert it into your Gradle's build file (invoked by Make Project from the build menu).

Open up the build.gradle file for your module (not the whole solution!) and insert this line into your dependencies:
  • compile files('libs/google-play-services.jar')
Now when you make the project, it should build to completion.

Checking for the runtime

The actual Google Play Services runtime is an APK distributed by Google Play. Slightly complicatedly, it's not guaranteed to be installed on all devices (although the newer ones will play ball) for various reasons - so it makes sense to check for it.

You can do that with the GooglePlayServicesUtil.isGooglePlayServicesAvailable(Context) method.

Google leaves it your discretion as to when you should do that. If your app depends on Google Play Services, it's advisable to do it as soon as possible during the lifecycle of your app, and Google recommends the onResume() method of your main activity.

Resources


Popular posts from this blog

What's the best way to handle the Android camera?

This article is adapted from a response I gave on Reddit to the question "What's the best way to handle the camera?" in r/androiddev...

The Camera and Camera2 APIs are far from painless to use. If you've ever written an app that uses the camera (embedded in an activity or not), you've almost certainly come up against orientation issues, stretched previews, or weird quirks that change from manufacturer to manufacturer...

There are three good libraries out there that can save you from many common pitfalls:
Google's (unofficial) CameraView library.Mark Murphy's CWAC-Cam2 library.Dylan McKintyre's CameraKit for Android library. Each has different strengths. If you don't have time to read this whole article, here's a quick rule of thumb:
If you want to capture photos in a full-screen preview, but you don't want to have to rely on the native camera app, then use CWAC-Cam2.If you want to embed a preview into your Activity, use CameraKit. It's f…

Manually Testing Web Services in Visual Studio

The first time you debug a web service after creating it, Visual Studio will launch the WCF Test Client - a very useful piece of kit - and preload it with the details of your service.

In this article, we'll look first at testing your WCF service, and then an all-too-familiar issue that might crop up and spoil your fun!

Using the WCF Test Client


The WCF Test Client looks a bit like SoapUI, and is pretty similar in purpose: It allows you to connect to a service, enter some parameters and submit them to any given service method to see what you get.

After the first time you run the WCF Client, Visual Studio will forget all about launching it again until you make significant changes to it - which means when you subsequently hit f5 or choose Debug, it won't run again. It's worth knowing how to bring it up...

You can find it again by launching the Visual Studio Command Prompt, and launching wcftestclient.exe from there.

Once connected, making use of it is as simple as filling in …