Library module development considerations

Be aware of the following behaviors and limitations when developing library modules and related applications.

After you add references to library modules to Android application modules, you can set their relative priority. At build time, libraries are merged with applications one by one in descending order of priority.

Resource Merge Conflict

The build tool merges resources in library modules with resources in related application modules. If a given resource ID is defined in both modules, the system uses the resources in the application.

If there is a conflict between multiple AAR libraries, the resources in the library first listed in the dependencies list (near the top of the Dependencies block) are used.

To avoid resource conflicts with commonly used resource ids, consider using prefixes or other consistent naming schemes that are unique to modules (or across all project modules).

In a multi-module build, the system treats JAR dependencies as transitive dependencies

When a JAR dependency is added to a library project that outputs an AAR, the JAR is processed by the library module and packaged with its AAR.

However, if your project contains a library module that is already used by an application module, the application module treats the library’s native JAR dependency as a delivery dependency. In this case, the local JAR will be processed by the application module that uses it, not by the library module. This is to speed up incremental builds caused by changes to the library code.

All Java resource conflicts caused by native JAR dependencies must be resolved in application modules that use the corresponding libraries.

Library modules can rely on external JAR libraries

You can develop a library module that relies on an external library, such as the Google Maps external library. In this case, the application must be built for a target that contains additional libraries, such as the Google API plug-in. Also note that both library modules and related applications must declare external libraries in elements of their manifest files.

The minSdkVersion of the application module must be equal to or greater than the version defined by the library

Libraries are compiled as part of the relevant application modules, so the apis used in library modules must be compatible with the platform versions supported by the application modules.

Each library module creates its own R class

When you build related application modules, library modules are compiled into AAR files and then added to application modules. Therefore, each library has its own R class, named after the library’s package name. Classes R generated from the main module and library modules are created in all required packages, including the main module’s packages and the library’s packages.

Library modules can contain their own ProGuard configuration files

If you have a library project to build and publish an AAR, you can add a ProGuard profile to the library’s build configuration, and the Android Gradle plug-in rules apply to the ProGuard rules you specify. The build tool will embed this file in the AAR file generated for the library module. After you add a library to an application module, the library’s ProGuard file is attached to the application module’s ProGuard configuration file (proguard.txt).

By embedding ProGuard files into library modules, you can ensure that application modules that depend on the library can use the library without manually updating their ProGuard files. When the Android Studio build system builds your application, it uses instructions from both application modules and libraries. There is no need to follow separate steps to run the code reductor on the library.

To add ProGuard rules to a library project, you must specify the file name using the consumerProguardFiles property (located in the defaultConfig block of the library’s build.gradle file). For example, the following code snippet sets lib-proguard-rules.txt to the proGuard configuration file for the library:

android {
    defaultConfig {
        consumerProguardFiles 'lib-proguard-rules.txt'
    }
    ...
}
Copy the code

However, if a library module is part of a multi-module build to be compiled into APK and an AAR is not generated, you should only run code reduction operations on application modules that use the corresponding library. For more information about ProGuard rules and their usage, see Reduction, Obfuscation Handling, and Application Optimization.

You test a library module the same way you test an application

The main difference is that the library and its dependencies are automatically included as dependencies for testing APK. This means that the test APK contains not only its own code, but also the LIBRARY’S AAR and all its dependencies. Since there is no separate “application under test,” the androidTest task only installs (and uninstalls) the test APK.

When merging multiple manifest files, Gradle follows the default priority order and merges the lists of libraries into the main list for testing APK.

See: developer.android.com/studio/proj…