程序类型已存在:com.google.android.material.internal.package-info

Abd*_*511 5 android gradle android-studio

我在编译代码时遇到此错误:

<!-- language: lang-none -->        
Program type already present: 
    com.google.android.material.internal.package-info
    Message{kind=ERROR, text=Program type already present: 
    com.google.android.material.internal.package-info, sources= 
    [Unknown source file], tool name=Optional.of(D8)}
Run Code Online (Sandbox Code Playgroud)

当我搜索类似的错误时,它建议添加

configurations {all*.exclude group: 'com.android.support', module: 'support-v13'}
Run Code Online (Sandbox Code Playgroud)

或将这些行添加到“ gradle.properties ”文件

android.useAndroidX = true
android.enableJetifier = false
Run Code Online (Sandbox Code Playgroud)

但这些解决方案都不适合我,这是我的 gradle 文件

app 

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 28  
    defaultConfig {
        applicationId "com.example.agh.yaomi"
        minSdkVersion 17
        targetSdkVersion 28
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v13'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // added meta data in manifiest to resolve
    //suppport version conflict
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:support-annotations:28.0.0-beta01'
    implementation "org.jetbrains.anko:anko:0.10.4"
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'com.android.support:design:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:support-media-compat:28.0.0-alpha3'

    // added to solve merge dex error
    implementation 'com.android.support:multidex:1.0.3'

    implementation ('com.github.omadahealth:lollipin:2.1.0@aar') {
        transitive = true
    }
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Run Code Online (Sandbox Code Playgroud)

项目

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

buildscript {
    ext.kotlin_version = '1.2.21'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

allprojects {
    repositories {
        google()
        jcenter()
        maven{ url "https://github.com/omadahealth/omada-nexus/raw/master/release" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Moh*_*waf 3

经过3个小时的搜索,我解决了:

问题是:添加material:1.0.0-beta01依赖项并使用任何版本的支持库时,都会出现此错误。

解决方案:

1-您应该使用所有androidx依赖项。

所以这些行

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:support-annotations:28.0.0-beta01'
implementation "org.jetbrains.anko:anko:0.10.4"
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'com.android.support:design:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-media-compat:28.0.0-alpha3'
Run Code Online (Sandbox Code Playgroud)

将它们更改为:

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.annotation:annotation:1.0.0-alpha1'
implementation "org.jetbrains.anko:anko:0.10.4"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'androidx.media:media:1.0.0-alpha1'
Run Code Online (Sandbox Code Playgroud)

我们删除了design:28.0.0-alpha3,因为它包含在material:1.0.0-beta01

2-将这些行添加到gradle.properties文件中

android.useAndroidX = true
android.enableJetifier = false
Run Code Online (Sandbox Code Playgroud)

正如这个答案中提到的。

3-最后一步您可能会遇到错误,MainActivity因为您仍然AppCompatActivity从支持库导入,因此您应该删除旧的导入并从androidx包中再次导入它,如下所示

import androidx.appcompat.app.AppCompatActivity;
Run Code Online (Sandbox Code Playgroud)