Importing google-play-services lib into Intellij IDEA 12 (and 13)

For historical reasons, I'm going to reproduce what I did, taken from here. Many thanks to @Hesam who found it. I will vote to close the whole question.

Note: the shortcuts are for OS X and Intellij 12 and 13.

Make sure you have the latest SDK/ADT Tools and the play services. They change every now and then and are usually not backwards compatible.

UPDATE I had to do this again, so this is the polished version of what I did.

You need to have a copy (as opposed to using it directly from the SDK folder), because if Google updates it, things are going to break when you update your SDK, it's safer to keep a copy (albeit more tedious because you will have to manually update when a new version comes out). I found out that it's better to decide when to update than to be surprised with cryptic compilation errors.

With that in mind, copy the library alongside your project. You should have a folder structure that looks like:

/something/yourmainmodule/

/something/any_other_module/

/something/modules.xml

/something/README.md (if you use github) :)

The library is located in

"ANDROID-SDK"/extras/google/google_play_services/libproject/google-play-services_lib

ANDROID-SDK is wherever you stored the SDK when you downloaded it. Google changes this every now and then, so just find it, it will be there.

Copy that entire folder so your project now looks like this:

/something/yourmainmodule/

/something/any_other_module/

/something/google-play-services_lib/ <-- THIS IS THE NEW ONE

/something/modules.xml

/something/README.md

Good, now let's import it to IntelliJ-IDEA the right way:

1) Select your project/main module from project panel (⌘ 1) and hit ⌘ ↓ in order to open project structure window or just do File -> Project Structure.

2) Select Modules from left panel, then hit + button (or ⌘ N) then select "import module" and navigate to the google-play-services_lib that you just copied next to your project's module folder. (/something/).

The wizard will ask a series of questions, just hit next, defaults are fine, but read what's going on, understand what's happening, this is a core concept in Java, Android and Intellij. Experiment if you don't know what each dialog is saying (or why some may be empty).

After you're done, the google play Project will be added to Modules alongside with your project.

Don't close this window (or open it again if you did).

3) Still in the modules, select your project/module, then select "Dependencies" from right panel (it's a tab) and finally Click + button at the bottom (or ⌘ N which will work if you have the focus in this tab), then from the popup chose "3 Module dependencies..." and select "google-play-services_lib", which you added in the previous step. It should be there. If it's not, you didn't add the module in step 2.

4) Finally, Click + button again (or ⌘ N) but this time select "1 jars or directories...", navigate to the "/libs" folder of the above path and select "google-play-services.jar".

5) Click ok and close project structure window.

If you did this, your main module should have a dependency that looks like this:

Dependencies

Also make sure that in the Project Structure window, FACETS Tab, the Google Play Services has the "Library Module" checked. It is a library module.

Note: Make sure you have not added "google-play-services_lib" project as library in "Libraries" (left panel under Modules). If you have, you can go to "Libraries and simply remove it. Google Play Library Should Not Appear In Libraries.

Credit: this text comes from this StackOverflow answer