Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Android library resources private?

How do I make the resources (string, dimen, color, etc.) in my Android library module private?

I've tried both documented ways of doing this and neither work...

  1. Following the official Android doc of creating a res/values/public.xml does not work; all of the resources remain public in the app that uses this library.
  2. Following Chris Banes's instruction (and reiterated in this StackOverflow answer) of creating a res-public/values/public.xml folder does not work either; all of the resources are made private, but those listed in the public.xml file are not made public as they should be.

Does anyone have the definitive instructions for making library resources private? Are there any open-source libraries out there that have properly made their resources private?

I'm using...

  • Android Studio v2.2.3
  • buildToolsVersion "24.0.2"
  • com.android.tools.build:gradle:2.2.3
like image 699
Gus Avatar asked Jan 25 '17 23:01

Gus


People also ask

How do I obfuscate an Android library project?

In order to obfuscate your library what you need to do is: Make sure gradle settings for lib module state minifyEnabled true . run ./gradlew assemble{flavor}Release.

How do I manage resources in Android?

Resource Manager is a tool window for importing, creating, managing, and using resources in your app. You can open the tool window by selecting View > Tool Windows > Resource Manager from the menu bar or by selecting Resource Manager on the left side bar. Click Add to add a new resource to your project.

What is .AAR file in Android Studio?

Android Studio can be used to create an Android archive file (*. aar) that can contain classes and methods that make use of Android classes and related files. Like creating the Jar file, an Android project must be created first, then the Android library module can be created and added.


1 Answers

Your resources are assumed to be public in any module (library, etc) unless you start, explicitly, declaring any resource in the module to be public. At that point you basically opt-in to every resource (within the module) being private except what you mark as public. This preserves backward compatibility and let's you incrementally tighten down access in large projects with many modules.

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

The above answer talks about adding a specific resource directory just to manage the public/private modifiers. While that works, I might suggest you manage the visibility and declaration in the main resource files next to where they are declared. Here's a sample strings.xml resource file I used with a brand new library module. The public/private prefix in the string names is for illustrative purposes only.

res/values/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>

Fundamentally, these qualifications are being used to create a public.txt file that is embedded in the AAR that will be depended upon by another module. Various pieces of tooling like Android Studio will use this information to flag/warn, but technically, a consumer of the library isn't prevented from using the resource unless their tooling is being really strict.

like image 67
PaulR Avatar answered Sep 22 '22 00:09

PaulR