Non-transitive R files have become available to all modules in AGP 4.2. Non-transitive R files allow your build to be faster and your AAB / APK smaller. This post will explain how to implement and build your app with non-transitive R files.
Not transitory what?
In mathematics, Non-transitivity Is a feature of binary relations that are not transitive relations. This may include any non-transitive ratio, or the more powerful feature of anti-transitive, that describes a non-transitive ratio to the world.
https://en.wikipedia.org/wiki/Intransitivity
Does this quote in math help us at all? probably not. Let’s try it: if A Depending on B and B Depending on third, A Do not know about third. In the Android world, non-transitive R files:
Non-transitive R classes allow the spacing of names of class R of each directory so that its class R includes only the declared resources in the directory itself and none of the dependencies of the directory, thus reducing the size of class R for that directory.
This blog post is not about how the R file works and the history involved, if you are interested in the background I would recommend This blog post is here.
Non-transitive modules
If module A Depending on B and B Depending on third. How can we relate to resources? lets see:
Module A Can refer to his resources (as usual):
R.string.hello_world
Module A Can refer to the module B Resources (full qualified package):
com.my.moduleB.R.string.hello_neighbour
Module A Can not refer to module thirdThe resources of.
You can use non-transitive R-rates with the Android Gradle plugin (> 4.2) to build faster build for multi-module applications. … this leads to more up-to-date builds and the parallel benefits of avoiding compilation.
https://developer.android.com/studio/releases#refactor-nontransitive-rclasses
The benefits of non-transitive R rates?
- Decrease in AAB / APK size, incl Counting references to the DEX field
- Decreased cumulative construction speed because less dependency can be included when making changes
- With the increase in modularity, the dependence becomes clearer
- Decreased in complexity, resources can not come from transient dependence
- Reducing the full construction duration because less code is included
lets do it!
You can enable this setting yourself by editing your own /gradle.properties
A file that will include:
android.nonTransitiveRClass=true
You may have read about android.namespacedRClass
In the past. Please note that in August 2020; android.namespacedRClass
Property name changed to android.nonTransitiveRClass
. As shown here.
or
You can use Android Studio’s AutoRefector, it will run the above setting and search your modules in an attempt to train all the R references it finds.
Note that this refactor was added in Android Studio Arctic Fox | 2020.3.1 “Automatic replacement for non-transitive R files”. As shown here.
This auto-recovery is not a money ball, it may get some incorrect resource references, meaning it will add the wrong package name before class R or it will not select one at all, or it will add one of your module is not dependent. In my experience, these problems are perceived during construction, and you can choose from the following three steps of the solution, to finish what the tool started again:
Once this is done, you will need to build your project and fix the errors. Errors can appear in several formats:
- You are using a resource from another module.
Correct: Add the full package, or import the R file, or (Kotlin only) use the nickname.// Fully qualifed package
val foo = com.my.moduleB.R.string.hello_neighbour
// Import then use string.hello_neighbour
import com.my.moduleB.R
// Alias then use RB.string.hello_neighbour
import com.my.moduleB.R as RB - You are using a resource from another module, but you are not declaring dependency on that module.
Correct: Do as number 1 but also add the rank dependency.implementation project(":libraries:moduleB")
- You use a resource from another module, but you do not declare dependency on that module, and you do not want to declare dependency.
Correct: The solution here is for both Create a reference And use the same name or copy / create a new resource of what you want.
![](https://blog.blundellapps.co.uk/wp-content/uploads/2021/09/Screenshot-2021-09-26-at-8.55.44-pm.png)
Summary
That’s all there is too, and that’s pretty easy for me to say, but in practice depending on the size of your project, you may have many hours ahead of you, debugging / searching where your resources are coming from and what the right skill is. Package name to use. I highly recommend any new project you run nonTransitiveRClass
Before you have to deal with the fallout below.