在Android Studio中执行任务失败:app: compileDebugJavaWithJavac。

8

我正在MacBook m1 pro上使用Android Studio开发Android应用程序。我不太确定出了什么问题,但我认为它与room有关。

下面是错误信息:

> Task :app:compileDebugJavaWithJavac FAILED
Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.ExceptionInInitializerError

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

我在我的应用程序中有2个类。

我的类:

@Entity
public class User {
    public static final String COLLECTION_NAME = "users";

    @PrimaryKey
    @NonNull
    String name = "";
    String phone = "";
    String email = "";
    String password = "";
    String location = "";
    String gender = "";
    String age = "";
    Long updateDate = new Long(0);

    @Ignore
    public User(){} //for room

    //TODO: add location
    public User(String name, String phone, String email, String password, String gender, String age) {
        this.name = name;
        this.phone = phone;
        this.email = email;
        this.password = password;
        this.location = location;
        this.gender = gender;
        this.age = age;
    }

public class Post {
    final public static String COLLECTION_NAME = "posts";

    @PrimaryKey
    @NonNull
    String id = "";
    String name ="";
    String userId = "";
    Long postTime = new Long(0);
    String text = "";
    String image = "";
    String type = "";
    String age = "";
    String size = "";
    String gender = "";
    String location = "";

    @Ignore
    public Post(){}

    public Post(String id, String userId, String text, String image, String type, String age, String size, String gender, String location){  //}), List<String> likesUserId) {
        this.id = id;
        this.userId = userId;
        this.text = text;
        this.image = image;
        this.type = type;
        this.age = age;
        this.size = size;
        this.gender = gender;
        this.location = location;
    }

我的应用 gradle:

plugins {
    id 'com.android.application'
    id 'androidx.navigation.safeargs'
    id 'com.google.gms.google-services'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.findmehomeapp"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.room:room-common:2.2.6'
    implementation 'androidx.room:room-runtime:2.2.6'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    annotationProcessor 'androidx.room:room-compiler:2.2.6'
    def nav_version = "2.3.5"
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    implementation platform('com.google.firebase:firebase-bom:29.0.3')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-firestore'
    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-storage'

}

Gradle 项目:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        def nav_version = "2.3.5"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        classpath 'com.google.gms:google-services:4.3.10'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我已经尝试过以下方法:

  1. 清除缓存并重启。
  2. 构建 -> 清理项目。
  3. 删除并添加我的成绩。
  4. 更改类中的@Ignore位置并删除。

我真的希望有人能够提供帮助。


1
你找到解决方案了吗? - Neel
3个回答

3

我曾经遇到过同样的问题,我修改了两个地方并解决了问题。

  1. 文件 -> 项目结构 -> SDK位置,然后点击“JDK位置已移动到Gradle设置”。然后,选择“Android Studio默认jdk版本(版本11)”并应用。第一步完成。

  2. 文件 -> 项目结构 -> 模块,确保源兼容性(Source Compatibility)目标兼容性(Target Compatibility)都是"$JavaVersion.VERSION__11"并应用。

我使用的Kotlin版本是1.6.10,这可能也很重要。


同样的问题,你能告诉我们 buildToolsVersion 是多少吗? - julio
编译SDK版本31 构建工具版本“30.0.3” - Rickon Gao
为什么需要 Android Studio 默认的 JDK 版本(版本 11) - IgorGanapolsky

1

简短回答:

将 Room 版本更新至 2.4.0-alpha03 或更高版本

详细回答:

我遇到了同样的问题,并尝试了所有这些方法,但都没有起作用。然后我执行了以下步骤:

  1. 在异常下的构建选项卡中点击“使用 --stacktrace 选项运行”。它会显示问题的根本原因。
  2. 然后你可以进一步查看错误及其解决方案。

对我来说,错误是以下内容:

Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch64. path=/org/sqlite/native/Mac/aarch64

经过进一步调查,我发现这是由于我使用的Room库的异常情况。我使用的是旧版本,不支持Mac M1芯片。因此,我已将其版本更改为2.4.0-alpha03或更高版本。

0

您可以将 implementation 'androidx.appcompat:appcompat:1.4.0' 更改为 implementation 'androidx.appcompat:appcompat:1.3.1'


3
没什么帮助,但还是谢谢你。 - Kely_97

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接