Instant Run (Android Studio 2.0)


Google Post: About Instant Run

Android Studio 2.0에 Instant Run이라는 새로운 기능이 추가되었다.
전체를 compile해서 APK를 생성하는것이 아닌 변경된 Dex파일만을 컴파일해서 reload하는 방식이다.
당연히 기존 방법보다 더 빠른 재실행 시간을 보장한다.

사용을 위해서는 다음의 조건을 만족 해야 한다.

  • Android Studio to 2.0 or higher
  • Android Plugin for Gradle version 2.0.0 or higher
  • minSdkVersion to 15 or higher. 최대의 성능을 위해서는 API level 21 이상을 추천한다

설정이 정상적으로 이뤄지면 노란색 tunderbolt icon이 Run 과 Debug 버튼 위에 나타나게 된다.
이 뜻은 다음 부터는 새로운 APK를 빌드하는 대신에 새롭게 변경된 부분은 푸쉬하겠다는 뜻이다.

발생된 코드와 resource 변화에 대해서 어떻게 반영할지에 대해서는 hot swapwarm swapcold swap으로 구분 한다.
각각의 swap타입의 결정은 자동으로 일어난다.
자세한 정보는 Google Deverloper's Video를 참조하자.

  • hot swap: method call과 관련됨. Object를 재 초기화 하지 않는다.
Change implementation code of an existing method
  • warm swap: 현재 activity만을 restart한다. resource 변화를 반영한다. 약간의 깜빡임(flicker) 정도의 screen 변화로 변경사항이 반영 되게 된다.
Change or remove an existing resource
  • cold swap: API level 21 이상에서 동작한다. 상대적으로 가장 느린 swap이다. 비록 새로운 APK를 생성하는 것은 아니지만, 전체적으로 code structure가 변경 되었다면 App을 restart해야되기 때문이다. 그마져도, API level 20 이하라면 full APK build 과정도 발생 시키므로 사실상 Instant Run의 이점은 사라진다.
Structural code changes, such as:
    Add, remove, or change:
        an annotation
        an instance field
        a static field
        a static method signature
        an instance method signature
    Change which parent class the current class inherits from
    Change the list of implemented interfaces
    Change a class's static initializer
    Reorder layout elements that use dynamic resource IDs 
  • manifest 또는 resources referenced by the manifest를 변경할경우에는 자동으로 deploys a new build를 실행 한다. 이러한 이유는app nameicon resources, and intent filters같은 경우가 manifest에서 변경되었다면 APK자체를 새로 설치해서 Android OS에 등록 시켜야 하기 때문이다.
Change the app manifest
Change resources referenced by the app manifest
Change an Android widget UI element (requires a Clean and Rerun)

UI widget을 변경했다면 Clean and Rerun을 실행해야 한다. 왜냐하면 onCreate()를 새로 실행 시켜야 하기 때문이다.
메뉴는 Run > Clean and Rerun 'app`에 위치한다.

실행 방법

  1. File > Settings
  2. Build, Execution, Deployment > Instant Run
  3. 아래와 같이 모두 체크한다. 

이렇게 모두 설정하면 Run 버튼이 와 같이 번개 모양이 뒤에 달리게 된다.

실제 간단한 Toast message로 테스트 해보면 Instant Run을 할때에는 2초 이내로 실행 되고
Restart App 기존 방식으로하면 20초 이내로 걸리게 된다.
많은 변화라고 할 수 있다.


+ Recent posts