簡體   English   中英

任務執行失敗:app:compileDebugJavaWithJavac in Android Studio java

[英]Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio java

我正在使用 MacBook m1 pro 在 Android Studio 中開發一個 Android 應用程序。 不太確定出了什么問題,我認為這與房間有關。

這是錯誤:

> 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;
    }

我的應用程序等級:

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. 文件 -> 項目結構 -> SDK 位置並單擊“JDK 位置已移至 Gradle 設置”。 然后,select “Android studio default jdk version (version 11)”並申請。 第一步完成。

  2. 文件 -> 項目結構 -> 模塊,確保源兼容性目標兼容性“$JavaVersion.VERSION__11”並應用。

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

您可以將實現 'androidx.appcompat:appcompat:1.4.0'更改為實現 'androidx.appcompat:appcompat:1.3.1'

回答我的問題

對於使用 M1 /M1 Pro 芯片的用戶,請使用 Room 2.4.0-alpha03。

def room_version = "2.4.0-alpha03"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM