Eric Holk | c4239ac | 2018-09-05 10:43:31 -0700 | [diff] [blame] | 1 | # View Compiler |
| 2 | |
| 3 | This directory contains an experimental compiler for layout files. |
| 4 | |
| 5 | It will take a layout XML file and produce a CompiledLayout.java file with a |
| 6 | specialized layout inflation function. |
| 7 | |
| 8 | To use it, let's assume you had a layout in `my_layout.xml` and your app was in |
| 9 | the Java language package `com.example.myapp`. Run the following command: |
| 10 | |
| 11 | viewcompiler my_layout.xml --package com.example.myapp --out CompiledView.java |
| 12 | |
| 13 | This will produce a `CompiledView.java`, which can then be compiled into your |
| 14 | Android app. Then to use it, in places where you would have inflated |
| 15 | `R.layouts.my_layout`, instead call `CompiledView.inflate`. |
| 16 | |
| 17 | Precompiling views like this generally improves the time needed to inflate them. |
| 18 | |
| 19 | This tool is still in its early stages and has a number of limitations. |
| 20 | * Currently only one layout can be compiled at a time. |
| 21 | * `merge` and `include` nodes are not supported. |
| 22 | * View compilation is a manual process that requires code changes in the |
| 23 | application. |
| 24 | * This only works for apps that do not use a custom layout inflater. |
| 25 | * Other limitations yet to be discovered. |
Eric Holk | d683f9f | 2018-10-26 16:08:09 -0700 | [diff] [blame^] | 26 | |
| 27 | ## DexBuilder Tests |
| 28 | |
| 29 | The DexBuilder has several low-level end to end tests to verify generated DEX |
| 30 | code validates, runs, and has the correct behavior. There are, unfortunately, a |
| 31 | number of pieces that must be added to generate new tests. Here are the |
| 32 | components: |
| 33 | |
| 34 | * `dex_testcase_generator` - Written in C++ using `DexBuilder`. This runs as a |
| 35 | build step produce the DEX files that will be tested on device. See the |
| 36 | `genrule` named `generate_dex_testcases` in `Android.bp`. These files are then |
| 37 | copied over to the device by TradeFed when running tests. |
| 38 | * `DexBuilderTest` - This is a Java Language test harness that loads the |
| 39 | generated DEX files and exercises methods in the file. |
| 40 | |
| 41 | To add a new DEX file test, follow these steps: |
| 42 | 1. Modify `dex_testcase_generator` to produce the DEX file. |
| 43 | 2. Add the filename to the `out` list of the `generate_dex_testcases` rule in |
| 44 | `Android.bp`. |
| 45 | 3. Add a new `push` option to `AndroidTest.xml` to copy the DEX file to the |
| 46 | device. |
| 47 | 4. Modify `DexBuilderTest.java` to load and exercise the new test. |
| 48 | |
| 49 | In each case, you should be able to cargo-cult the existing test cases. |
| 50 | |
| 51 | In general, you can probably get by without adding a new generated DEX file, and |
| 52 | instead add more methods to the files that are already generated. In this case, |
| 53 | you can skip all of steps 2 and 3 above, and simplify steps 1 and 4. |