【问题标题】:Android DuplicateFileException when using RealmSearchView使用 RealmSearchView 时出现 Android DuplicateFileException
【发布时间】:2016-12-14 20:37:55
【问题描述】:

我正在尝试实现 RealmSearchView (link here)。但问题是,当我将它添加到我的依赖项时,我收到以下错误:

    Error:Execution failed for task ':app:transformNative_libsWithMergeJniLibsForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK lib/x86/librealm-jni.so
    File1: C:\Users\Username\.gradle\caches\modules-2\files-2.1\io.realm\realm-android\0.82.2\c6325383149a08e5cb668bdc4d3c790708636e47\realm-android-0.82.2.jar
    File2: D:\MyApp\AppName\app\build\intermediates\exploded-aar\io.realm\realm-android-library\2.2.1\jni

我的依赖如下:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.android.gms:play-services:9.8.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.google.firebase:firebase-core:9.8.0'
    compile 'com.google.firebase:firebase-messaging:9.8.0'
    compile 'com.android.support:design:24.2.1'
    compile 'com.github.thorbenprimke:realm-searchview:0.9.1' 
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    testCompile 'junit:junit:4.12'
}

要添加 Realm,我遵循了他们的教程 (here),我还遵循了 RealmSearchView 教程 (here) 来添加它。但他们似乎有冲突?

如何在项目中同时包含这两个文件,但又不创建这些重复文件?

编辑: 这是我为了让它运行而添加的:

packagingOptions {
        pickFirst 'lib/x86/librealm-jni.so'
        pickFirst 'lib/arm64-v8a/librealm-jni.so'
        pickFirst 'lib/armeabi-v7a/librealm-jni.so'
        pickFirst 'lib/mips/librealm-jni.so'
    }

但是,现在我收到了这个错误(现在是不是使用了错误的 Realm 版本?)

E/UncaughtException: java.lang.NoSuchMethodError: No static method init(Landroid/content/Context;)V in class Lio/realm/Realm; or its super classes (declaration of 'io.realm.Realm' appears in /data/data/uk.tether.topcom.tether/files/instant-run/dex/slice-realm-android-0.82.2_f252e1b50f018c2840011d6a94a195daa3c99f49-classes.dex)
                                                                                    at uk.abc.def.ghi.RealmUtilities.RealmManagement.intialiseRealmDatabase(RealmManagement.java:20)
                                                                                    at uk.abc.def.ghi.SplashScreenActivity.onCreate(SplashScreenActivity.java:63)
                                                                                    at android.app.Activity.performCreate(Activity.java:5958)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:155)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

这会破坏此处的“init”方法调用:

public void intialiseRealmDatabase(Context context){
        Realm.init(context);
    }

编辑 2: 原来 Realm 搜索控制器教程 (here) 中使用的 RealmSearchView 版本已经过时了。应该是0.9.6版!

【问题讨论】:

    标签: java android dependencies realm


    【解决方案1】:

    您对 compile 'com.github.thorbenprimke:realm-searchview:0.9.1' 的依赖引用了 Realm 版本 0.82.2,而您还包括 Realm 2.1。

    您应该将依赖项删除到realm-searchview,因为它已过期,否则您需要复制存储库并自行更新并手动将其添加到您的项目中。

    如果它支持最新的 Realm 版本,或者只是将其更新到较新的版本。

    【讨论】:

    • 是的!在您发布之前,我确实改变了这一点。我意识到我使用的是 0.9.1,而我应该使用的是 0.9.6!
    【解决方案2】:
    packagingOptions {
            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'
            pickFirst 'lib/x86/librealm-jni.so'
    
        }
    

    在你的 gradle 文件中使用这一行会对你有所帮助。

    【讨论】:

    • 错误现在显示:“在 APK lib/arm64-v8a/librealm-jni.so 中复制的文件重复”,我需要先为每个文件选择一个吗?
    • 我已经添加了运行应用程序所需的所有 pickFirst,但现在是 'Realm.init(context);'未被识别为现有方法调用?
    • 查看我添加的编辑。它可以运行,但我现在不再拥有某些 Realm 方法
    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多