androidgradlecontinuous-integrationgithub-actionsandroid-safe-args

GitHub Actions with Android throws Compatible with Java 17 and the consumer needed a component, compatible with Java 11


I can run lintDebug or the test cases in my local Android studio terminal, builds are successful. I am currently learning CI/CD and trying to run Github Actions to do the job when I push my code. But when pushing, CI fails on the first job with below exception:

> Could not resolve all files for configuration ':classpath'.
   > Could not resolve androidx.navigation:navigation-safe-args-gradle-plugin:2.7.7.
     Required by:
         project : > androidx.navigation.safeargs:androidx.navigation.safeargs.gradle.plugin:2.7.7
      > No matching variant of androidx.navigation:navigation-safe-args-gradle-plugin:2.7.7 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.4' but:
          - Variant 'apiElements' capability androidx.navigation:navigation-safe-args-gradle-plugin:2.7.7 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.4')
          - Variant 'runtimeElements' capability androidx.navigation:navigation-safe-args-gradle-plugin:2.7.7 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.4')
          - Variant 'sourcesElements' capability androidx.navigation:navigation-safe-args-gradle-plugin:2.7.7 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.4')

As I understand from the error, GitHub runner uses Java 11 and my code uses Java 17. So in my CI, I've added java-version and distribution under each uses keyword to solve it. I've also tried to run with self hosted, did not work either. Here is my beginning part of the CI code that generates error:

name: CI

on:
  push:
    branches: [master]

  pull_request:
    branches: [master]

jobs:
  start:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the code
        uses: actions/checkout@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false
  
      - name: Run sample script
        run: echo Hello, world

  lint:
    name: Perform lint check
    needs: [start]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the code
        uses: actions/checkout@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false

      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
          restore-keys: ${{ runner.os }}-gradle-

      - name: Make Gradle executable
        run: chmod +x ./gradlew
  
      - name: Run lint
        run: ./gradlew lintDebug
  
      - name: Upload html test report
        uses: actions/upload-artifact@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false
          name: lint.html
          path: app/build/reports/lint-results-debug.html

  unit-test:
    name: Perform Unit Testing
    needs: [lint]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the code
        uses: actions/checkout@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false
  
      - name: Run tests
        run: ./gradlew test
  
      - name: Upload test report
        uses: actions/upload-artifact@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false
          name: unit_test_report
          path: app/build/reports/test/testDebugUnitTest/

As I search similar issues, suggestions were putting the related plugin id("androidx.navigation.safeargs.kotlin") to the end of plugin sections, and check compileOptions to be JavaVersion.VERSION_17 in build.gradle. Done those but still can not resolve the error. Any help or ideas will be appreciated. Thanks in advance.


Solution

  • After some time I've found that the error does not occur from SafeArgs. Because when I completely remove safeargs from my project and used Bundle, this time the next plugin that I declared in build.gradle, below the safears com.android.application throws the same error.

    I thought that I'm running my CI using JavaVersion.VERSION_17 with the below code apparently does not override github runner's java version:

    with:
      distribution: 'temurin'
      java-version: '17'
    

    In the docs I've found that I can install my own java version to Github runner, so in every job I've added the lines with run. With this I've created same environment like my local ubuntu machine, then it worked like a charm in there too. Code below is just part of my CI, but will enough for you to see how to change Github's default runner machine. Hope that helps.

    lint:
      name: Perform lint check
      needs: [start]
      runs-on: ubuntu-latest
      steps:
        - name: Checkout the code
          uses: actions/checkout@v4
          with:
            fetch-depth: 0
    
        - name: Cache Gradle
          uses: actions/cache@v4
          with:
            path: ~/.gradle/caches
            key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
            restore-keys: ${{ runner.os }}-gradle-
    
        - run: |
            download_url="https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz"
              wget -O $RUNNER_TEMP/java_package.tar.gz $download_url
        - uses: actions/setup-java@v4
          with:
            distribution: 'jdkfile'
            jdkFile: ${{ runner.temp }}/java_package.tar.gz
            java-version: '17'
            architecture: x64
    
        - name: Make Gradle executable
          run: chmod +x ./gradlew
    
        - name: Run lint
          run: ./gradlew lintDebug
    
        - name: Upload html test report
          uses: actions/upload-artifact@v4
          with:
            name: lint.html
            path: app/build/reports/lint-results-debug.html