You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacBook M1 Pro环境下Android Studio中:app:compileDebugJavaWithJavac任务执行失败(疑似Room相关问题)

Fixing Room Compilation 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:

  1. Go to Build > Rebuild Project
  2. 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):

  1. Open File > Project Structure
  2. Navigate to SDK Location
  3. Under JDK Location, select Embedded JDK (recommended)

Final Quick Checks

  • Double-check that both User and Post classes 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 @Database annotation

Give these steps a try—they should resolve the ExceptionInInitializerError you're seeing.

内容的提问来源于stack exchange,提问作者Kely_97

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:32:34