程序类型已存在:android.support.compat.R $ attr

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了程序类型已存在:android.support.compat.R $ attr相关的知识,希望对你有一定的参考价值。

我的项目有两个模块,即app和moduleX。 app项目主要基于Java构建,而moduleX完全基于Kotlin。

在创建“debug”构建时,它运行正常但是当我尝试创建发布版本,即devRelease时,它会给出以下错误 -

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithDexMergerForDevRelease'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
  Program type already present: android.support.compat.R$attr

这是app level build.gradle-

apply plugin: 'com.android.application'
apply plugin: 'com.facebook.testing.screenshot'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    defaultConfig {
        applicationId "com.appname"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner rootProject.ext.testInstrumentationRunner
    }
    flavorDimensions "environment"
    productFlavors {
        dev {
            dimension "environment"
            versionNameSuffix "-dev"
            applicationIdSuffix ".dev"
        }

        qa {
            dimension "environment"
            versionNameSuffix "-test"
            applicationIdSuffix ".test"
        }

        staging {
            dimension "environment"
            versionNameSuffix "-staging"
            applicationIdSuffix ".staging"
        }

        prod {
            dimension "environment"
        }
    }

    signingConfigs {
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newDataInputStream())
        release {
            storeFile file("keystore/appname_keystore.jks")
            storePassword properties.getProperty('storePassword')
            keyAlias properties.getProperty('keyAlias')
            keyPassword properties.getProperty('keyPassword')
        }
    }

    buildTypes {
        debug {
            shrinkResources false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            shrinkResources false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }

    bundle {
        language {
            enableSplit = false
        }
    }
    configurations.all {
        resolutionStrategy {
            // force certain versions of dependencies (including transitive)
            force 'com.squareup.okhttp3:okhttp:' + okHttpLibVersion
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //Unit testing
    testImplementation rootProject.ext.junit
    androidTestImplementation rootProject.ext.androidTestRunner
    androidTestImplementation rootProject.ext.espresso
    testImplementation rootProject.ext.mockito
    testImplementation rootProject.ext.facebookScreenshotTestCommon
    implementation rootProject.ext.facebookScreenshotTestLitho
    androidTestImplementation rootProject.ext.supportTestRules

    //Support Library & UI
    implementation rootProject.ext.constraintLayout
    implementation rootProject.ext.supportCompatV7
    implementation rootProject.ext.supportDesign
    implementation rootProject.ext.supportCardView
    implementation rootProject.ext.supportCustomTabs
    implementation rootProject.ext.glide
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    // Dagger dependency for DI
    implementation 'com.google.dagger:dagger:2.16'
    annotationProcessor "com.google.dagger:dagger-compiler:2.16"
    compileOnly 'javax.annotation:jsr250-api:1.0'
    implementation 'javax.inject:javax.inject:1'

    // RxJava lib
    implementation rootProject.ext.rxAndroid
    implementation rootProject.ext.rxJava
    implementation rootProject.ext.rxJavaRetrofitAdapter

    //Retrofit
    implementation(rootProject.ext.retrofit) {
        exclude module: 'okhttp'
    }
    implementation rootProject.ext.okHttp
    implementation rootProject.ext.okHttpLoggingInterceptor
    implementation rootProject.ext.retrofitGsonConverter
    implementation rootProject.ext.retrofitScalarsConverter

    //Memory leaks
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
    debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'

    //Others
    implementation rootProject.ext.parceler
    annotationProcessor rootProject.ext.parcelerAnnotationProcessor
    implementation rootProject.ext.lombok
    annotationProcessor rootProject.ext.lombokAnnotationProcessor
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

    // Fingerprint Auth
    implementation 'com.multidots:fingerprint-auth:1.0.1'

    //Module Projects
    api project(':energyswitchcui')
}

screenshots {
    multipleDevices true
}

这是moduleX的build.gradle文件

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"

        // need separate runner for facebook screenshot test in module
        testInstrumentationRunner 'com.appname.SnapshotTestRunner'
    }

    flavorDimensions "environment"
    productFlavors {
        dev {
            dimension "environment"
        }

        qa {
            dimension "environment"
        }

        staging {
            dimension "environment"
        }

        prod {
            dimension "environment"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //Unit testing
    testImplementation rootProject.ext.junit
    androidTestImplementation rootProject.ext.androidTestRunner
    androidTestImplementation rootProject.ext.espresso
    testImplementation rootProject.ext.mockito
    testImplementation rootProject.ext.facebookScreenshotTestCommon
    implementation rootProject.ext.facebookScreenshotTestLitho
    androidTestImplementation rootProject.ext.supportTestRules

    //Support Library & UI
    implementation rootProject.ext.constraintLayout
    implementation rootProject.ext.supportCompatV7
    implementation rootProject.ext.supportDesign
    implementation rootProject.ext.supportCardView
    implementation rootProject.ext.supportCustomTabs
    implementation rootProject.ext.glide
    implementation 'com.intuit.sdp:sdp-android:1.0.6'

    // RxJava lib
    implementation rootProject.ext.rxAndroid
    implementation rootProject.ext.rxJava
    implementation rootProject.ext.rxJavaRetrofitAdapter

    //Retrofit
    implementation(rootProject.ext.retrofit) {
        exclude module: 'okhttp'
    }
    implementation rootProject.ext.okHttp
    implementation rootProject.ext.okHttpLoggingInterceptor
    implementation rootProject.ext.retrofitGsonConverter
    implementation rootProject.ext.retrofitScalarsConverter

    //Others
    implementation rootProject.ext.parceler
    kapt rootProject.ext.parcelerAnnotationProcessor
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
}

repositories {
    mavenCentral()
}

// need for facebook screenshot test in module
apply plugin: 'com.facebook.testing.screenshot'
screenshots {
    multipleDevices true
}

这是项目级build.gradle文件 -

apply from: 'dependencies.gradle'

buildscript {
    ext.kotlinVersion = '1.3.30'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath 'com.facebook.testing.screenshot:plugin:0.8.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

这是依赖.gradle-

ext {
    //Android
    minSdkVersion = 24
    targetSdkVersion = 28
    compileSdkVersion = targetSdkVersion
    testInstrumentationRunner = "com.appname.runner.SnapshotTestRunner"
    androidSupportLibVersion = "28.0.0"
    okHttpLibVersion = "3.14.0"

    //Unit testing
    junit = "junit:junit:4.12"
    androidTestRunner = "com.android.support.test:runner:1.0.2"
    espresso = "com.android.support.test.espresso:espresso-core:3.0.2"
    mockito = "org.mockito:mockito-all:1.10.19"
    facebookScreenshotTestCommon = "com.facebook.testing.screenshot:layout-hierarchy-common:0.8.0"
    facebookScreenshotTestLitho = "com.facebook.testing.screenshot:layout-hierarchy-litho:0.8.0"
    supportTestRules = "com.android.support.test:rules:1.0.2"

    //Support Library & UI
    constraintLayout = "com.android.support.constraint:constraint-layout:1.1.3"
    supportCompatV7 = "com.android.support:appcompat-v7:$androidSupportLibVersion"
    supportDesign = "com.android.support:design:$androidSupportLibVersion"
    supportCardView = "com.android.support:cardview-v7:$androidSupportLibVersion"
    supportCustomTabs = "com.android.support:customtabs:$androidSupportLibVersion"
    glide = "com.github.bumptech.glide:glide:3.7.0"

    // RxJava lib
    rxAndroid = "io.reactivex.rxjava2:rxandroid:2.0.1"
    rxJava = "io.reactivex.rxjava2:rxjava:2.1.8"
    rxJavaRetrofitAdapter = "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"

    //Retrofit
    retrofit = "com.squareup.retrofit2:retrofit:2.4.0"
    okHttp = "com.squareup.okhttp3:okhttp:$okHttpLibVersion"
    okHttpLoggingInterceptor = "com.squareup.okhttp3:logging-interceptor:$okHttpLibVersion"
    retrofitGsonConverter = "com.squareup.retrofit2:converter-gson:2.3.0"
    retrofitScalarsConverter = "com.squareup.retrofit2:converter-scalars:2.3.0"

    //Others
    parceler = "org.parceler:parceler-api:1.1.6"
    parcelerAnnotationProcessor = "org.parceler:parceler:1.1.6"
    lombok = "org.projectlombok:lombok:1.16.16"
    lombokAnnotationProcessor = "org.projectlombok:lombok:1.16.16"
}

我已经尝试了许多答案,即Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

但这里没有任何工作。

答案

嗨,我已经提出了解决方案,只需很少的版本更改就可以在gradles下面成功运行:

主要项目等级:

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

buildscript {
    ext.kotlin_version = '1.3.30'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath 'com.facebook.testing.screenshot:plugin:0.8.0'
        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()

    }
}

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


ext {
    //Android
    minSdkVersion = 24
    targetSdkVersion = 28
    compileSdkVersion = targetSdkVersion
    testInstrumentationRunner = "com.appname.runner.SnapshotTestRunner"
    androidSupportLibVersion = "28.0.0"
    androidCompatVersion = "1.0.0-beta01"
    androidCardViewVersion = "1.0.0"
    constraintlayoutVersion = "1.1.3"
    okHttpLibVersion = "3.14.0"

    //Unit testing
    junit = "junit:junit:4.12"
    androidTestRunner = "androidx.test:runner:1.1.0-alpha4"
    espresso = "androidx.test.espresso:espresso-core:3.1.0-alpha4"
    supportTestRules = "com.android.support.test:rules:1.0.2"

    //Support Library & UI
    constraintLayout = "androidx.constraintlayout:constraintlayout:$constraintlayoutVersion"
    supportCompatV7 = "androidx.appcompat:appcompat:$androidCompatVersion"
    supportDesign = "com.android.support:design:$androidSupportLibVersion"
    supportCardView = "androidx.cardview:cardview:$androidCardViewVersion"
    supportCustomTabs = "com.android.support:customtabs:$androidSupportLibVersion"
    glide = "com.github.bumptech.glide:glide:3.7.0"


    mockito = "org.mockito:mockito-all:1.10.19"
    facebookScreenshotTestCommon = "com.facebook.testing.screenshot:layout-hierarchy-common:0.8.0"
    facebookScreenshotTestLitho = "com.facebook.testing.screenshot:layout-hierarchy-litho:0.8.0"


    // RxJava lib
    rxAndroid = "io.reactivex.rxjava2:rxandroid:2.0.1"
    rxJava = "io.reactivex.rxjava2:rxjava:2.1.8"
    rxJavaRetrofitAdapter = "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"

    //Retrofit
    retrofit = "com.squareup.retrofit2:retrofit:2.4.0"
    okHttp = "com.squareup.okhttp3:okhttp:$okHttpLibVersion"
    okHttpLoggingInterceptor = "com.squareup.okhttp3:logging-interceptor:$okHttpLibVersion"
    retrofitGsonConverter = "com.squareup.retrofit2:converter-gson:2.3.0"
    retrofitScalarsConverter = "com.squareup.retrofit2:converter-scalars:2.3.0"

    //Others
    parceler = "org.parceler:parceler-api:1.1.9"
    parcelerAnnotationProcessor = "org.parceler:parceler:1.1.9"
    lombok = "org.projectlombok:lombok:1.16.16"
    lombokAnnotationProcessor = "org.projectlombok:lombok:1.16.16"

}

App Level gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.example.gradletest"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation rootProject.ext.junit
    androidTestImplementation rootProject.ext.androidTestRunner
    androidTestImplementation rootProject.ext.espresso
    androidTestImplementation rootProject.ext.supportTestRules
    testImplementation rootProject.ext.mockito
    testImplementation rootProject.ext.facebookScreenshotTestCommon
    implementation rootProject.ext.facebookScreenshotTestLitho
    implementation rootProject.ext.constraintLayout
    implementation rootProject.ext.supportCompatV7
    implementation rootProject.ext.supportDesign
    implementation rootProject.ext.supportCardView
    implementation rootProject.ext.supportCustomTabs
    implementation rootProject.ext.glide
    // RxJava lib
    implementation rootProject.ext.rxAndroid
    implementation rootProject.ext.rxJava
    implementation rootProject.ext.rxJavaRetrofitAdapter
    //Retrofit
    implementation(rootProject.ext.retrofit) {
        exclude module: 'okhttp'
    }
        implementation rootProject.ext.okHttp
        implementation rootProject.ext.okHttpLoggingInterceptor
        implementation rootProject.ext.retrofitGsonConverter
        implementation rootProject.ext.retrofitScalarsConverter
    implementation rootProject.ext.parceler
//        annotationProcessor rootProject.ext.parcelerAnnotationProcessor
        implementation rootProject.ext.lombok
        annotationProcessor rootProject.ext.lombokAnnotationProcessor
    //Memory leaks
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
    debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.multidots:fingerprint-auth:1.0.1'
    implementation project(':energyswitchcui')
}

模块(“energyswitchcui”)gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //Unit testing
    testImplementation rootProject.ext.junit
    androidTestImplementation rootProject.ext.androidTestRunner
    androidTestImplementation rootProject.ext.espresso
    testImplementation rootProject.ext.mockito
    testImplementation rootProject.ext.facebookScreenshotTestCommon
    implementation rootProject.ext.facebookScreenshotTestLitho
    androidTestImplementation rootProject.ext.supportTestRules

    //Support Library & UI
    implementation rootProject.ext.constraintLayout
    implementation rootProject.ext.supportCompatV7
    implementation rootProject.ext.supportDesign
    implementation rootProject.ext.supportCardView
    implementation rootProject.ext.supportCustomTabs
    implementation rootProject.ext.glide
    implementation 'com.intuit.sdp:sdp-android:1.0.6'

    // RxJava lib
    implementation rootProject.ext.rxAndroid
    implementation rootProject.ext.rxJava
    implementation rootProject.ext.rxJavaRetrofitAdapter

    //Retrofit
    implementation(rootProject.ext.retrofit) {
        exclude module: 'okhttp'
    }
    implementation rootProject.ext.okHttp
    implementation rootProject.ext.okHttpLoggingInterceptor
    implementation rootProject.ext.retrofitGsonConverter
    implementation rootProject.ext.retrofitScalarsConverter

    //Others
    implementation rootProject.ext.parceler
//    kapt rootProject.ext.p

以上是关于程序类型已存在:android.support.compat.R $ attr的主要内容,如果未能解决你的问题,请参考以下文章

程序类型已存在:android.support.v7.util.AsyncListUtil $ 1

错误:程序类型已存在:android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat

错误:程序类型已存在:android.support.design.widget.CoordinatorLayout$Behavior

程序类型已存在:com.google.android.gms.location.places.zza

程序类型已存在:com.google.android.gms.common.util.VisibleForTesting -':app:transformDexArchiveWithExternalL

程序类型已存在:android.support.compat.R $ attr