Program type already present: android.support.constraint.BuildConfig

跟風遠走 提交于 2019-12-05 16:57:43

问题


build.gradle (Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 'android-P'
    buildToolsVersion '27.0.3'
    defaultConfig {
        multiDexEnabled true
        applicationId "tk.megh.myapplication"
        minSdkVersion 'P'
        targetSdkVersion 'P'
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        preDexLibraries = false
    }
}



dependencies {
    implementation 'com.android.support:multidex:1.0.1'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:+'
    testImplementation 'junit:junit:4.12'

}

Well i think i know what's causing the error, if you look at the dependencies there are two redundant packages with different names

com.android.support.constraint:constraint-layout:1.1.0 androidx.constraintlayout:constraintlayout:1.1.0

But i can't remove either of them because they are used by some packages. I'm a novice in android development, so i don't have much idea about any workarounds.

if i remove

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

i get this error while debugging:

    java.lang.RuntimeException: Unable to start activity 
ComponentInfo{tk.megh.myapplication/tk.megh.myapplication.MainActivity}: 
android.view.InflateException: Binary XML file line #2: Binary XML file 
line #2: Error inflating class android.support.constraint.ConstraintLayout

and if i remove

implementation 'androidx.constraintlayout:constraintlayout:1.1.0'

i get the following error while debugging:

 java.lang.RuntimeException: Unable to start activity 
ComponentInfo{tk.megh.myapplication/tk.megh.myapplication.DisplayMessageActivity}: 
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: 
Error inflating class androidx.constraintlayout.widget.ConstraintLayout

Additional Details:

imports of MainActivity.java:

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

imports of DisplayMessageActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;   

Thanks in advance.


回答1:


The errors indicate that you're using the ConstraintLayout in your layout xml files.

Keep only one version of the library and make sure, that you are using that version's ConstraintLayout in your xmls.

So, if you keep androidx, check your layout files and make sure, you are using androidx.constraintlayout.ConstraintLayout there, and not android.support.constraint.ConstraintLayout.




回答2:


Be careful not to reference com.android.support.constraint:constraint-layout and androidx.constraintlayout:constraintlayout at the same time. Settle on one (preferably androidx), remove the other, and make sure the package names are consistent in your layout files too. That fixed the issue for me.




回答3:


I also had the same issue. I was using two different versions for android.arch.core library.So,fixing those versions helped me.Try to use one version throughout your application.Hope it helps someone.

Thanks



来源:https://stackoverflow.com/questions/50279992/program-type-already-present-android-support-constraint-buildconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!