I figured out how to get Google Play Service and all the basic Google apps onto a custom compiled AOSP image. It's kind of tricky, so I'll outline what I learned here. I specifically got it working on a Nexus 5 device using a modified version of Android 5.0.2, but these steps should hopefully work for all Nexus devices and most Android version. This tutorial is broken up into 4 parts:
Part 1: Compiling a Custom Image
First, make sure you've downloaded the vendor drivers for your device: link
Once you've downloaded the appropriate files, unzip them. You should now have a bunch of script files. Place these script files in the root directory of your AOSP repository and run them.
Next, make sure you've loaded your sources and you're in the correct lunch for your device (for Nexus 5, this is hammerhead aka 20):
source build/envsetup.sh
lunch 20
If this is your first time doing a make with the vendor drivers, you need to clobber to make sure the drivers are compiled into the ROM:
make clobber
After that, make your ROM:
make
Part 2: Flashing Custom Image to a Nexus Device
First, reboot your device into fastboot. The hardware button sequence for your device can be found here: link
Once in fastboot, ensure that your bootloader is unlocked. You can lookup how to do this online.
After that, check that your computer can detect your device. You should see it when you run the following command:
fastboot devices
If you can't see it, check your USB drivers and make sure you have Google's Nexus USB drivers if you're using a Nexus device.
Finally, flash the device:
fastboot -w flashall
Part 3: Flashing Recovery
At this point, you've compiled a custom image and flashed it to your device. The next goal is to install the standard Google apps (GPS, Google Play, Gmail, etc.). Unfortunately, the default recovery doesn't work well with rooted devices. So before we can do that, we need to install a 3rd-party recovery.
First, reboot the device into fastboot. This can be done via adb:
adb reboot bootloader
Once in fastboot, we can flash the recovery partition with our custom recovery. I recommend twrp which can be found here: link
For Nexus 5, this version of twrp will work: link
fastboot flash recovery twrp-2.8.7.1-hammerhead.img
After that, reboot the device:
fastboot reboot
Part 4: Install Gapps
First, push a gapps archive onto your sd card. This can be downloaded from cyanogen's website: link
The website only shows which gapps corresponds to which cyanogenmod version and not the equivalent AOSP version. From my experiences, I believe this much to be true:
- Android 5.1.0 <=> CM 12.1
- Android 5.0.0, 5.0.1, 5.0.2 <=> CM 12
If you are using a different version of AOSP, you'll have to experiment and find the right version on your own. Note, you can always do a factory reset from recovery to remove gapps and then install another version.
Once you've downloaded a version of gapps, push it to the sd card:
adb push gapps.zip /sdcard/
Once the zip is written to the sd card, reboot into recovery:
adb reboot recovery
Once in the Recovery, select install from zip and select your zip. After the installation is complete, select the wipe button at the bottom of the screen and then reboot the device. If everything worked correctly, you should be prompted with the Welcome screen which will ask you to configure your device. If you do not get the Welcome screen, then you either didn't install the correct version of gapps or you forgot to wipe something.