Keywords: Android | Gradle | JDK | BuildError | Kotlin
Abstract: This article discusses the build error 'unrecognized Attribute name MODULE' encountered in Android development when updating to Android S (API 31) with JDK8. The error is caused by JDK version incompatibility, especially with Lambda expression code. By upgrading to JDK11 and updating Gradle configuration, this issue can be effectively resolved. The article provides a detailed technical analysis and step-by-step solution, covering causes, fix steps, and code examples.
In Android development, migrating to new API levels often leads to build errors. One common error is "unrecognized Attribute name MODULE", which can unexpectedly halt the build process. This article delves into the root causes of this error and provides a systematic solution.
Problem Description
The error occurs during the kaptDebugKotlin task in Gradle, manifesting as an AssertionError in the Java compiler. The stack trace indicates a failure in com.sun.tools.javac.code.TypeAnnotations.annotationType method, specifically involving the MODULE attribute name. The error message points to an unrecognized attribute, often related to mismatches between JDK version and Android compilation target.
Root Cause Analysis
This error stems from incompatibility between the JDK version and Android compileSdkVersion. When using compileSdkVersion 31 or higher with JDK8, the Java compiler encounters issues processing type annotations, particularly in code containing Lambda expressions. The MODULE attribute is part of the module system introduced in Java 9, and older JDKs cannot handle these new features properly, leading to compilation failures.
Solution: Upgrade to JDK11
To resolve this issue, it is essential to upgrade the JDK to version 11. In Android Studio Arctic Fox or later, configure the Gradle JDK by navigating to Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK, then select JDK 11. If this option is not available, it is recommended to update Android Studio to the latest version.
Technical Deep Dive
From a technical perspective, the MODULE attribute error reflects the evolution of Java toolchains. JDK11 and later versions introduce support for Java module systems and enhanced type annotations, which JDK8 lacks. When using Kotlin's kapt (Kotlin Annotation Processing) task, the compiler needs to correctly process these attributes, otherwise, it throws an AssertionError. This highlights the importance of keeping JDK in sync with target APIs in Android development.
Code Example: Configuring JDK Toolchain in Gradle
In addition to IDE settings, JDK version can be specified via Gradle configuration files. Below is an example snippet from a build.gradle file, demonstrating how to configure the Kotlin compiler to use JDK11:
android {
compileSdkVersion 31
// Other configurations
}
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
This configuration ensures the Kotlin compiler uses JDK11, thereby avoiding compatibility issues. If the project uses Java, similarly set sourceCompatibility and targetCompatibility to 11 in build.gradle.
Conclusion and Best Practices
Upgrading to JDK11 is crucial for ensuring compatibility with Android API 31 and above. Developers should regularly check and update JDK and Gradle configurations to prevent similar build errors. It is advisable to set toolchain versions early in the project and conduct tests for stability. By understanding the underlying technical principles, debugging and preventing such issues becomes more efficient.