1 unstable release

0.1.0 Jun 2, 2024

#625 in Development tools

Custom license

98KB
2K SLoC

R55

What's this?

R55 is both a parser and compiler for R.txt files generated by aapt. It can generate R.java file from a R.txt file, and better, skip java compiler entirely by generating R.class files (bytecode) directly.

How do i use it?

R55 is split into several subcommands.

help

Gives you help and command usage

java

This generates a R.java class from R.txt file. r55 java build/gen/R.txt --package com.example.r55

compile

This generates a java bytecode from R.txt file. R55 embeds a mini java compiler that is able to write java bytecode of the input R.txt files.

find

Finds a value of a resource from R.txt file.

--split

This is a flag that can be found on java and compile subcommand. It is used to tell R55 that you would like to generate R classes for various libraries/dependencies.

example usage

You are labt and you have resolved your dependencies and downloaded all relevant aar files, cool. Now you have a heavy task of compiling all the dependencies into an android application. Appt2 compiles your resource libraries faithfully but there is no way of generating individual library specific R.java file. At your disposal you have --extra-packages arg Generate the same R.java but with different package names. from aapt2, which works but you end up with a (1.5Mb R.java)*16 files for all your dependencies. So you decide to create R.java files that contains only resource ids that each library needs but there is no way. So you decide to write a tool that does this and call it R55...

Anyway, android aar files comes with R.txt files that lists what resource ids the library exposes. The difference between these R.txt files and the one aapt2 generates is that these contain wrong resource id values. e.g. from the library recyclerview-1.1.0,

int attr alpha 0x0
int attr fastScrollEnabled 0x0
int attr fastScrollHorizontalThumbDrawable 0x0
int attr fastScrollHorizontalTrackDrawable 0x0

The values for each entry is 0x0 which is an invalid reference in android resources.arsc file. This is intentional as aapt2 calculates and generates new values every time these resources entries change, so there is no way for a library to know its ids in advance. What our --split flag does is use these files and the newly generated R.txt files by aapt2 to produce a valid library specific R.class. From our example above, it will find the value of fastScrollEnabled and replace its value with the correct value e.g from 0x0 to 0x7fadbeef.

r55 compile ../build/gen/txt/R.txt \
    --package com.edu \
    --split \
    --files lib-res/core-1.5.0/R.txt:lib-res/appcompat-resources-1.1.0/R.txt:lib-res/appcompat-1.2.0/R.txt:lib-res/cardview-1.0.0/R.txt \
    --packages androidx.core:androidx.appcompat:androidx.appcompat:androidx.cardview \
    --output .

So you will need paths to R.txt files in your libraries and their corresponding library package names and R55 will faithfully generate java files as needed.

TODO

  • Add a dex intermediate writer (Files that d8 or r8 can use for incremental compilation.)
  • Proper error reporting that is also consistent

Dependencies

~1MB
~15K SLoC