Android 24 with Google APIs

android
how-to

#1

With the recent update to Android SDK and the Gradle plugin, we have been seeing an increase in test timeouts. Part of this appears due to our older emulator AVD images not having Google Play Services APIs installed.

While we work to get these updated, here are instructions on how to create a newer AVD with the Google Play Services APIs installed:

dependencies:
  pre:
    - echo y | android update sdk --no-ui --all --filter tools
    - echo y | android update sdk --no-ui --all --filter platform-tools
    - echo y | android update sdk --no-ui --all --filter extra-android-m2repository
    - echo y | android update sdk --no-ui --all --filter extra-android-support
    - echo y | android update sdk --no-ui --all --filter extra-google-google_play_services
    - echo y | android update sdk --no-ui --all --filter extra-google-m2repository
    - echo y | android update sdk --no-ui --all --filter android-24
    - echo y | android update sdk --no-ui --all --filter build-tools-24.0.1
    - echo y | android update sdk -a -u -t sys-img-armeabi-v7a-google_apis-24
test:
  override:
    - echo no | android create avd -n custom-android24-googleapis -t "android-24" --abi google_apis/armeabi-v7a
    - android list avd
    - emulator -avd custom-android24-googleapis -no-window:
        background: true
        parallel: true
    # wait for emulator to have booted
    - circle-android wait-for-boot

Couple of things to note: -no-audio has been removed as it is not a valid flag under the ARM image and will cause the emulator to not load. Also, the tools and platform-tools tools will fetch the latest versions, so if you want to use a certain version you will need to change those. You can run android list sdk --all --extended to get a full list of everything that is available.

You will also want to make sure you are running the latest Gradle plugin, otherwise your tests may fail stating they can find no compatible emulator image.

Thanks to How do you create android emulator with google apis? for the instructions on how to fetch the ARM image and create the AVD.

One final troubleshooting tip: any command run with the background: true modifier writes it’s stderr file to the artifacts, so even if you cancel a build you can check there to see if the emulator threw any errors.


#2

I’m trying to use a very similar version to this one, but it’s been 30 minutes since I’ve started the circle-android wait-for-boot and it doesn’t seem to be going much places from there.

Any way, my circle.yml is:

machine:
  environment:
    PATH: "~/$CIRCLE_PROJECT_REPONAME/gradle-2.9/bin:$PATH"
    TERM: "dumb"
    ADB_INSTALL_TIMEOUT: "10"
    GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError"'


dependencies:
  pre:
    - wget "https://services.gradle.org/distributions/gradle-2.9-bin.zip"; unzip gradle-2.9-bin.zip
    - echo y | android update sdk --no-ui --all --filter tools
    - echo y | android update sdk --no-ui --all --filter platform-tools
    - echo y | android update sdk --no-ui --all --filter extra-android-m2repository
    - echo y | android update sdk --no-ui --all --filter extra-android-support
    - echo y | android update sdk --no-ui --all --filter extra-google-google_play_services
    - echo y | android update sdk --no-ui --all --filter extra-google-m2repository
    - echo y | android update sdk --no-ui --all --filter android-24
    - echo y | android update sdk --no-ui --all --filter build-tools-24.0.1
    - echo y | android update sdk -a -u -t sys-img-armeabi-v7a-google_apis-24
    - android list targets

test:
  override:
    - echo no | android create avd -n custom-android24-googleapis -t "android-24" --abi google_apis/armeabi-v7a
    - android list avd
    - emulator -avd custom-android24-googleapis -no-window -no-audio:
        background: true
        parallel: true
    # wait for emulator to have booted
    - circle-android wait-for-boot
    # unlock the emulator screen
    - sleep 30
    - adb shell input keyevent 82
    # run tests  against the emulator.
    - ./gradlew connectedAndroidTest -PdisablePreDex
    # copy the build outputs to artifacts
    - cp -r app/build/outputs $CIRCLE_ARTIFACTS
    # copy the test results to the test results directory.
    - cp -r app/build/outputs/androidTest-results/* $CIRCLE_TEST_REPORTS

And I also have added com.android.ddmlib.DdmPreferences.setTimeOut(60000)

Thanks a lot!


#3

We have recently found that there is an issue with the new version of Android 24 emulator where it refuses to boot on CircleCI, please check this post for a workaround. Thanks!


#4