How to sign APK in gitlab CI and send release to slack

Viewed 716

I want to build an android signed APK and receive release APK through the slack channel. Tried the below script but it's not working due to my app written with JDK 8.

This is the script which I used.

image: jangrewe/gitlab-ci-android

cache:
  key: ${CI_PROJECT_ID}
  paths:
    - .gradle/

before_script:
  - export GRADLE_USER_HOME=$(pwd)/.gradle
  - chmod +x ./gradlew

stages:
  - build

assembleDebug:
  stage: build
  only:
    - development
    - tags
  script:
    - ./gradlew assembleDebug
    - |
      curl \
        -F token="${SLACK_CHANNEL_ACCESS_TOKEN}" \
        -F channels="${SLACK_CHANNEL_ID}" \
        -F initial_comment="Hello team! Here is the latest APK" \
        -F "file=@$(find app/build/outputs/apk/debug -name 'MyApp*')" \
        https://slack.com/api/files.upload
  artifacts:
    paths:
      - app/build/outputs/apk/debug
view raw

But is showing some java classes not found. (That java files deprecated in Java 11)

1 Answers

First, you need to setup slack authentication keys.

  1. Create App in Slack
  2. Go to Authentication Section and Generate Authentication Key.
  3. Get Channel Id which you want to receive messages.
  4. Mention your app name in your slack thread and add the app to the channel.
  5. Setup those keys in your GitLab ci setting variables.

SLACK_CHANNEL_ACCESS_TOKEN = Access Token Generated by Slack App

SLACK_CHANNEL_ID = Channel Id (Check URL Last section for the channel id)

8.Copy your existing Keystore file to the repository. (Please do this if your project is private.) 7.Change GitLab script's content to the below code.

Make sure to change certificate password,key password and alias.

image: openjdk:8-jdk

variables:

  # ANDROID_COMPILE_SDK is the version of Android you're compiling with.
  # It should match compileSdkVersion.
  ANDROID_COMPILE_SDK: "29"

  # ANDROID_BUILD_TOOLS is the version of the Android build tools you are using.
  # It should match buildToolsVersion.
  ANDROID_BUILD_TOOLS: "29.0.3"

  # It's what version of the command line tools we're going to download from the official site.
  # Official Site-> https://developer.android.com/studio/index.html
  # There, look down below at the cli tools only, sdk tools package is of format:
  #        commandlinetools-os_type-ANDROID_SDK_TOOLS_latest.zip
  # when the script was last modified for latest compileSdkVersion, it was which is written down below
  ANDROID_SDK_TOOLS: "6514223"

# Packages installation before running script
before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1

  # Setup path as android_home for moving/exporting the downloaded sdk into it
  - export ANDROID_HOME="${PWD}/android-home"
  # Create a new directory at specified location
  - install -d $ANDROID_HOME
  # Here we are installing androidSDK tools from official source,
  # (the key thing here is the url from where you are downloading these sdk tool for command line, so please do note this url pattern there and here as well)
  # after that unzipping those tools and
  # then running a series of SDK manager commands to install necessary android SDK packages that'll allow the app to build
  - wget --output-document=$ANDROID_HOME/cmdline-tools.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
  # move to the archive at ANDROID_HOME
  - pushd $ANDROID_HOME
  - unzip -d cmdline-tools cmdline-tools.zip
  - popd
  - export PATH=$PATH:${ANDROID_HOME}/cmdline-tools/tools/bin/

  # Nothing fancy here, just checking sdkManager version
  - sdkmanager --version

  # use yes to accept all licenses
  - yes | sdkmanager --sdk_root=${ANDROID_HOME} --licenses || true
  - sdkmanager --sdk_root=${ANDROID_HOME} "platforms;android-${ANDROID_COMPILE_SDK}"
  - sdkmanager --sdk_root=${ANDROID_HOME} "platform-tools"
  - sdkmanager --sdk_root=${ANDROID_HOME} "build-tools;${ANDROID_BUILD_TOOLS}"

  # Not necessary, but just for surity
  - chmod +x ./gradlew

# Make Project
assembleDebug:
  interruptible: true
  stage: build
  only:
    - tags
  script:
    - ls
    - last_v=$(git describe --abbrev=0 2>/dev/null || echo '')
    - tag_message=$(git tag -l -n9 $last_v)
    - echo $last_v
    - echo $tag_message
    - ./gradlew assembleRelease
      -Pandroid.injected.signing.store.file=$(pwd)/Certificate.jks
      -Pandroid.injected.signing.store.password=123456
      -Pandroid.injected.signing.key.alias=key0
      -Pandroid.injected.signing.key.password=123456
    - |
      curl \
        -F token="${SLACK_CHANNEL_ACCESS_TOKEN}" \
        -F channels="${SLACK_CHANNEL_ID}" \
        -F initial_comment="$tag_message" \
        -F "file=@$(find app/build/outputs/apk/release -name 'app*')" \
        https://slack.com/api/files.upload

  artifacts:
    paths:
      - app/build/outputs/
Related