Android Studio 编译: Program type already present: XXX 解决方案

https://blog.csdn.net/richiezhu/article/details/80320185

按住两次SHIFT 进行类名搜索,然后再gradle中 配置configruations,all*.exclude 这个module ''

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.keraseye.xkk.keraseye"
        minSdkVersion 21
        targetSdkVersion 28
        multiDexEnabled true
        javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14"
            }
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/INDEX.LIST'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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'
    implementation 'org.deeplearning4j:deeplearning4j-core:0.7.2'
    implementation 'org.nd4j:nd4j-native:0.7.2'

    implementation 'org.nd4j:nd4j-native:0.7.2:android-x86'
    implementation 'org.nd4j:nd4j-native:0.7.2:android-arm'
    implementation 'org.deeplearning4j:deeplearning4j-modelimport:1.0.0-beta3'
}

configurations {
    all*.exclude module:'nd4j-base64'
}

情况1:个例
build.gradle 中

 dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        //
    }
改成

 dependencies {
         //目前最新版【2018年05月15日】
         classpath 'com.android.tools.build:gradle:3.1.2'
        //
    }
情况2:确实是依赖冲突
Error: Program type already present: android.support.v4.app.xxx

例子: 
引入以下依赖报该错误

//Paho Android Service
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
执行命令:

gradlew -q app:dependencies
 


排查 support-v4 出现的依赖关系中 发现 org.eclipse.paho:org.eclipse.paho.android.service:1.0.2 
依赖了 com.google.android:support-v4 注意不是 com.android.support:support-v4 !!!

坑:

//刚开始下意识去写了个排除,发现没有用。。。,原因就是它用 com.google.android:support-v4 不是 com.android.support
    implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'){
          exclude group: 'com.android.support', module: 'support-v4'
      }
总结: 
找到依赖的问题根源后进行排除,按提示报错的来灵活处理冲突问题!

排除方式1:

configurations {
   all*.exclude group: 'com.google.android', module: 'support-v4'
   //或者粗暴点,就没有上面的坑了  all*.exclude module: 'support-v4'
}
dependencies {
...
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
...
}
排除方式2:

 implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
 implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
         exclude(group: 'com.google.android', module: 'support-v4')
    }

/* 或者粗暴点,就没有上面的坑了
implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
        exclude module: 'support-v4'
    }
    */

com.android.support:xxx 等官方依赖包 v4 v7 v13 等版本号保持一致 比如 27.1.1
--------------------- 
作者:路易斯睿齐 
来源:CSDN 
原文:https://blog.csdn.net/richiezhu/article/details/80320185 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Android Studio 编译: Program type already present: XXX 解决方案)