How to make Android library resources private?

How to make resources (string, size, color, etc.) in my Android library module private?

I tried both documented ways to do this, and none of them worked ...

  1. Following the Android res/values/public.xml paper on creating res/values/public.xml does not work; all resources remain open in the application that uses this library.
  2. Following the instructions of Chris Baines (and repeated in https://stackoverflow.com/a/3/2727/16/ ), creating the res-public/values/public.xml also does not work; all resources are made private, but those listed in public.xml are not public.xml in public.xml order.

Does anyone have clear instructions on closing library resources? Are there any open source libraries that have properly made their resources private?

I use...

  • Android Studio v2.2.3
  • buildToolsVersion "24.0.2"
  • com.android.tools.build:gradle:2.2.3
+6
source share
3 answers

Option number 2 really works. I did not define my sourceSets in my build.gradle file ...

 sourceSets { main.res.srcDirs = [ 'src/main/res', 'src/main/res-public' ] } 
+1
source

It is assumed that your resources will be publicly available in any module (library, etc.), unless you start explicitly declaring any resource in the module public. At this point, you basically choose that each resource (inside the module) be private, except that you mark it as public. This preserves backward compatibility and allows you to gradually increase access to large projects with many modules.

To make all resources private, simply add <public /> to any of your existing resource files.

The answer above talks about adding a specific resource directory only for managing public / private modifiers. Although this works, I could suggest that you manage the visibility and declaration in the main resource files next to where they are declared. Here is an example strings.xml resource file that I used with the new library module. The prefix public / private in line names is for illustration only.

RES / value / strings.xml

 <resources> <string name="app_name">My Library2</string> <public type="string" name="public_string_in_lib2" /> <string name="public_string_in_lib2">public string in lib2</string> <string name="private_string_in_lib2">private string in lib2</string> </resources> 

Basically, these qualifications are used to create a public.txt file built into the AAR that another module depends on. Various tools, such as Android Studio, will use this information for a flag / warning, but technically, the library consumer will not be prevented from using the resource if their equipment is not really strict.

+3
source

If you stumbled upon this and you still have this problem, check out this Android doc:

https://developer.android.com/studio/projects/android-library#PrivateResources

Hope this helps.

0
source

Source: https://habr.com/ru/post/1263367/


All Articles