Keywords: Android | Gradle | SDK | Build Error | API Level
Abstract: This article provides an in-depth analysis of the Gradle build error "failed to find target with hash string 'android-22'" in Android Studio, explaining that the root cause is the absence of the specified API level SDK. Based on the best solution, the article highlights steps to install the required SDK via the Android SDK Manager, supplemented by methods to update SDK tools and configure build.gradle. With code examples and step-by-step guidance, it offers best practices to help developers quickly restore project builds.
Problem Description
When building projects in Android Studio, developers often encounter the Gradle error message: "failed to find target with hash string 'android-22'". This error typically appears after updating Android Studio or the SDK, even if API 18 is installed, as the build process detects missing corresponding API levels. Additionally, the exception message includes a link "Open Android SDK Manager" to open the SDK manager.
Error Analysis
The fundamental cause of this exception is the lack of the Android SDK corresponding to the API level specified in the project configuration. In Android development, the Gradle build system defines the API target via targetSdkVersion and compileSdkVersion. When targetSdkVersion is set to 19 or another value in the build.gradle file, but the local environment lacks the SDK for that API, this type of exception is triggered. For example, 'android-22' in the error indicates API 22, and if only API 18 is installed, the build will fail. This is a platform-related error, not a code logic issue.
Primary Solution: Using Android SDK Manager
Based on the best solution, the most direct way to resolve this exception is to install the required SDK via the Android SDK Manager. Specific steps are as follows:
- In Android Studio, click the link "Open Android SDK Manager" in the error message. If the error message is not displayed, manually open it: go to Tools → Android → SDK Manager.
- In the SDK Manager interface, review the list of installed SDKs. Ensure to adjust the API level to the value required by the project, such as API 22 or higher, and click the checkbox in front of the corresponding API to install it.
- After installation, restart Android Studio and re-sync the project (Sync Project with Gradle Files). In most cases, the build error will be resolved automatically.
Typically, this single step is sufficient to restore the build process. However, if similar issues persist, consider updating the SDK-related tools.
Supplementary Measures: Updating SDK Tools and Configuring build.gradle
As a complementary approach, it is recommended to update Android SDK tools and build tools to ensure compatibility and performance. This method references auxiliary answers, with specific steps as follows:
- In the SDK Manager, ensure that the latest versions of Android SDK Tools and Android SDK Build Tools are installed. These tools are available in the download options of the SDK Manager.
- Properly configure the project's
build.gradlefile. Rewritten example code based on modern practices is as follows:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
}
}
In this code, compileSdkVersion is set to 23, representing compilation with API 23; buildToolsVersion is "23.0.1", specifying the build tool version; targetSdkVersion remains 19 to maintain application-layer API compatibility. Note: ensure the local SDK is installed for the corresponding API level, or errors may be triggered.
Conclusion
In summary, the Gradle build error "failed to find target with hash string 'android-22'" is primarily caused by the absence of the specified API level Android SDK. The most effective solution is to install the required SDK via the Android SDK Manager, which is the core recommendation from the best answer. Combining this with updating SDK tools and correctly configuring build.gradle can reduce the probability of subsequent build anomalies. Developers are advised to regularly check the consistency between the local environment and project configuration to avoid similar issues. Through step-by-step optimization, the stability and efficiency of Android development can be enhanced.