How can I cache Android NDK in my Github Actions workflow?

Viewed 1793

I want to cache the Android NDK in my Github Actions workflow. The reason is that I require a specific version of the NDK and CMake which aren't pre-installed on MacOS runners.

I tried to use the following workflow job to achieve this:

jobs:
  build:
    runs-on: macos-latest
    steps:

    - name: Cache NDK
      id: cache-primes
      uses: actions/cache@v1
      with:
        path: ${{ env.ANDROID_NDK_HOME }}
        key: ${{ runner.os }}-ndk-${{ hashFiles(env.ANDROID_NDK_HOME) }}

    - name: Install NDK
      run: echo "y" | $ANDROID_HOME/tools/bin/sdkmanager "ndk;21.0.6113669" "cmake;3.10.2.4988404"

The problem with this is that the env context doesn't contain the ANDROID_NDK_HOME variable. So this means build.steps.with.path evaluates empty.

The regular environment variable is present and prints the correct path if I debug using the following step:

jobs:
  build:
    steps:
    - name: Debug print ANDROID_NDK_HOME
      run: echo $ANDROID_NDK_HOME

But the regular environment variable can only be used in shell scripts and not in build.steps.with as far as I understand.

2 Answers
  - name: Prepare NDK dir for caching ( workaround for https://github.com/actions/virtual-environments/issues/1337 )
    run: |
      sudo mkdir -p /usr/local/lib/android/sdk/ndk
      sudo chmod -R 777 /usr/local/lib/android/sdk/ndk
      sudo chown -R $USER:$USER /usr/local/lib/android/sdk/ndk
  - name: NDK Cache
    id: ndk-cache
    uses: actions/cache@v2
    with:
      path: /usr/local/lib/android/sdk/ndk
      key: ndk-cache-21.0.6113669-v2
  - name: Install NDK
    if: steps.ndk-cache.outputs.cache-hit != 'true'
    run: echo "y" | sudo /usr/local/lib/android/sdk/tools/bin/sdkmanager --install "ndk;21.0.6113669"

Here is a config I use in my project.

Couple of note:

You can easily specify the NDK installation directory to be cached.

- name: Cache (NDK)
  uses: actions/cache@v2
  with:
    path: ${ANDROID_HOME}/ndk/21.0.6113669
    key: ndk-cache


- name: Install NDK
  run: echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install 'ndk;21.0.6113669'
Related