Not able to use RealmAdapter - java.lang.NoClassDe

2019-09-10 12:41发布

问题:

I am trying to use Realm Adapter in one of my projects. However I am not able to use it.

When ever I add compile 'io.realm:android-adapters:1.0.1' into gradle file. Project gets compiled successfully, but the app crashes on tabLayout.setupWithViewPager(viewPager); -- the line where I am adding viewpager to tabLayout.

App runs successfully when I comment compile 'io.realm:android-adapters:1.0.1'

I am attaching the screenshot of the activiy where its crashing. Its crashing on line 57. I have also pasted the stacktrace of the error

05-09 22:11:48.074 16932-16932/com.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                         Process: com.app, PID: 16932
                                                         java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/widget/TintManager;
                                                             at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1185)
                                                             at android.support.design.widget.TabLayout.createTabView(TabLayout.java:656)
                                                             at android.support.design.widget.TabLayout.addTabView(TabLayout.java:695)
                                                             at android.support.design.widget.TabLayout.addTab(TabLayout.java:386)
                                                             at android.support.design.widget.TabLayout.addTab(TabLayout.java:361)
                                                             at android.support.design.widget.TabLayout.setTabsFromPagerAdapter(TabLayout.java:645)
                                                             at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:616)
                                                             at com.app.MainActivity.onCreate(MainActivity.java:57)
                                                             at android.app.Activity.performCreate(Activity.java:6251)
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                             at android.os.Looper.loop(Looper.java:148)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                          Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.TintManager" on path: DexPathList[[zip file "/data/app/com.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.app-1/lib/arm64, /data/app/com.app-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]
                                                             at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                             at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1185) 
                                                             at android.support.design.widget.TabLayout.createTabView(TabLayout.java:656) 
                                                             at android.support.design.widget.TabLayout.addTabView(TabLayout.java:695) 
                                                             at android.support.design.widget.TabLayout.addTab(TabLayout.java:386) 
                                                             at android.support.design.widget.TabLayout.addTab(TabLayout.java:361) 
                                                             at android.support.design.widget.TabLayout.setTabsFromPagerAdapter(TabLayout.java:645) 
                                                             at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:616) 
                                                             at com.app.MainActivity.onCreate(MainActivity.java:57) 
                                                             at android.app.Activity.performCreate(Activity.java:6251) 
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                             at android.os.Looper.loop(Looper.java:148) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                            Suppressed: java.lang.ClassNotFoundException: android.support.v7.widget.TintManager
                                                             at java.lang.Class.classForName(Native Method)
                                                             at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                             at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                    ... 21 more
                                                          Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Here is the snippet of build.gradle file from the app -

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.rengwuxian.materialedittext:library:2.1.4'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.github.paolorotolo:appintro:3.2.0'
compile 'io.realm:android-adapters:1.0.1'

}

回答1:

All support libs must have the same version. 23.1.0 or 23.1.1 or any other but you can't use different versions.

Try to use 23.3.0 for all libs.

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.rengwuxian.materialedittext:library:2.1.4'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.github.paolorotolo:appintro:3.2.0'
compile 'io.realm:android-adapters:1.0.1'

More reference



回答2:

io.realm:android-adapters:1.0.1 requires appcompat-v7 version 23.3.0 or later.

By the way, I recommend to use a variable for the support version. (It should be the same across all support libraries.)

def supportVersion = '23.3.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportVersion"
compile "com.android.support:design:$supportVersion"
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.rengwuxian.materialedittext:library:2.1.4'
compile "com.android.support:cardview-v7:$supportVersion"
compile 'com.github.paolorotolo:appintro:3.2.0'
compile 'io.realm:android-adapters:1.0.1'