Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: App crashes frequently with instant run

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?

like image 914
Lahiru Chandima Avatar asked Jun 24 '16 02:06

Lahiru Chandima


People also ask

Why do my Apps keep crashing immediately?

Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.


3 Answers

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.

like image 190
Youngjae Avatar answered Oct 12 '22 22:10

Youngjae


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:

  1. Open the Settings or Preferences dialog.
  2. Navigate to Build, Execution,Deployment > Instant Run.
  3. Uncheck the box next to Enable Instant Run.
like image 25
ישו אוהב אותך Avatar answered Oct 12 '22 23:10

ישו אוהב אותך


This is document about Instant Run:

  1. 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.
  2. 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

like image 1
Fuyuba Avatar answered Oct 12 '22 21:10

Fuyuba