升级到2.2.1版本后,莫名其妙的发现一个关于InstantRun的bug,而且是必现的bug,本来想直接通过Android Studio的自带反馈入口提交给Google的,但是即使开了ld,依然无法提交成功,只能放弃了,但是还是要记录一下,给遇到的小伙伴提个醒。
需要注意的是,每个人的开发环境都不尽相同,不能保证每个人遇到的情况都适用,关于InstantRun的bug的描述,仅供参考。
虽然Google在2.2.1版本极力推荐开发者打开InstantRun功能,默认的设置也是打开InstantRun的,但是InstantRun还是不太完善,很多开发者还是主动关闭这个功能。本人在平时开发的时候是打开的,对于比较大的项目来说,这个功能还是挺不错的功能,当然也会遇到一些问题,导致编译运行不正常,这种情况下,就手动关闭InstantRun,然后再打开。虽然有点麻烦,但是本人接受。
打开InstantRun功能
创建一个新的工程
用Android Studio2.2.1创建一个新的工程,当然,以前创建好的也可以,这个时候项目的包路径是这样com/a/b,applicationId为:com.a.b;修改Package Name
但是由于某种原因,这个包路径需要改成:com/c/d,这时,按照普通的修改包名的方法来修改这个包名即可,改完包名,再修改applicationId为com.c.d,同步,Rebuild,没有问题。运行新的工程
不管是clean还是rebuild都是没有问题的,这时运行会出错,错误信息如下:
10-18 16:29:31.692 26030-26030/? I/art: Late-enabling -Xcheck:jni10-18 16:29:31.694 26030-26030/? D/Zygote: zygotejni setunlimit sucess pid==2603010-18 16:29:31.709 26030-26038/? I/art: Debugger is no longer active10-18 16:29:31.722 26030-26030/? W/System: ClassLoader referenced unknown path: /data/app/com.example.csy.myapplication2-1/lib/arm6410-18 16:29:31.724 26030-26040/? I/System: FinalizerDaemon: finalize objects = 110-18 16:29:31.725 26030-26030/? I/InstantRun: Instant Run Runtime started. Android package is com.example.csy.myapplication, real application class is null.10-18 16:29:31.725 26030-26030/? E/InstantRun: Failed to create directory /data/data/com.example.csy.myapplication/files/instant-run/dex10-18 16:29:31.726 26030-26030/? E/InstantRun: Couldn't create dex code folder10-18 16:29:31.728 26030-26030/? W/InstantRun: No instant run dex files added to classpath10-18 16:29:31.731 26030-26030/? E/InstantRun: IO Error creating local socket at com.example.csy.myapplication java.io.IOException: Address already in use at android.net.LocalSocketImpl.bindLocal(Native Method) at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:306) at android.net.LocalServerSocket.<init>(LocalServerSocket.java:48) at com.android.tools.fd.runtime.Server.<init>(Server.java:94) at com.android.tools.fd.runtime.Server.create(Server.java:88) at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:359) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5131) at android.app.ActivityThread.-wrap2(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5905) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)10-18 16:29:31.732 26030-26030/? D/ActivityThread: BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{cde1f43 com.example.csy.myapplication2}}10-18 16:29:31.732 26030-26030/? V/ActivityThread: Handling launch of ActivityRecord{8ebe9c0 token=android.os.BinderProxy@82b36f9 {com.example.csy.myapplication2/com.example.csy.myapplication2.MainActivity}} startsNotResumed=false10-18 16:29:31.733 26030-26030/? D/AndroidRuntime: Shutting down VM10-18 16:29:31.733 26030-26030/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.csy.myapplication2, PID: 26030 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.csy.myapplication2/com.example.csy.myapplication2.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.csy.myapplication2.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.csy.myapplication2-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.csy.myapplication2-1/lib/arm64, /vendor/lib64, /system/lib64]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2570) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729)
异常信息显示MainActivity的ClassNotFoundException,但是这个MainActivity是绝对存在的,而且一切路径信息都是对的,完全可以找到,这个MainActivity也不是那个jar包里的Activity,也没有引用任何jar包:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.csy.myapplication2/com.example.csy.myapplication2.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.csy.myapplication2.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.csy.myapplication2-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.csy.myapplication2-1/lib/arm64, /vendor/lib64, /system/lib64]]
其实关键信息看这里:
10-18 16:29:31.722 26030-26030/? W/System: ClassLoader referenced unknown path: /data/app/com.example.csy.myapplication2-1/lib/arm64 10-18 16:29:31.724 26030-26040/? I/System: FinalizerDaemon: finalize objects = 1 10-18 16:29:31.725 26030-26030/? I/InstantRun: Instant Run Runtime started. Android package is com.example.csy.myapplication, real application class is null. 10-18 16:29:31.725 26030-26030/? E/InstantRun: Failed to create directory /data/data/com.example.csy.myapplication/files/instant-run/dex 10-18 16:29:31.726 26030-26030/? E/InstantRun: Couldn't create dex code folder 10-18 16:29:31.728 26030-26030/? W/InstantRun: No instant run dex files added to classpath 10-18 16:29:31.731 26030-26030/? E/InstantRun: IO Error creating local socket at com.example.csy.myapplication
找到关键字InstantRun:
Instant Run Runtime started. Android package is com.example.csy.myapplication, real application class is null.
这里说Android package is com.example.csy.myapplication,这明明就是我修改包名和applicationId之前的包名,修改后的是Android package is com.example.csy.myapplication2,Package是旧的Package,根本没改成新的Package。这就是问题所在,所以后面才有了这些输出:
10-18 16:29:31.725 26030-26030/? E/InstantRun: Failed to create directory /data/data/com.example.csy.myapplication/files/instant-run/dex 10-18 16:29:31.726 26030-26030/? E/InstantRun: Couldn't create dex code folder 10-18 16:29:31.728 26030-26030/? W/InstantRun: No instant run dex files added to classpath
创建路径失败,无法创建dex文件夹,最终导致ClassNotFoundException,为了验证是InstantRun引起的问题,关闭InstantRun再次运行,一切正常,再开启InstantRun,又是ClassNotFoundException,一模一样,不管怎么倒腾,怎么clean、Rebuild,清除缓存什么的,都不起作用。
同时,我在另一个demo上重新操作了一下修改包名的流程,现象也完全一样。
需要提醒的是,这样的问题只在本地出现,修改包名后提交到svn,别人更新下来后却没有问题。还有就是为什么说2.2.1呢?因为我换成2.2.0的话也是没有这个问题的,所以这个应该是2.2.1才有的bug。
现在出现了这个问题,只能是先把InstantRun关掉了,如果你也出现过这种问题,并且知道是怎么回事,烦请留言告知解决办法,谢谢。再有就是坐等Android Studio下次更新啦!!
================2016年11月4日13:35:47===============
最近升级的2.2.2版本依然没有修复该问题。
共同学习,写下你的评论
评论加载中...
作者其他优质文章