How Instant Run works @DroidKaigi2016

24837993455f54c957883ba1f1db7f2d?s=47 Atsushi Eno
February 18, 2016

How Instant Run works @DroidKaigi2016

24837993455f54c957883ba1f1db7f2d?s=128

Atsushi Eno

February 18, 2016
Tweet

Transcript

  1. How Instant Run works Atsushi Eno (@atsushieno)

  2. Instant Run is About Faster Development Write Code Build Deploy

    Debug
  3. Android Build Basics

  4. Instant Run is only About Debug Build Type Debug Release

    Exec. Environment only developer anywhere Build Frequency very often few times Build Perf. Requirement should be fast doesn't matter App Size doesn't matter should be small
  5. Android Debug Build ◇ can Package only for Developer's Device

    ◇ can Skip Proguard ◇ can Copy and Install Any Support Files on Developer's Device cf. iOS debug build
  6. What is a "Build System"? ◇ describe a set of

    Tasks to get build Outputs ■ from build Inputs ■ (each Task has Inputs and Outputs) ◇ describe Dependencies of each build Task. ◇ Tasks Cache their Outputs and Skip execution, if not needed. ■ should be carefully implemented. [example bug] ◇ deployment can also be a part of the tasks. make, cmake, rake, ant, gradle, gyp, msbuild...
  7. APK: Android Build Output ◇ AndroidManifest.xml ◇ App Code as

    Dalvik Bytecode (classes.dex) ◇ Compiled and Zipped Resources and Assets (resources- debug.ap_) ◇ Native Libraries (libFooBarBaz.so) APK build takes ALL of those as Inputs. Any changes on them will trigger the build and update APK.
  8. Android Build Tasks and events ◇ aapt to process res.,

    9-patch, generate R.java ◇ javac (*.java -> *.class) ◇ proguard, if used ◇ scan all *.class-es for multidex.keep, if used ◇ dx (*.class, *.jar -> *.dex) ◇ zip and zipalign into .apk ◇ sign .apk *.java *.class classes.dex *.jar multidex.keep res/* assets resources-debug.ap_ patched res/* R.java Foo -signed .apk AndroidManifest.xml aapt javac (proguard) (multidex) dx
  9. Android Deployment Tasks and events ◇ Transmit APK to the

    Device (via TCP, for emulator) ◇ Uninstall Previously Installed APK, if any ◇ the Device will... ■ Create and Adjust Permissions for the app. ■ Run `dex2oat` for ART Device.
  10. FD

  11. Fast Deployment What needs improvements: ◇ Reduce APK Installation occurrences.

    ◇ Reduce task execution as much as we can. To achieve that...
  12. Write Code Compile One-Time APK Setup Package Push APK Push

    dex/res Debug ◇ install APK only once (only when required) ◇ While debugging, push only code and data to somewhere. 1st. 2nd. ...
  13. Altered Application ◇ AndroidManifest is injected to use BootstrapApplication ◇

    monkey patching ■ replaces the path to dex, in app data path {ApplicationInfo.datadir}/files/studio-fd/dex/slice-*/*.dex ■ IncrementalClassLoader loads .dex-es from there ■ replaces the path to res and native libs similarly ■ using non-public Android API via reflection...
  14. Split Dex Uploads Keep changes and uploaded units as small

    as possible. ◇ APP code -> small, frequently changed ◇ Library jars -> big, rarely changed ◇ Non-Instant-Build: everything is in one classes.dex. ◇ Instant Build ■ libraries are compiled to one different .dex. ■ Dex Sharding: split .dex to multiple files (default: 10) ■ Skip .dex-ing / uploading / dex2oat if input is older!
  15. Lessons Learned - Be careful when designing adding build tasks.

    - Data Binding - Kotlin - App bootstrap difference may trigger some problems. - PackageManager - Instrumentation
  16. Bazel? https://github.com/bazelbuild/bazel/tree/master/src/tools/android/java/com/google/devtools/build/android/incrementaldeployment http://bazel.io/docs/mobile-install.html

  17. Instant

  18. Launch App Navigate to target Activity Launch target Activity Operate

    "Instant" Run Basics Hot Swap Warm Swap Cold Swap ◇ Run code without Restarting App ◇ or, even without Restarting Activity
  19. Detecting Changes ◇ Hot Swap: only when Java classes structures

    haven't changed ◇ Warm Swap: need to identify which resource values matter ◇ Changes to Manifest or referenced values: reinstallation There is some Gradle Task that scans changes and detects them. If it is buggy, Instant Run misbehaves. (InstantRunVerifier.java in Gradle 2.0 source tarball)
  20. Instant: past, present and future? ◇ "Inventing on Principle" (2012)

    → Xcode Playground ◇ "Making Instant C# Viable" (2012) → Xamarin Sketches ◇ Continuous Coding (2015) "The Evolution of Interactive C#" -> Change app code and see results instantly.
  21. Thanks Android Studio 2.0のInstant Runの仕組みを解読する (In lengthy text) http://qiita.com/atsushieno/items/141f790a9bbaa93b62c1