1

I am trying to import a custom tool into the pipeline Jenkinsfile script (specifically, the Android SDK). From the build console, it says that I need to set the ANDROID_HOME environment variable to build my Android app. However, when I tried adding the SDK download and set it to the ANDROID_HOME variable, it's telling me that the directory does not exist:

Caused by: java.lang.RuntimeException: The SDK directory '/var/jenkins_home/tools/com.cloudbees.jenkins.plugins.customtools.CustomTool/Android_SDK_CLI_tools_4333796/bin' does not exist.

In the global tools configuration page, here's how I added the Android SDK:

Name: Android_SDK_CLI_tools_4333796

Tool home: bin

Command: (below)

if [ ! -f "Android_SDK_tools_4333796/" ]; then
    echo "curl"
    curl -O https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
    echo "unzip"
    yes | unzip -q sdk-tools-linux-4333796.zip
    rm sdk-tools-linux-4333796.zip 
    echo "licenses"
    yes | tools/bin/sdkmanager --licenses
fi

Below is how I set my Jenkinsfile:

pipeline {
    agent any
    environment {
        ANDROID_HOME = tool 'Android_SDK_CLI_tools_4333796'
        //ANDROID_HOME = tool name: 'Android_SDK_CLI_tools_4333796', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool' //alternative but still has the same results. 
        //env.PATH = "${env.PATH}:${androidSDKHome}"
    }
    tools {
        jdk 'JDK_9.0.4'
    }

    stages {
        stage('Pre-build') {
            steps {
                // seeing if Java JDK installed correctly
                sh 'java --version'

                print (env.ANDROID_HOME) // prints: /var/jenkins_home/tools/com.cloudbees.jenkins.plugins.customtools.CustomTool/Android_SDK_CLI_tools_4333796/bin

                
            }
        }
        stage('Test') {
            steps {
                echo 'Testing stage...'
                sh './gradlew clean --stacktrace' //ERROR (above)
            }
        }
    }
}

My question is, am I pulling in this custom tool into the build properly? It seems Jenkins can't find it the custom tool even though I trying to access the absolute path.

I tried referencing this SO question; However, this is for me to obtain the executable whereas I'm looking to set the env var for Gradle to pick up the ANDROID_HOME env var automatically.

Community
  • 1
  • 1
rj2700
  • 1,770
  • 6
  • 28
  • 55

1 Answers1

0

Ok, after struggling with this for the past few days, I realized many new things, the most important was how the dir structure has to be for commands to work.

The folder structure should be in this layout:

/<unpacked_location>
  /tools
    /bin
    ...
  /build-tools
  /licenses
  /platforms

From there, everything should fall like dominos. My problem was, when Jenkins unpacks the Android SDK cli tools download, I was setting the home to bin. That's wrong since when you unpack the Android SDK, /bin is located inside the /tools dir. Jenkins kinda confused me by not allowing me to leave the home dir blank (makes sense since you can also set absolute paths if needed), so I set it to the present working dir (. in this case).

The other major thing that I was missing was installing the latest Android SDK version. I just added this to the global tool config script for Android SDK:

yes | tools/bin/sdkmanager "build-tools;28.0.3"
rj2700
  • 1,770
  • 6
  • 28
  • 55