【问题标题】:Error: duplicate files during packaging of APK after add Realm dependence to gradle错误:将Realm依赖添加到gradle后打包APK时出现重复文件
【发布时间】:2016-04-28 02:45:54
【问题描述】:

问题是在我将领域库添加到我的项目后发生的。我在我的 gradle 项目中有这个依赖:

apply plugin: 'com.android.library'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }

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

        packagingOptions {
            exclude 'META-INF/services/javax.annotation.processing.Processor'
        }
    }


    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.squareup.okhttp:okhttp:2.3.0'
        compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
        compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
        compile 'io.realm:realm-android:0.87.2'
    }

我在 StackOverflow 中找到了很多答案,并且我测试了其中的很多,但我没有成功。试过这个answer 和这个answer 但我认为我的问题更具体。

我还添加了 LogCat 错误的建议,但我得到了同样的错误。任何答案将不胜感激。

为了更好看,我提供了完整错误的图像。

【问题讨论】:

  • 您是否尝试按照错误消息中的说明进行操作? “您可以在 build.gradle 中忽略这些文件...”?
  • @CommonsWare 是的,我添加了指令,就像它在正确的 android 标签中显示的那样,` exclude 'META-INF/services/javax.annotation.processing.Processor'`。我不知道我是否必须忽略其他文件。
  • 您可能希望发布完整的build.gradle 文件。
  • @CommonsWare 我编辑并添加了完整的 gradle 文件
  • 尝试将android 闭包移到dependencies 闭包之后。

标签: android gradle realm


【解决方案1】:

android 闭包移到dependencies 闭包之后:

apply plugin: 'com.android.library'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.okhttp:okhttp:2.3.0'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    compile 'io.realm:realm-android:0.87.2'
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

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

    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

LogCat 显示此错误 Gradle DSL method not found: 'packagingOptions()'

那么你在android里面没有packagingOptions

【讨论】:

  • 它有效,问题是因为我有两个模块。模块 A 是可执行文件,模块 B 是库,我在模块 B 中添加了 packagingOptions 块,所以当我从模块 B 中删除块并添加模块 A 时,这仍然显示相同的错误。非常感谢。
【解决方案2】:

这是packagingOptions {,到目前为止,我通常能够构建任何东西。

android {
    ...
    packagingOptions {
        // Exclude file to avoid
        // Error: Duplicate files during packaging of APK
        exclude 'META-INF/DEPENDENCIES'
        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/ASL2.0'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-30
    • 2014-04-23
    • 2016-06-19
    • 1970-01-01
    • 2014-08-11
    • 2015-12-20
    相关资源
    最近更新 更多