androidgradlecontinuous-integrationgitlab-cigradlew

Android GitLab CI. Reuse compiled files through stages


I am looking for a solution to optimize the pipeline execution by reusing compiled files from previous stages.

Let's narrow it down to 2 stages: compile - test. compile will share its build folder for test.

So far, I tried dependencies keyword. But the Gradle doesn't seem to be reusing the files whatever I do.

Proof of concept

The proof of concept is achievable locally, in Android Studio:

./gradlew clean
./gradlew compileReleaseKotlin compileReleaseJavaWithJavac
./gradlew --console=plain app: testReleaseUnitTest

I can see all the UP-TO-DATE logs next to compile, ksp, etc tasks when the test command is executed.

Attempt 1

But when pipeline runs with these, I can see that gradle is doing all the tasks again.

compile:
  stage: compile
  script:
    - ./gradlew --console=plain compileReleaseKotlin compileReleaseJavaWithJavac
  artifacts:
    paths:
      - app/build/
    expire_in: 1 days

tests_release:
  stage: tests
  dependencies:
    - compile
  script:
    - ./gradlew --console=plain app:testReleaseUnitTest 

Attempt 2

I event tried removing some commands manually like so

- ./gradlew --console=plain app:testReleaseUnitTest -x :app:compileGmsReleaseKotlin -x :app:kspGmsReleaseKotlin

But then it fails with

Execution failed for task ':app:processGmsReleaseJavaRes'.
> Querying the mapped value of provider(java.util.Set) before task ':app:compileGmsReleaseKotlin' has completed is not supported

Attempt 3

I also tried downloading the artifacts and putting them into my local files. And it seems to partially working. So maybe the problem is that I need to apply these files somehow?

TL;DR

How to reuse the compiled files (module/build folder) for subsequent CI stages in a single pipeline?


Solution

  • Just add the .gradle folder to the artifact

      ...
      artifacts:
        paths:
          - .gradle/
          - app/build/