Android CI

直接使用脚本

编译 Android 的 CI 版本有两个版本,一个是官方推荐的版本:

功能比较完善:

  • build
  • Unit Test
  • UI Test

有几个问题:

See Android SDK Build-Tools 25.0.1 and Android SDK Platform 25 license problem (#1956) · Issues · GitLab.org / gitlab-ci-multi-runner · GitLab

最终可以的版本为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
image: openjdk:8-jdk
variables:
ANDROID_COMPILE_SDK: "25"
ANDROID_BUILD_TOOLS: "25.0.3"
ANDROID_SDK_TOOLS: "24.4.1"
before_script:
- apt-get --quiet update --yes
- apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
- wget --quiet --output-document=android-sdk.tgz https://dl.google.com/android/android-sdk_r${ANDROID_SDK_TOOLS}-linux.tgz
- tar --extract --gzip --file=android-sdk.tgz
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter android-${ANDROID_COMPILE_SDK}
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter platform-tools
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter build-tools-${ANDROID_BUILD_TOOLS}
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-android-m2repository
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-google-google_play_services
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-google-m2repository
- export ANDROID_HOME=$PWD/android-sdk-linux
- export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
- chmod +x ./gradlew
stages:
- build
- test
build:
stage: build
script:
- ./gradlew assembleDebug
artifacts:
paths:
- app/build/outputs/
unitTests:
stage: test
script:
- ./gradlew test

另外一个方案也是这边文章中提到的,使用 sdkmanager 来解决:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
image: openjdk:8-jdk
variables:
ANDROID_COMPILE_SDK: "25"
ANDROID_BUILD_TOOLS: "25.0.3"
ANDROID_SDK_TOOLS_REV: "3859397" # "26.0.1"
ANDROID_CMAKE_REV: "3.6.4111459"
GIT_SUBMODULE_STRATEGY: recursive # Remove if you don't have to clone submodules
before_script:
- mkdir $HOME/.android # For sdkmanager configs
- echo 'count=0' > $HOME/.android/repositories.cfg # Avoid warning
- wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS_REV}.zip
- mkdir $PWD/android-sdk-linux
- unzip -qq android-sdk.zip -d $PWD/android-sdk-linux
- export ANDROID_HOME=$PWD/android-sdk-linux
- export ANDROID_NDK_HOME=$ANDROID_HOME/ndk-bundle # Remove if you don't need NDK
- export PATH=$PATH:$ANDROID_HOME/platform-tools/:$ANDROID_NDK_HOME
- echo y | $ANDROID_HOME/tools/bin/sdkmanager --update
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'tools'
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'platform-tools'
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'build-tools;'$ANDROID_BUILD_TOOLS
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'platforms;android-'$ANDROID_COMPILE_SDK
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'extras;android;m2repository'
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'extras;google;google_play_services'
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'extras;google;m2repository'
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'cmake;'$ANDROID_CMAKE_REV
- echo y | $ANDROID_HOME/tools/bin/sdkmanager 'ndk-bundle' # Remove if you don't need NDK
- chmod +x ./gradlew
stages:
- build
build:
stage: build
script:
- ./gradlew assembleDebug --stacktrace
artifacts:
name: "${CI_PROJECT_NAME}_${CI_COMMIT_REF_NAME}_${CI_COMMIT_TAG}_${CI_COMMIT_SHA}"
paths:
- app/build/outputs/

使用 docker

另外有两个专门用于编译 android 的 docker: