: If you're doing bytecode instrumentation as part of your Android build, or if you are providing plugins that do instrumentation, and you want it to work with Instant Run, you need
to switch to the Transform API.
While it's currently possible to add bytecode instrumentation to the build process, there is (was) no API for it. The general mechanism was to find the dx task, grab its inputs, and replace them with your own outputs. Then the tasks dependencies had to be rewired to include the new intermediary task.
This is problematic for several reasons:
- It's impossible to differentiate some of the inputs (sub-modules vs external libraries)
- Dealing with legacy multi-dex makes it harder as the computation of the main dex class list must also include the output of the instrumentation.
- combining more than one third-party instrumentation is fragile and prone to breakage.
On top of this, this will simply not work with Instant Run. While the original build of the APK does go through the regular build steps, the hotswap mode runs different tasks.
During a regular build, Instant Run adds the following steps:
- Instrumentation of the project classes to add an indirection for code replacement.
- Copy of the classes to compare later and produce the override class.
This must happen after
any third party instrumentation, which is not possible with old the mechanism of hooking up instrumentations (since they are added at the end just before dx).
During a hotswap build, Instant Run will run javac, and then compare the output to the previous build output to generate the override class(es). Then, it runs a different dx task that only dx the override classes and nothing else. This flows is very different from the regular build and the old mechanism of hooking up instrumentation would completely miss this.
To fix this, we created the Transform API. Its goal is to provide an API to hook up first and third party instrumentations into the build process. The API does not deal with Gradle tasks, but instead handles what needs to be instrumented, with the plugin doing the task instantiation and wiring.
For information about the transform API please see here: http://tools.android.com/tech-docs/new-build-system/transform-api
The Transform API is currently in beta, and we'd like to stabilize it as soon as possible, so we'd love to get feedback. Please join our development Google Groups and give us your feedback: https://groups.google.com/forum/#!forum/adt-dev