Skip to content

li2/hello_fastlane

Repository files navigation

Android CI

Hello Fastlane

Running Android tests using fastlane

Add a test lane in Fastfile:

  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
    gradle(task: "connectedAndroidTest")
  end

To use the newly created test lane, just run

$ fastlane test

Then you will see the result printed:

+------+----------------------+-------------+
|             fastlane summary              |
+------+----------------------+-------------+
| Step | Action               | Time (in s) |
+------+----------------------+-------------+
| 1    | default_platform     | 0           |
| 2    | test                 | 3           |
| 3    | connectedAndroidTest | 83          |
+------+----------------------+-------------+

Further read:

  • test: Run unit tests for all variants.
  • connectedAndroidTest: Installs and runs instrumentation tests for all flavors on connected devices.

you can run ./gradlew tasks to get a list of tasks with all build types and flavors. Refer to Running unit tests with Gradle

Add androidTestVariantName folders where you can add test cases specific to each one of your flavors to fix androidTest failure:

useAppContext 
org.junit.ComparisonFailure: expected:<...droid.hello_fastlane[]> but was:<...droid.hello_fastlane[.dev]>

Jenkins

Install Jenkins

$ brew update && brew install jenkins
$ jenkins
    http://localhost:8080/

Config Jenkins for Android

Set environment variables of Android and Java: Dashboard -> Manage Jenkins -> Configure System -> Global properties -> Environment variables

ANDROID_HOME=/Users/your_name/Library/Android/sdk
JAVA_HOME=/usr/libexec/java_home
if encounter error: JAVA_HOME is set to an invalid directory: /usr/libexec/java_home
then try this:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home

Create a new job to build Android project: Dashboard -> New Item, enter project name and select Freestyle Project style. click on OK button to continue. In the next page, click on Source Code Management tab to set up code repository.

Then setup Android build variants in Jenkins. see commit #1336a7bb for detail

APK renaming

${applicationId}-v${versionName}(${versionCode}-${productFlavor}-${buildType}-${gitsha})

Run Android unit tests and generate reports on jenkins build page

Install JUnit Plugin then configure Job to Add post-build action ▼:

Test report XMLs report test-results/*.xml Available on Jenkins build page if failed.
Health report amplification factor 1.0 1% failing tests scores as 99% health. 5% failing tests scores as 95% health
Allow empty results True There is no test reports if all state (test, build, sign, etc) succeed, check this option can avoid Jenkins shows failure status.

Then update build.sh to add test task and report test results task. Then you can check the test results for failure details which is much more friendly than Console Output.

Refer

Beta Release to Firebase App Distribution

Make sure you have already setup your Firebase project, added your app to firebase project, onboard your app by pressing the "Get started" button on the App Distribution page.

Then you can follow the doc to setup Firebase CLI, refer:

Add a UAT beta release lane in Fastfile:

  desc "Submit a new Beta Build to Firebase App Distribution"
  lane :uat do
    gradle(
      task: "assemble",
      flavor: "Uat",
      build_type: "Release",
      print_command: false,
      properties: {
        "versionCode" => 1,
        "versionName" => "1.0.0",
        "android.injected.signing.store.file" => "keystore.jks",
        "android.injected.signing.store.password" => ENV["STORE_PASSWORD"],
        "android.injected.signing.key.alias" => ENV["KEY_ALIAS"],
        "android.injected.signing.key.password" => ENV["KEY_PASSWORD"],
      }
    )
    firebase_app_distribution(
        app: "1:123456789:android:abcd1234",
        testers: "[email protected], [email protected]",
        release_notes: "Lots of amazing new features to test out!",
    )
  end

To avoid having private key info hardcoded in Fastfile, you can use environment variables as showed above, refer

To use the newly created beta release lane, just run

$ fastlane uat

which will generate APK, sign it, upload to Firebase App Distribution:

+------+---------------------------+-------------+
|                fastlane summary                |
+------+---------------------------+-------------+
| Step | Action                    | Time (in s) |
+------+---------------------------+-------------+
| 1    | default_platform          | 0           |
| 2    | assembleuatrelease        | 3           |
| 3    | firebase_app_distribution | 16          |
+------+---------------------------+-------------+

[20:09:18]: fastlane.tools finished successfully 🎉

Deploy to Google Play using fastlane

Preparation

  • Creata an Application and upload a bundle build manually to Play Console to avoid error "No application was found for the given package name" in the following supply setup steps.
  • Setting up supply
    • create a Google Service Account in Google Cloud Console;
    • grant application access for the newly added service account in Play Console;
    • download service account credentials json file from Google Cloud Console;
    • test the connection to Play Console with the json file;
    • init supply and download your App metadata from Play;

TODO (stuck on this issue 2020-06-13 22:53:42)

Google Api Error: Invalid request - Access Not Configured. Google Play Developer API has not been used in project xxx before or it is disabled. fastlane issue #16593

GitHub Actions

GitHub Actions introduces workflows that can have multiple jobs and such workflows can be triggered by an event (commit, pull request, etc.) or can be scheduled.

Added 3 workflows:

Development workflow

workflow will be triggered on every new code commits on feature branches. jobs include:

  • junit tests.
  • todo: code linters (which check style formatting), security checks, code coverage, instrumented unit tests, etc.

UAT release workflow

workflow will be triggered on pushing commits or pull request to master (main trunk), jobs include:

  • junit test
  • build and sign APK, upload APK to GitHub artifacts, deploy to Firebase App Distribution.

Production release workflow

workflow will be triggered on Github tags created, jobs include:

  • junit test
  • automatically bump version code (simply +1) and version name (from tag).
  • build and sign App bundle file .aab,
  • upload the signed artifact to GitHub artifacts,
  • deploy the signed artifact to PlayStore.

Refer