Slide 1

Slide 1 text

How Instant Run works Atsushi Eno (@atsushieno)

Slide 2

Slide 2 text

Instant Run is About Faster Development Write Code Build Deploy Debug

Slide 3

Slide 3 text

Android Build Basics

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

FD

Slide 11

Slide 11 text

Fast Deployment What needs improvements: ◇ Reduce APK Installation occurrences. ◇ Reduce task execution as much as we can. To achieve that...

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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!

Slide 15

Slide 15 text

Lessons Learned - Be careful when designing adding build tasks. - Data Binding - Kotlin - App bootstrap difference may trigger some problems. - PackageManager - Instrumentation

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Instant

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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)

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

Thanks Android Studio 2.0のInstant Runの仕組みを解読する (In lengthy text) http://qiita.com/atsushieno/items/141f790a9bbaa93b62c1