colymore
colymore

Reputation: 12316

CircleCI for android project recompiles everything for unit test

I have this circleci config.

    version: 2.1

orbs:
  android: circleci/[email protected]
  aws-cli: circleci/[email protected]

references:
  cache_key: &cache_key
    key: cache-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
  
  restore_cache: &restore_cache
    restore_cache:
      <<: *cache_key

  save_cache: &save_cache
    save_cache:
      <<: *cache_key
      paths:
        - ~/.gradle
        - ~/.m2
        - "/opt/android-sdk-linux/licenses/"

  attach_debug_workspace: &attach_debug_workspace
    attach_workspace:
      at: workspace

  persist_debug_workspace: &persist_debug_workspace
    persist_to_workspace:
      root: .
      paths:
        - app/build/intermediates
        - app/build/outputs/androidTest-results
        - app/build/outputs/apk
        - app/build/outputs/code-coverage
        - app/build/test-results

  persist_release_workspace: &persist_release_workspace
    persist_to_workspace:
      root: workspace
      paths:
        - app/build

jobs:

  build_debug:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - run:
          name: Download dependencies
          command: ./gradlew androidDependencies
      - *save_cache
      - run:
          name: Gradle build (debug)
          command: ./gradlew :app:assembleDebug :app:assembleAndroidTest --no-daemon --stacktrace
      - *persist_debug_workspace
      - store_artifacts:
          path: app/build/outputs/apk/
          destination: /apk/
  build_release:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - run:
          name: Download dependencies
          command: ./gradlew androidDependencies
      - *save_cache
      - run:
          name: Gradle build (release)
          command: ./gradlew :app:assembleRelease --no-daemon --stacktrace
      - *persist_release_workspace
      - store_artifacts:
          path: app/build/outputs/apk/
          destination: /apk/
  test_unit:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - run:
          name: Run unit tests
          command: ./gradlew test --no-daemon
      - *persist_debug_workspace
      - store_artifacts:
          path: app/build/reports/
          destination: /reports/
      - store_test_results:
          path: app/build/test-results/
          destination: /test-results/
  test_instrumented:
    parameters:
      system-image:
        type: string
        default: system-images;android-29;default;x86
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - android/start-emulator-and-run-tests:
          test-command: ./gradlew connectedFlavorADebugAndroidTest --no-daemon
          system-image: << parameters.system-image >>
      - run:
          name: Save test results
          command: |
            mkdir -p ~/test-results/junit/
            find . -type f -regex ".*/build/outputs/androidTest-results/.*xml" -exec cp {} ~/test-results/junit/ \;
          when: always
      - *persist_debug_workspace
      - store_artifacts:
          path: app/build/reports/
          destination: /reports/
      - store_test_results:
          path: app/build/outputs/androidTest-results/connected/
          destination: /test-results/
  report_coverage:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - run:
          name: Generate JaCoCo report
          command: ./gradlew jacocoAggregatedReport sonarqube --info
      - store_artifacts:
          path: app/build/reports/
          destination: /reports/
  upload_artifacts:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - run:
          name: Upload to artifactory
          command: |
            ./jfrog upload

workflows:
    version: 2
    test-and-coverage:
      jobs:
        - build_debug:
            context:
              - CodeArtifact
        - test_unit:
            requires:
              - build_debug
        - test_instrumented:
            requires:
              - build_debug
        - report_coverage:
            requires:
              - test_unit
              - test_instrumented
            context:
              - SonarCloud
    conditional-workflow:
      when:
        or:
          - equal: [ develop, << pipeline.git.branch >> ]
          - equal: [ master, << pipeline.git.branch >> ]
      jobs:
        - build_debug:
        - build_release:
        - test_unit:
            requires:
              - build_debug
        - test_instrumented:
            requires:
              - build_debug
        - report_coverage:
            requires:
              - test_unit
              - test_instrumented
            context:
              - SonarCloud
        - upload_artifacts:
           requires:
            - build_release

The expected is, first job is build, so gradle builds everything and the next jobs (Unit tests and IT tests) does not need to compile again because it was compiled before, but gradle is compiling everything in all jobs.

What did I make wrong? Thanks

Upvotes: 0

Views: 157

Answers (1)

FelicianoTech
FelicianoTech

Reputation: 4017

Jobs are unique environments. The package built in the first job, anything really done in the first job, does not exist in the second job.

You either need to combine everything into one job, or use a CircleCI feature called workspaces to copy certain files from one job into other subsequent jobs.

Upvotes: -1

Related Questions