This page is obsolete. Redirecting to https://developer.android.com/studio/projects/android-library.html#PrivateResourcestl;dr;Private Resources is a new facility offered by the Android Gradle plugin and Android Studio which lets library authors designate specific resources in their library as “public”, and everything else is implied to be private. The tools then use this information to filter out private resources from code completion, to warn about name conflicts, etc.The ProblemA library will often contain a number of resources intended only for internal usage by the library; they are not intended to be referenced directly by developers using the library.Take appcompat for example: it ships with something like 400 resources. Now that it supports Material Design, some of the attributes are clearly intended for use, such as attributes controlling the Toolbar widget etc, but a large number of the resources are involved in implementing the backport of the ActionBar: internal layouts for the different action bar modes, nine-patches for backgrounds, and so on.Library resources share the same symbol namespace as the user’s own code, and this causes two problems:
Private ResourcesThe private resource mechanism lets developers designate some resources as “public”. When this is done, all remaining resources are considered private: this means that the library itself can use the resource, but clients can not.(This approach matches the Android Framework: The framework itself ships with both public and private resources; only resources that are explicitly marked as public are exposed in the SDK and are available to developers, and only the framework code itself can reference the other attributes. (Many Google applications used to access them anyway using the special *android syntax, but aapt no longer allows this and the best practice is to copy the resources for private use). Resources are made public in the framework by using the <public> resource type, where you specify the name, type, and assigned constant value.)Declaring Public ResourcesPublic.xmlDevelopers can define resources to be made public in the same way that this is already done in the framework: by adding a <public> declaration in any resource file:<resources> <public name="mylib_app_name" type="string"/> <public name="mylib_public_string" type="string"/> </resources> public.xml .One key difference between this public declaration and the one used in the framework is that in the framework, you also specify the specific R constant you want aapt to assign to it. That attribute does not apply here (and obviously can’t work: the values will have to be adjusted when merged into the final app; otherwise two unrelated libraries could specify the same constant value that would conflict.) (See the Compatibility section below for how we can start using this in libraries now, without breaking Eclipse or Makefile builds, as well as using earlier Gradle versions than the one which adds support for private resources.) AAR PackagingThe Android Gradle plugin has been modified such that when building a library, it grabs the public resource definitions and extracts them into a special file, “public.txt”, which is then packaged inside the AAR file next to “R.txt”. This file has a very simple syntax: type + space + name + newline. For the example above, it would bestring mylib_app_name string mylib_public_string The builder API has been updated to let clients look up the public symbols for an AAR: it now has a “File getPublicResources()” method which returns the location of the public symbols. IDE UsageThe IDE needs to make a deliberate decision for each feature whether it wants to know about all resources, or just the public resources. For layout rendering for example, it will need to track all resources, public and private. But for code completion, it should filter the results to only include public resources.
![]() Lint UsageAAPT2 will let developers deal with name conflicts properly. However, in the meantime, if you (accidentally) name your resources the same as a library resource, you’re overriding the library resource, with possibly disastrous results. |
Technical docs >