错误:程序类型已存在:android.arch.lifecycle.LiveData-编译错误

时间:2019-02-08 05:41:59

标签: android android-studio android-gradle build.gradle

在编译项目时出现这些错误 为了解决dex档案错误,我尝试从项目目录中删除.gradle文件夹,但问题仍未解决。

这是我的项目gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven{
            url "https://maven.google.com"
        }
    }
}

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

这是我的应用gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.spars.myapplication"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.razorpay:checkout:1.4.5'
    implementation 'com.google.firebase:firebase-database:16.0.6'
    implementation 'com.firebaseui:firebase-ui:3.0.0'
    testImplementation 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

//added part xxx
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        }
    }
}

这是我的项目的Java文件:

package com.example.xxx.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class MainActivity extends AppCompatActivity {
    ListView lv;
    FirebaseListAdapter adapter;
    FirebaseDatabase database;
    DatabaseReference ref;
    TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Payment.class);
                startActivity(i);
            }
        });
        database = FirebaseDatabase.getInstance();
        ref = database.getReference();
        lv=(ListView)findViewById(R.id.listView);
        Query query = FirebaseDatabase.getInstance().getReference().child("martyrs");
        FirebaseListOptions<martyrs> options = new FirebaseListOptions.Builder<martyrs>()
                .setLayout(R.layout.martyrs)
                .setLifecycleOwner(MainActivity.this)
                .setQuery(query,martyrs.class)
                .build();
        adapter = new FirebaseListAdapter(options) {
            @Override
            protected void populateView(View v, Object model, int position) {
                //ImageView img = v.findViewById(R.id.);
                TextView name = v.findViewById(R.id.name);
                TextView age = v.findViewById(R.id.age);
                martyrs mt = (martyrs) model;
                name.setText(mt.getName().toUpperCase());
                age.setText(mt.getAge().toUpperCase());
            }
        };
        lv.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

最后我遇到以下错误:

  1. 原因:java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:合并dex存档时出错:

  2. 原因:com.android.builder.dexing.DexArchiveMergerException:合并dex存档时出错:

  3. 原因:com.android.tools.r8.CompilationFailedException:编译无法完成

  4. 原因:com.android.tools.r8.utils.AbortException:错误:程序类型已存在:android.arch.lifecycle.LiveData $ 1

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

这为我解决了问题

  1. 干净
  2. 重建

请尝试并提供反馈。

更新1:

尝试在您的app.gradle中添加以下代码行

implementation "android.arch.lifecycle:extensions:1.0.0"
// Multidex
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.google.firebase:firebase-analytics:15.0.0'

答案 1 :(得分:1)

更改

implementation 'com.firebaseui:firebase-ui:3.0.0'

收件人

implementation 'com.firebaseui:firebase-ui:3.3.1'
implementation 'android.arch.lifecycle:extensions:1.1.1