MacBook M1 Pro环境下Android Studio中:app:compileDebugJavaWithJavac任务执行失败(疑似Room相关问题)
ExceptionInInitializerError on M1 Pro MacBook Hey there, let's tackle this Room-related compilation error you're hitting on your M1 Pro MacBook. That ExceptionInInitializerError can stem from a few different issues, so let's walk through targeted fixes based on your setup.
First: Fix the Missing Entity Annotation
Looking at your code, your Post class doesn't have the @Entity annotation! Room requires every model class you want to map to a database table to be marked with this annotation. Without it, the Room compiler can't recognize it as an entity, which is likely triggering the initialization error.
Update your Post class to add the annotation:
@Entity // Add this critical line public class Post { final public static String COLLECTION_NAME = "posts"; @PrimaryKey @NonNull String id = ""; // ... rest of your existing code }
Second: Upgrade Room Dependencies for M1 Compatibility
Your current Room version (2.2.6) is quite outdated, and older versions often have poor support for Apple Silicon chips. Let's upgrade to a more recent stable version optimized for M1.
In your app-level build.gradle, update all Room dependencies to use the same consistent version:
dependencies { // Replace old Room dependencies with these implementation 'androidx.room:room-runtime:2.4.3' implementation 'androidx.room:room-common:2.4.3' annotationProcessor 'androidx.room:room-compiler:2.4.3' // ... rest of your existing dependencies }
Third: Add M1-Specific Gradle Configurations
M1 chips sometimes need extra configuration to handle native libraries and JDK compatibility. Add these lines to your app-level build.gradle inside the android block:
android { // ... your existing compileSdk, defaultConfig, buildTypes, etc. // Add these for M1 compatibility splits { abi { enable true reset() include 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' universalApk true } } packagingOptions { exclude 'META-INF/INDEX.LIST' exclude 'META-INF/io.netty.versions.properties' } }
Also, open your project's gradle.properties file and add these lines to enforce using Android Studio's embedded JDK (which is M1-friendly):
org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/Contents/Home android.useAndroidX=true android.enableJetifier=true org.gradle.jvmargs=-Xmx2048m
Fourth: Rebuild (Not Just Clean) Your Project
After making these changes, do a full rebuild instead of just cleaning—this ensures all annotation processor-generated code gets updated properly:
- Go to
Build > Rebuild Project - Wait for the build to complete, then check if the error persists
Fifth: Verify JDK Setup
Ensure you're using Android Studio's embedded JDK instead of a system-installed one (which might be incompatible with M1):
- Open
File > Project Structure - Navigate to
SDK Location - Under
JDK Location, selectEmbedded JDK (recommended)
Final Quick Checks
- Double-check that both
UserandPostclasses have all fields properly initialized (your current code looks okay here, but it's worth confirming) - If you have a Room database class (not shared here), make sure it correctly references both entities in its
@Databaseannotation
Give these steps a try—they should resolve the ExceptionInInitializerError you're seeing.
内容的提问来源于stack exchange,提问作者Kely_97

