Skip to content

tadashi0713/circleci-demo-android

Repository files navigation

CircleCI Demo Android

CircleCI

Demo for CI/CD pipeline for Android Native app using CircleCI.

Forked from Sunflower App.

You can see config file here.

  • Use Context for storing secrets(this time token for Firebase) for across projects.
  • Upload test results & visualize in Test Insights.

Test splitting and parallelism of Android UITests(Espresso) using Android Emulators on CircleCI

This demo includes UITests(Espresso), which need to be run on either Android devices or emulators.

I added sleep(Thread.sleep()) on purpose randomly to make each tests execution time sparsely.

class GardenActivity3Test {
    @Test fun clickAddPlant_OpensPlantList() {
        // When the "Add Plant" button is clicked
        onView(withId(R.id.add_plant)).perform(click())

        Thread.sleep(30000)

        // Then the ViewPager should change to the Plant List page
        onView(withId(R.id.plant_list)).check(matches(isDisplayed()))
    }
}

If you just want to run these Espresso tests on Android Emulators, you can easily create CircleCI pipeline using Android Orb.

integration_test:
  executor:
    name: android/android-machine
    resource-class: xlarge
    tag: 2022.09.1
  steps:
    - checkout
    - android/start-emulator-and-run-tests
    - store_test_results:
        path: ./app/build/outputs/androidTest-results/connected

android/start-emulator-and-run-tests includes following steps:

  • Create & launch Android Emulator
  • Restore Gradle cache
  • Build for testing(./gradlew assembleDebugAndroidTest)
  • Wait for Android Emulator to start
  • Run tests(./gradlew connectedDebugAndroidTest)

However, since UITests takes time, you want to split these tests and run them in multiple Android emulators.

To run these tests in parallel using CircleCI, follow these steps:

  • Pre-build (assembleAndroidTest) to run Espresso tests
  • Launch multiple Linux VMs/Android emulators
  • Split tests based on execution time
  • Run split tests in parallel
  • Upload test results which include execution time

Pre-build (assembleAndroidTest)

All Android application tests, including UI tests, must be built before running.

build_for_integration_test job pre-build using ./gradlew assembleDebugAndroidTest command in order to run the tests in parallel on multiple Android emulators later on.

build_for_integration_test:
  executor:
    name: android/android-machine
    resource-class: xlarge
    tag: 2022.09.1
  steps:
    - checkout
    - android/restore-gradle-cache
    - run: ./gradlew assembleDebugAndroidTest
    - android/save-gradle-cache
    - persist_to_workspace:
        root: ~/
        paths: .

Test splitting and parallelism of UITests(Espresso)

integration_test_parallel splits UITests(Espresso) and runs in parallel.

integration_test_parallel:
  parallelism: 6
  executor:
    name: android/android-machine
    resource-class: xlarge
    tag: 2022.09.1
  steps:
    - checkout
    - attach_workspace:
        at: ~/
    - run:
        name: Split Espresso tests
        command: |
          cd app/src/androidTest/java
          CLASSNAMES=$(circleci tests glob "**/*Test.kt" \
            | sed 's@/@.@g' \
            | sed 's/.kt//' \
            | circleci tests split --split-by=timings --timings-type=classname)
          echo "export GRADLE_ARGS='-Pandroid.testInstrumentationRunnerArguments.class=$(echo $CLASSNAMES | sed -z "s/\n//g; s/ /,/g")'" >> $BASH_ENV
    - android/create-avd:
        avd-name: test
        install: true
        system-image: "system-images;android-29;default;x86"
    - android/start-emulator:
        avd-name: test
        post-emulator-launch-assemble-command: ""
    - run:
        name: Run Espresso tests
        command: ./gradlew connectedDebugAndroidTest $GRADLE_ARGS
    - store_test_results:
        path: ./app/build/outputs/androidTest-results/connected

If you want to run specific UITests(Espresso), you need to add following Gradle args.

./gradlew connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.google.samples.apps.sunflower.GardenActivity1Test,com.google.samples.apps.sunflower.GardenActivity2Test

Therefore, below script glob test files, process to classname, and process to Gradle args.

cd app/src/androidTest/java
CLASSNAMES=$(circleci tests glob "**/*Test.kt" \
  | sed 's@/@.@g' \
  | sed 's/.kt//' \
  | circleci tests split --split-by=timings --timings-type=classname)
echo "export GRADLE_ARGS='-Pandroid.testInstrumentationRunnerArguments.class=$(echo $CLASSNAMES | sed -z "s/\n//g; s/ /,/g")'" >> $BASH_ENV

After launch Android emulators, we will add this Gradle args.

post-emulator-launch-assemble-command can be blank since we already pre-build in previous job.

Result

Getting Started

This project uses the Gradle build system. To build this project, use the gradlew build command or use "Import Project" in Android Studio.

There are two Gradle tasks for testing the project:

  • connectedAndroidTest - for running Espresso on a connected device
  • test - for running unit tests

Unsplash API key

Sunflower uses the Unsplash API to load pictures on the gallery screen. To use the API, you will need to obtain a free developer API key. See the Unsplash API Documentation for instructions.

Once you have the key, add this line to the gradle.properties file, either in your user home directory (usually ~/.gradle/gradle.properties on Linux and Mac) or in the project's root folder:

unsplash_access_key=<your Unsplash access key>

The app is still usable without an API key, though you won't be able to navigate to the gallery screen.

Screenshots

List of plants Plant details My Garden

Libraries Used

  • Foundation - Components for core system capabilities, Kotlin extensions and support for multidex and automated testing.
    • AppCompat - Degrade gracefully on older versions of Android.
    • Android KTX - Write more concise, idiomatic Kotlin code.
    • Test - An Android testing framework for unit and runtime UI tests.
  • Architecture - A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence.
    • Data Binding - Declaratively bind observable data to UI elements.
    • Lifecycles - Create a UI that automatically responds to lifecycle events.
    • LiveData - Build data objects that notify views when the underlying database changes.
    • Navigation - Handle everything needed for in-app navigation.
    • Room - Access your app's SQLite database with in-app objects and compile-time checks.
    • ViewModel - Store UI-related data that isn't destroyed on app rotations. Easily schedule asynchronous tasks for optimal execution.
    • WorkManager - Manage your Android background jobs.
  • UI - Details on why and how to use UI Components in your apps - together or separate
  • Third party and miscellaneous libraries

About

Demo for CI/CD pipeline for Android Native app using CircleCI.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •