What's in the Android Toolbox

What's in the Android Toolbox

A look into the Android build tools. How are Android apps built? What's new in the Android build tools?

108056ccba92f98fdbbabad534537573?s=128

Bryan Herbst

February 10, 2018
Tweet

Transcript

  1. What’s in the Android Toolbox? Bryan Herbst (@bryancherbst)

  2. What tools?

  3. $ sdkmanager --list

  4. $ sdkmanager --list Tools

  5. $ sdkmanager --list Tools Platform-Tools

  6. $ sdkmanager --list Tools Platform-Tools Build-Tools

  7. SDK tools

  8. $ ls $ANDROID_HOME/tools

  9. $ ls $ANDROID_HOME/tools android emulator monitor

  10. $ ls $ANDROID_HOME/platform-tools

  11. $ ls $ANDROID_HOME/platform-tools adb fastboot systrace

  12. $ ls $ANDROID_HOME/build-tools/27.0.3

  13. $ ls $ANDROID_HOME/build-tools/27.0.3 aapt apksigner dx

  14. What’s in an APK?

  15. APK ≈ JAR ≈ Zip

  16. APK META_INF/ APK metadata

  17. APK META_INF/ lib/ Native libs

  18. APK META_INF/ lib/ res/ Static resources (PNGs, XML layouts, etc.)

  19. APK META_INF/ lib/ res/ assets/ Static assets (fonts, HTML, etc.)

  20. APK META_INF/ lib/ res/ assets/ AndroidManifest.xml Hey, this looks familiar!

  21. APK META_INF/ lib/ res/ assets/ AndroidManifest.xml classes.dex Your code

  22. APK META_INF/ lib/ res/ assets/ AndroidManifest.xml classes.dex resources.arsc Compiled resources

    (strings, dimens, etc.)
  23. Dex?

  24. Code (.c, etc) Machine code

  25. Code (.c, etc) Machine code (arm) Machine code (x86) Machine

    code (mips)
  26. Java

  27. Java (.java) Bytecode (.class) Machine code

  28. Java (.java) Bytecode (.class) Machine code Ahead-of-time

  29. Java (.java) Bytecode (.class) Machine code Just in time

  30. Android

  31. Ahead-of-time Java (.java) Bytecode (.class) Machine code Dex Bytecode (.dex)

  32. Just-in-time Java (.java) Bytecode (.class) Machine code Dex Bytecode (.dex)

    Dalvik
  33. Ahead-of-time Java (.java) Bytecode (.class) Machine code Dex Bytecode (.dex)

    ART
  34. Ahead-of-time (on-device) Java (.java) Bytecode (.class) Machine code Dex Bytecode

    (.dex) ART
  35. Ahead-of-time (on-device) (with JIT) Java (.java) Bytecode (.class) Machine code

    Dex Bytecode (.dex) ART
  36. Compile Transform Package

  37. Compile Transform Package .java à .class

  38. Compile Transform Package .classà .dex

  39. Compile Transform Package everything à .apk

  40. Shift from generic tools to Android-specific tools

  41. incrementality parallelism

  42. Gradle The orchestrator

  43. By default Gradle does nothing

  44. Plugins add functionality

  45. apply plugin: 'com.android.application' Android functionality

  46. apply plugin: 'com.android.application' apply plugin: 'java' Java functionality

  47. apply plugin: 'com.android.application' apply plugin: 'java' apply plugin: 'kotlin' Kotlin

    functionality
  48. Plugins can extend the DSL

  49. Plugins can add dependency configurations

  50. Plugins can add tasks

  51. Android Plugin 3.x & Gradle 4.x

  52. compile Configurations

  53. compile testCompile Configurations

  54. compile testCompile debugCompile Configurations

  55. compile testCompile debugCompile apk Configurations

  56. compile testCompile debugCompile apk provided Configurations

  57. compile testCompile debugCompile apk provided Configurations

  58. api Available at compile time and runtime Configurations

  59. api implementation Available at compile time, Only available to consumers

    at runtime Configurations
  60. api implementation compileOnly Only available at compile time Configurations

  61. api implementation compileOnly runtimeOnly Only available at runtime Configurations

  62. Lib B Lib A App compile ':libB'

  63. Lib B Lib A App compile ':libB'

  64. Lib B Lib A App compile ':libB'

  65. Lib B Lib A App compile ':libB'

  66. Lib B Lib A App implemenation ':libB'

  67. Lib B Lib A App implemenation ':libB'

  68. Lib B Lib A App implemenation ':libB'

  69. And so much more

  70. Compile .java à .class

  71. Before we compile…

  72. Code Generation Dagger Butterknife Databinding

  73. buildConfigField "String", "FOO", '"bar"'

  74. resValue "string", "FOO", "bar"

  75. Compile javac / kotlinc

  76. Transforms .class à .dex

  77. Java 8 bytecode Java 6/7 bytecode

  78. Java 8 bytecode Java 6/7 bytecode Bytecode Optimized bytecode

  79. Java 8 bytecode Java 6/7 bytecode Bytecode Optimized bytecode Java

    bytecode Dex bytecode
  80. import com.android.build.api.transform.Transform

  81. import com.android.build.api.transform.Transform class RetrolambdaTransform : Transform { }

  82. app/build ├── generated ├── intermediates │ ├── classes │ └──

    transforms │ ├── dex[...] │ ├── mergeJavaRes │ ├── profilers-transform │ └── proguard └── outputs └── apk
  83. DX and D8 The dexing duo

  84. DX is the current dexing tool

  85. $ javac Main.java $ dx --dex --output=. Main.class

  86. dexdump lets you disassemble dex files

  87. $ dexdump –d classes.dex

  88. [000148] Main.main(String) 0000: sget-object v0, System;.out:PrintStream; 0002: const-string v1, "Hello

    World" 0004: invoke-virtual {v0, v1}, println:(String)V 0007: return-void
  89. [000148] Main.main(String) 0000: sget-object v0, System;.out:PrintStream; 0002: const-string v1, "Hello

    World" 0004: invoke-virtual {v0, v1}, println:(String)V 0007: return-void
  90. D8 is the next-gen dexing tool

  91. Faster compilation!

  92. Faster compilation! Smaller dex files!

  93. Faster compilation! Smaller dex files! R8 integration!

  94. gradle.properties android.enableD8=true

  95. Default in Studio + Gradle Plugin 3.1

  96. Desugar Artificial Java sweetener

  97. Retrolambda

  98. Retrolambda

  99. Retrolambda Jack

  100. Retrolambda Jack

  101. Retrolambda Jack Desugar

  102. Retrolambda Jack Desugar D8

  103. Jack and Jill Dead tools

  104. Shrink + Obfsucate ProGuard and R8

  105. Activity Class A Class B

  106. Activity Class A Class B

  107. Activity Class A

  108. A B

  109. ProGuard The old guard

  110. ProGuard is an open-source Java shrinker, obfuscation, and optimization tool

  111. minifyEnabled true proguardFiles 'proguard-rules.pro'

  112. -keep class com.model.ImportantData { *; }

  113. -keep class * extends android.view.View { <init>(...); }

  114. AAPT automatically generates ProGuard rules for Activities

  115. AAPT automatically generates ProGuard rules for Views

  116. AAPT automatically generates ProGuard rules for Services

  117. AAPT automatically generates ProGuard rules for ContentProviders

  118. AAPT automatically generates ProGuard rules for BroadcastReceivers

  119. ProGuard is an open-source Java shrinker, obfuscation, and optimization tool

  120. ProGuard is an open-source Java shrinker, obfuscation, and optimization tool

  121. Activity Drawable Class A

  122. Drawable Class A

  123. R8 The new (pro)guard

  124. R8 The new (pro)guard And D8

  125. R8 does full program Shrinking, obfuscation, and optimization

  126. Better incremental builds!

  127. Better incremental builds! New DSL!

  128. Not available yet :(

  129. Package Making the APK

  130. A A P T

  131. Android Asset Packaging Tool

  132. AAPT packages, reads, and updates APKs

  133. AAPT also…

  134. AAPT also… Processes resources

  135. AAPT also… Processes resources Generates R class

  136. AAPT also… Processes resources Generates R class Compiles resources

  137. AAPT2 Android Asset Packaging Tool 2

  138. AAPT2 is enabled by default as of plugin 3.0.0

  139. AAPT2 split linking and compilation

  140. AAPT2 is more restrictive

  141. <manifest> <application> <activity> <intent-filter /> <action android:name= "ACTION_NAME" /> </activity>

    </application> </manifest>
  142. <manifest> <application> <activity> <intent-filter /> <action android:name= "ACTION_NAME" /> </activity>

    </application> </manifest>
  143. APK Signing

  144. V1 signatures use jarsigner

  145. V2 signatures use apksigner

  146. Whole-file signature https://source.android.com/security/apksigning/v2

  147. Gradle Plugin 2.2+ defaults to v2 signatures

  148. Zipalign

  149. Zipalign aligns APKs on 4-byte boundaries

  150. Memory CPU

  151. Memory CPU Data

  152. Memory CPU Data

  153. Memory CPU

  154. Memory CPU Data

  155. Memory CPU Data

  156. Memory CPU Data

  157. Memory CPU Data

  158. Memory CPU Data

  159. V1 signature: After signing V2 signature: Before signing

  160. apkzlib

  161. Packing tools aapt apksigner zipalign

  162. Packing tools aapt apksigner zipalign Gradle Android Plugin apkzlib

  163. Post-build

  164. ART Android Runtime

  165. Ahead-of-time (on-device) (with JIT) Java (.java) Bytecode (.class) Machine code

    Dex Bytecode (.dex)
  166. Compiled APK (.dex)

  167. Compiled APK (.dex) optimize (dex2oat) Optimized APK (.dex, .oat)

  168. Compiled APK (.dex) optimize (dex2oat) Optimized APK (.dex, .oat) JIT

    Profile
  169. Compiled APK (.dex) optimize (dex2oat) Optimized APK (.dex, .oat) JIT

    Profile optimize (dex2oat) Optimized .oat
  170. Compiled APK (.dex) optimize (dex2oat) Optimized APK (.dex, .oat) JIT

    Profile optimize (dex2oat) Optimized .oat
  171. Compiled APK (.dex) optimize (dex2oat) Optimized APK (.dex, .oat) JIT

    Profile optimize (dex2oat) Optimized .oat Android 5.0
  172. Compiled APK (.dex) optimize (dex2oat) Optimized APK (.dex, .oat) JIT

    Profile optimize (dex2oat) Optimized .oat Android 7.0
  173. State of the ART

  174. State of the ART Interpreted (.dex)

  175. State of the ART Interpreted (.dex) JIT Compiled (in memory)

  176. State of the ART Interpreted (.dex) JIT Compiled (in memory)

    AOT compiled (.oat)
  177. Tools: android.googlesource.com/platform/tools/base ART, APK Signing: source.android.com/ R8 + D8: r8.googlesource.com

  178. https://goo.gl/cMsvmf @bryancherbst