未能找到具有哈希字符串 'android-25' 的目标,但实际安装了 android-25

Failed to find target with hash string 'android-25', though android-25 is actually installed

我在 OpenStack 中有一个 Ubuntu Xenial VM 运行,我在那里下载了最新的 Android SDK 工具(tools_r25.2.3-linux.zip ) from https://developer.android.com/studio/index.html.一切都配置好了:

但是当尝试执行任务时:./gradlew clean,出现错误:

Preparing "Install Android SDK Build-Tools 25".
Warning: Trying to install into ${ANDROID_HOME}/build-tools/25.0.0/ but package "Android SDK Tools 25.2.4" already exists at ${ANDROID_HOME}. It must be deleted or moved away before installing into a child directory.
Preparing "Install Android SDK Platform 25".
Warning: Trying to install into ${ANDROID_HOME}/platforms/android-25/ but package "Android SDK Tools 25.2.4" already exists at ${ANDROID_HOME}. It must be deleted or moved away before installing into a child directory.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':demo'.
> Failed to install the following SDK components:
  [Android SDK Build-Tools 25, Android SDK Platform 25]
  Please install the missing components using the SDK manager in Android Studio.

实际上正如错误日志中提到的那样,我已经安装了所需的工具。 运行 $ANDROID_HOME/bin/sdkmanager --list 显示我安装的包:

Installed packages:
  Path                        | Version | Description                       | Location
  -------                     | ------- | -------                           | -------
  build-tools;25.0.0          | 25.0.0  | Android SDK Build-Tools 25        | build-tools/25.0.0/
  extras;android;m2repository | 41.0.0  | Android Support Repository, re... | extras/android/m2repository/
  platform-tools              | 25.0.2  | Android SDK Platform-Tools 25.0.2 | platform-tools/
  platforms;android-25        | 3       | Android SDK Platform 25, rev 3    | platforms/android-25/
  tools                       | 25.2.4  | Android SDK Tools 25.2.4          | tools/

这里可能有什么问题?我对这里的SDK路径有点怀疑。

刚刚证实了我的推测 - 这是由于最新的 Android SDK 版本中的新变化所致。

感谢上帝,我们有时光机,这样我们就可以回去看看到底发生了什么(无法​​从 SDK Tools Release Notes 中找到任何线索)。

我们来看看ANDROID_HOME改变前后的文件结构

之前:

android-sdk-linux
├── SDK Readme.txt
├── add-ons (empty)
├── platforms (empty)
└── tools
    ├── a bunch of preloaded tools
    ├── ...
    └── ...

之后:

tools
├── a bunch of preloaded tools
├── ...
└── ...

不再有 add-onsplatforms,只有 tools 作为新的根目录。

问题:如果直接将$ANDROID_HOME设置到解压后的最顶层tools目录,Gradle会混淆。

解决方法:解压新的SDK文件后(会得到一个tools目录),新建一个目录(如android-sdk),然后将 tools 目录完全移动到空的 android-sdk 中,然后将环境变量设置为:

ANDROID_HOME=/android-sdk
PATH=${PATH}:${ANDROID_HOME}/tools

简而言之:不要$ANDROID_HOME 直接设置到新的 tools 父目录,将其包裹在另一个父目录中!

我刚刚在我的 android 工作室中使用了 Invalidate catches / Restart。它对我有用。