Android常见的异常之ClassNotFoundException--Didn't find class
2017-01-13 11:47
537 查看
两个可以完美运行的程序合在一起就会报以下的错误,彷徨了好久,终于迎来答案:
报的异常(还有很多相似的异常,这里只截取了一部分)是:解决方案:
还是先解决打包问题,回头再研究那些高深的动态化加载技术。偷懒一下咯考虑到投入产出比,决定使用Google官方的multiDex解决。(Google的补丁方案啊,不会再有坑了吧?后面才发现还是太天真) 该方案有两步:
1.修改gradle脚本来产生多dex。
2.修改manifest 使用MulitDexApplication。
步骤1.在gradle脚本里写上:
android { compileSdkVersion 21 buildToolsVersion "21.1.0" defaultConfig { ... minSdkVersion 14 targetSdkVersion 21 ... // Enabling multidex support. multiDexEnabled true } ... } dependencies { compile 'com.android.support:multidex:1.0.0' }
步骤2. manifest声明修改
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.multidex.myapplication"> <application ... android:name="android.support.multidex.MultiDexApplication"> ... </application> </manifest>
如果有自己的Application,继承MulitDexApplication。如果当前代码已经继承自其它Application没办法修改那也行,就重写 Application的attachBaseContext()这个方法。
@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
run一下,可以了!但是dex过程好像变慢了。。。
文档还写明了multiDex support lib 的局限。瞄一下是什么:
1.在应用安装到手机上的时候dex文件的安装是复杂的(complex)有可能会因为第二个dex文件太大导致ANR。请用proguard优化你的代码。呵呵
2.使用了mulitDex的App有可能在4.0(api level 14)以前的机器上无法启动,因为Dalvik linearAlloc bug(Issue
22586) 。请多多测试自祈多福。用proguard优化你的代码将减少该bug几率。呵呵
3.使用了mulitDex的App在runtime期间有可能因为Dalvik linearAlloc limit (Issue
78035) Crash。该内存分配限制在 4.0版本被增大,但是5.0以下的机器上的Apps依然会存在这个限制。
4.主dex被dalvik虚拟机执行时候,哪些类必须在主dex文件里面这个问题比较复杂。build tools 可以搞定这个问题。但是如果你代码存在反射和native的调用也不保证100%正确。呵呵
感觉这就是个坑啊。补丁方案又引入一些问题。但是插件化方案要求对现有代码有比较大的改动,代价太大,而且动态化加载框架意味着维护成本更高,会有更多潜在bug。所以先测试,遇到有问题的版本再解决。
这种顽固没有头绪的bug是不好找,必须我们亲自遇见才能更好的解决它,希望能帮助和我有相同问题的同志。
详细可以访问:http://www.open-open.com/lib/view/open1452264136714.html
相关文章推荐
- 【异常处理】java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle"
- [Android]系统级ap的ClassNotFoundException: Didn't find class问题
- android 升级ADT到22后,出现bug: Could not find class,或者Caused by: java.lang.ClassNotFoundException:异常
- 【异常解决】android.content.ActivityNotFoundException: Unable to find explicit activity class
- 【异常处理已解决】java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle"
- Android ClassNotFoundException: Didn't find class on path xxxx
- Android开发之~java.lang.ClassNotFoundException: Didn't find class "android.view.x" on path: ...
- [置顶] Android开发之viewpager导报错误解决方法:错误代码 Caused by: java.lang.ClassNotFoundException: Didn't find class
- java.lang.ClassNotFoundException:Didn't find class "android.os.PersistableBundle" on path 奇葩BUG解决
- android 升级ADT到22后,出现bug: Could not find class,或者Caused by: java.lang.ClassNotFoundException:异常
- java.lang.ClassNotFoundException: Didn't find class "android.hardware.fingerprint.FingerprintManager
- Caused by: java.lang.ClassNotFoundException: Didn’t find class on path apk Android Studio解决方案
- Android开发Exception:ActivityNotFoundException: Unable to find explicit activity class
- android.content.ActivityNotFoundException: Unable to find explicit activity class 【安卓报错】
- Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class
- Android开展Exception:ActivityNotFoundException: Unable to find explicit activity class
- android studio运行程序找不到class,java.lang.ClassNotFoundException: Didn't find class,完美解决!
- 【Android 疑难杂症1】android.content.ActivityNotFoundException: Unable to find explicit activity class
- Caused by: java.lang.ClassNotFoundException: Didn't find class "net.oschina.app.AppContext" on path:
- java.lang.ClassNotFoundException: Didn't find class "com.mob.wenda.MainActivity"