我使用新的Android命令行工具,因为旧的Android sdk-tools库已经不可用了。所以我改变了我的gitlab-ci来加载commandlintools。但是当我尝试运行它时,我得到以下错误:

Warning: Could not create settings
java.lang.IllegalArgumentException
    at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.<init>(SdkManagerCliSettings.java:428)
    at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:152)
    at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:134)
    at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:57)
    at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)

我已经尝试手动执行这些命令,但我得到了相同的错误。同样,如果我运行sdkmanager——version,也会出现同样的错误。 我的gitlab-ci长这样:

image: openjdk:9-jdk

variables:
  ANDROID_COMPILE_SDK: "29"
  ANDROID_BUILD_TOOLS: "29.0.3"
  ANDROID_SDK_TOOLS:   "6200805"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
  - unzip -d android-sdk-linux android-sdk.zip
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  #- echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
  - set -o pipefail

stages:
  - build
  - test

lintDebug:
  stage: build
  script:
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint

assembleDebug:
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

debugTests:
  stage: test
  script:
    - ./gradlew -Pci --console=plain :app:testDebug

当前回答

我们不再为每个命令执行传递参数——sdk_root,而是深入研究真正的原因。

Starting from Android SDK Command-line Tools 1.0.0 (6200805), in contrast to Android SDK 26.1.1 (4333796), the tools directory hierarchy has been changed. Previously it was placed right inside ANDROID_HOME (which is deprecated, we will use the term ANDROID_SDK_ROOT for the rest of the paragraph), now it's still named as tools (the only thing you'll get after unpacking the downloaded commandlinetools zip file), but differently, you have to place it inside a directory called cmdline-tools on your own. The name cmdline-tools comes from its package name, where you can get from listing packages command sdkmanager --list, whose outputs include cmdline-tools;1.0 | 1.0 | Android SDK Command-line Tools.

将tools目录包装到cmdline-tools目录中将使其工作,并帮助您摆脱恼人的——sdk_root参数。但是其他部分呢?

这就是你要改变的。让我解释更多。

The king - sdkmanager lives inside cmdline-tools/tools/bin, you'd better set in PATH environment variable cmdline-tools should not be set as ANDROID_SDK_ROOT. Because later, when updating Android SDK, or installing more packages, the other packages will be placed under ANDROID_SDK_ROOT, but not under cmdline-tools. The final, complete ANDROID_SDK_ROOT directory structure should look like below, consist of quite a few sub-directories: build-tools, cmdline-tools, emulator, licenses, patcher, platform-tools, platforms, system-images. You can easily point out that build-tools and cmdline-tools are siblings, all sit inside the parent ANDROID_SDK_ROOT.

让我简单概括一下:

设置您首选的ANDROID_SDK_ROOT(就像以前一样) 下载并解压commandlinetools zip文件到名为cmdline-tools的目录中,该目录位于ANDROID_SDK_ROOT目录中 将目录$ANDROID_SDK_ROOT/cmdline-tools/tools/bin追加到环境变量PATH后,这样系统就知道在哪里可以找到sdkmanager

更新! ! ! !

自构建6858069 (Android SDK命令行工具3.0)以来,行为再次发生了变化:

After unzipping the package, the top-most directory you'll get is cmdline-tools. Rename the unpacked directory from cmdline-tools to tools, and place it under $ANDROID_SDK_ROOT/cmdline-tools, so now it should look like: $ANDROID_SDK_ROOT/cmdline-tools/tools. And inside it, you should have: NOTICE.txt bin lib source.properties. Actually according to the official Command-Line Tools doc, the tree structure should be android_sdk/cmdline-tools/version/bin/, but I've checked, using version or tools makes no difference here. For your environment variable PATH, I would recommend you to set like this: PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin, because after update later, you'll get the latest sdkmanager placed under $ANDROID_SDK_ROOT/cmdline-tools/latest/bin, put it in front will make it higher priority.

其他回答

Android SDK工具现在休息在以下位置:“android_sdk/cmdline-tools/version/bin/”;

因此,在Windows操作系统中解决此问题(其他操作系统也一样),请执行以下操作:

在android_sdk文件夹中,创建文件夹:cmdline-tools,并在其中创建另一个文件夹:version extract / put all your files "/bin /lib注意和源代码。此版本文件夹中的“属性”文件。 将ANDROID_HOME设置为android_sdk文件夹。 添加到系统路径:android_sdk\cmdlineAndroidSDK\cmdline-tools\version\bin\

类似地,将Android SDK平台工具放在android_sdk/ Platform - Tools /中,并在系统变量下的环境变量中添加相应的PATH

我们不再为每个命令执行传递参数——sdk_root,而是深入研究真正的原因。

Starting from Android SDK Command-line Tools 1.0.0 (6200805), in contrast to Android SDK 26.1.1 (4333796), the tools directory hierarchy has been changed. Previously it was placed right inside ANDROID_HOME (which is deprecated, we will use the term ANDROID_SDK_ROOT for the rest of the paragraph), now it's still named as tools (the only thing you'll get after unpacking the downloaded commandlinetools zip file), but differently, you have to place it inside a directory called cmdline-tools on your own. The name cmdline-tools comes from its package name, where you can get from listing packages command sdkmanager --list, whose outputs include cmdline-tools;1.0 | 1.0 | Android SDK Command-line Tools.

将tools目录包装到cmdline-tools目录中将使其工作,并帮助您摆脱恼人的——sdk_root参数。但是其他部分呢?

这就是你要改变的。让我解释更多。

The king - sdkmanager lives inside cmdline-tools/tools/bin, you'd better set in PATH environment variable cmdline-tools should not be set as ANDROID_SDK_ROOT. Because later, when updating Android SDK, or installing more packages, the other packages will be placed under ANDROID_SDK_ROOT, but not under cmdline-tools. The final, complete ANDROID_SDK_ROOT directory structure should look like below, consist of quite a few sub-directories: build-tools, cmdline-tools, emulator, licenses, patcher, platform-tools, platforms, system-images. You can easily point out that build-tools and cmdline-tools are siblings, all sit inside the parent ANDROID_SDK_ROOT.

让我简单概括一下:

设置您首选的ANDROID_SDK_ROOT(就像以前一样) 下载并解压commandlinetools zip文件到名为cmdline-tools的目录中,该目录位于ANDROID_SDK_ROOT目录中 将目录$ANDROID_SDK_ROOT/cmdline-tools/tools/bin追加到环境变量PATH后,这样系统就知道在哪里可以找到sdkmanager

更新! ! ! !

自构建6858069 (Android SDK命令行工具3.0)以来,行为再次发生了变化:

After unzipping the package, the top-most directory you'll get is cmdline-tools. Rename the unpacked directory from cmdline-tools to tools, and place it under $ANDROID_SDK_ROOT/cmdline-tools, so now it should look like: $ANDROID_SDK_ROOT/cmdline-tools/tools. And inside it, you should have: NOTICE.txt bin lib source.properties. Actually according to the official Command-Line Tools doc, the tree structure should be android_sdk/cmdline-tools/version/bin/, but I've checked, using version or tools makes no difference here. For your environment variable PATH, I would recommend you to set like this: PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin, because after update later, you'll get the latest sdkmanager placed under $ANDROID_SDK_ROOT/cmdline-tools/latest/bin, put it in front will make it higher priority.

我找到了使用最新命令行工具的解决方案,以下步骤:

1 -将命令行工具提取到如下结构的文件夹: 例如:$ HOME /开发/ android / cmdline-tools /最新 (此文件夹必须包含lib, bin, notice.txt和source.properties)

2 -将ANDROID_HOME定义为环境变量:

ANDROID_HOME="$HOME/Development/android/cmdline-tools/latest"

3 -加载它的路径:

PATH="$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/lib:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools"

根据@Jing Li的最新建议。这是我的gitlab-ci.yml版本

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "30"
  ANDROID_BUILD_TOOLS: "29.0.2"
  ANDROID_COMMAND_LINE_TOOLS: "6858069"
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

before_script:
  - export GRADLE_USER_HOME=$(pwd)/.gradle
  - chmod +x ./gradlew
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - mkdir -p android-sdk-linux/cmdline-tools
  - export ANDROID_SDK_ROOT=$PWD/android-sdk-linux
  - cd android-sdk-linux/cmdline-tools
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_COMMAND_LINE_TOOLS}_latest.zip
  - unzip android-sdk.zip
  - rm android-sdk.zip
  - mv cmdline-tools version
  - echo y | version/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | version/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | version/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools/
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | version/bin/sdkmanager --licenses
  - set -o pipefail
  - cd ../../
  - chmod +x ./gradlew

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

我在新Mac上下载独立命令行工具(commandlinetools-mac-6200805_latest)时就遇到了这种情况。

基于这里所有的答案,我能够让它像这样工作

# Define ANDROID_HOME, if not defined already
export ANDROID_HOME="~/Library/Android/sdk"

# Create the folder if missing
mkdir -p $ANDROID_HOME

# Let the tool know that it should use that SDK location. 
sdkmanager --list --sdk_root=$ANDROID_HOME

——sdk_root选项的文档说“使用指定的SDK根而不是包含此工具的SDK”。这让我想到,尽管该工具是独立发布的,但它有望成为SDK安装包的一部分。