InstantRun

Instant Run – Android

Here is all about “Instant Run”. The most requested feature from android developers is to improve the performance of deploying and running android apps from android studio to phones/ emulators. Finally, Google released a new feature which would almost increases this process 50 times faster than it was and named it “Instant Run”.

Though it has few limitations and known issues, it allows you to make changes in your code and see them running on the emulator or device much more quickly. Google released this feature in Android Studio 2.0 preview release. Start downloading and installing Android Studio 2.0 from Canary Channel.

Easiest way to try out Instant Run is to try it with one of Android code samples. Choose

 

instant_run_01

 

instant_run_02

Then choose any one to import from the list of sample codes. Lets go with FloatingActionButtonBasic example.

Once the sample is imported as project, as this is existing project which is not created now, we need to configure “Instant Run” feature manually. Go to Settings to enable this feature.

In Mac, Click Android Studio -> Preferences

In Windows, File -> Settings

 

In Settings/Preferences, go to Build, Execution, Deployment, under it you will find Instant Run feature. This will require a new version of Gradle plugin for the project. The link to update the project is provided for the same.

instant_run

After updating the Gradle plugin for the project, it will use a new faster version of dex, which helps both instant run and a full build be a bit faster. The classpath in dependences of Gradle file will have the updated gradle plugin com.android.tools.build:gradle:2.0.0-alpha1.

Now your project is ready to use the Instant Run feature. Click on “Run” to run the project in your device or emulator. The limitation of this feature is, your project should have min API level as icecream sandwich or above.

 

instant_run_1

 

Click on the two Floating Action buttons, you will see that they currently toggle states and log the toggle events. You can see the events by clicking “SHOW LOG” near the upper right of the emulator.

As now we have android app running, to experience the feature of Instant Run, make changes in the code to reflect instantly on the device or emulator.

Go to Application\src\main\java\com\example\android\

floatingactionbarbuttonbasic\FloatingActionButtonBasicFragment.java

Add the below highlighted line

@Override

public void onCheckedChanged(FloatingActionButton fabView, boolean isChecked) {

  // When a FAB is toggled, log the action.

  switch (fabView.getId()){

      case R.id.fab_1:

Toast.makeText(getActivity(), "Instant Run rocks!", Toast.LENGTH_SHORT).show();
          Log.d(TAG, String.format("FAB 1 was %s.", isChecked ? "checked" : "unchecked"));

          break;

      case R.id.fab_2:

          Log.d(TAG, String.format("FAB 2 was %s.", isChecked ? "checked" : "unchecked"));

          break;

      default:

          break;

  }

}
import android.widget.Toast

 

Now again click on the “Run” in the toolbar. You will notice that the build is completed almost instantaneously and the emulator shows a brief message telling you that the code change has been successfully applied.Try clicking on the first Floating Action button. You will see that the code change you made is already running!

instant_run_2

 

Below are the changes that are supported by Instant Run

Code Change

Instant Run Support

Change instance method implementation

Change static method implementation

Add or remove a class

Supported
Add, remove, or change a string resource Supported but requires an Activity restart.

 

Here are some code changes that Instant Run does not currently support:

  • Add/remove/change annotations

  • Add/remove/change an instance field

  • Add/remove/change a static field

  • Add/remove a static method signature

  • Change a static method signature

  • Add/remove an instance method

  • Change an instance method signature

  • Changing which parent class the current class inherits from

  • Change the list of implemented interfaces

  • Changing static initializer of a class

 

Here are also some known issues that we are working on:

  • Apps that are using legacy multi-dex (with minSdkVersion < 21), with a main dex file near the 65K method limit may not build. This would require reworking the app to make room in the main dex list by reducing the number of classes required in the main dex file.

  • As of google, Instant Run was tested on many devices, but as mentioned by them it’s impossible to try it on every single device model.

  • Some intermittent issues where the IDE loses connection with the app which will trigger a full rebuild.

  • This feature is not tested with 3rd party Gradle plugins, especially those that have not been updated to use the new transforms API.

  • Data-binding is broken in this build.

 

You can use this link to report to google if you face any bug.

Download this tutorial

373 total views, 1 views today

Leave a Reply