java.lang.NoClassDefFoundError,选择第三方库还需多注意

java.lang.NoClassDefFoundError,选择第三方库还需多注意

四月 27, 2017
1
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHelper;

看到 NoClassDefFoundError 的时候,我就在想,以前还好好的,怎么就不行了呢?
然后看了看 External Libraries 列表,support 包版本怎么是 26.0.0-alpha1,我明明写的是 24.2.1 啊。

解决方案:

查看全部依赖都引用了哪些依赖

1
gradlew -q dependencies app:dependencies --configuration compile

1
2
3
4
5
6
+--- cn.xxx.xxx:1.x.x
| +--- cn.xx.xx:xx:1.x.x
| | +--- com.android.support:support-v4:latest.release -> 26.0.0-alpha1 (*)
| | \--- com.android.support:support-annotations:latest.release -> 26.0.0-alpha1
| +--- com.android.support:support-v4:latest.release -> 26.0.0-alpha1 (*)
| \--- com.android.support:support-annotations:latest.release -> 26.0.0-alpha1

其中看到有个是这样的,直接 latest.release ,坑啊,我自己是引用 24.2.1 版本的,24 和 26 版本相差不小(移除了类是很有可能的),
所以 sync project 后,项目会自动依赖到最高版本。

知道原因就好办了,直接排除这个库的 support 引用:

1
2
3
compile('cn.xx.xx:xx:1.x.x', {
exclude group: 'com.android.support'
})