问题
In my android app, I am debugging it with instant run enabled.
The app gives me unexpected errors very frequently, which gets resolved if I did a clean build.
For an example, there are sometimes class not found exceptions where the class is actually there. Also, sometimes there are class cast exceptions when trying to cast the View
returned by findViewById()
to the target type (probably android studio has misinterpreted the element IDs when hot deploying the layouts).
All these errors get resolved without no code change, just by doing a full build. Instant run has successfully made the build process two or three times slower for me.
Why am I getting these errors? Does anyone know how to get instant run to working properly?
回答1:
You are not the only one. Instant Run is little stable, or we can say unreliable, currently.
Instant Run has several swap steps as this document describes. In your case, Instant Run could not recognize your changes well, so did warm swap even it required cold swap, and so on.
Instant Run is useful when editting minor GUI placement, I want to say it is the only useful scenario currently, so I recommend you to turn it off during general development. You can make it disable by unchecking Settings > Build, Execution, Deployment > Enable Instant Run to hot swap...
checkbox.
If you think Instant Run is fancy and still want to use it, you will likely be faced such a problem many times. For that, just rebuild all as you experienced. That's all we can do now.
回答2:
Instant Run is still full of bugs now. Even google says that:
Instant Run is designed to speed up the build and deploy process in most situations. However, there are some aspects to using Instant Run that might affect its behavior and compatibility with your app. If you experience any other issues while using Instant Run, please file a bug
It add some dex files to our app to speed up the build and deploy process. Sometimes when changing a layout then Build and Run the project, it still showing the old layout one. Even the apk will produced by it will not run on some devices.
For now, better if we turn it off:
To disable Instant Run:
- Open the Settings or Preferences dialog.
- Navigate to Build, Execution,Deployment > Instant Run.
- Uncheck the box next to Enable Instant Run.
回答3:
This is document about Instant Run:
- Android Studio now deploys clean builds faster than ever before. Additionally, pushing incremental code changes to the emulator or a physical device is now almost instantaneous. Review your updates without redeploying a new debug build or, in many cases, without restarting the app.
- Instant Run supports pushing the following changes to a running app:
- Changes to the implementation of an existing instance method or static method
- Changes to an existing app resource
- Changes to structural code, such as a method signature or a static field (requires a target device running API level 21 or higher).
So I think when you add new class or add new view_id, you need to clean - rebuild your project. This is just my opinion, correct me if i'm wrong. More info about Instant Run
回答4:
If I change the AndroidManifest.xml
file, android studio performs a full build, but this is much faster to fist cleaning, and then building the project.
So, I add a blank line to the manifest file, whenever I want android studio to full build the project. This is much effective to perform a manual full build.
来源:https://stackoverflow.com/questions/38004581/android-app-crashes-frequently-with-instant-run