Method of creating Android library and Usage Summary of Android. AAR file

Create Android Library

The structure of Android library is the same as that of Android Application module. It can provide everything needed to build an application, including source code, resource files and Android listings. However, the Android library will be compiled into Android Archive (AAR: Android archive resource) files that you can use as Android Application module dependencies, rather than APK running on the device. Unlike jar files, AAR files can contain Android resources and a manifest file. In this way, in addition to Java classes and methods, you can also bundle shared resources such as layout and paintable objects.

Library modules are useful when:

In either case, you only need to move the files you want to reuse into the library module, and then add the library for each application module in the form of dependencies. This page will explain how to perform these two operations.

Create library module

To create a new library module in your project, follow these steps:

1. Click File > New > new module.

2. In the create new module window that appears, click Android library and next. There is also an option to create a Java library to build traditional jar files. Although jar files are useful in most projects (especially when you want to share code with other platforms), they do not allow you to include Android resources or manifest files, which are very useful for code reuse in Android projects. Therefore, this guide will focus on creating Android libraries.

3. Name your library, select a minimum SDK version for the code in the library, and then click finish.

After the gradle project synchronization is completed, the library module will be displayed in the project panel on the left. If you do not see the new module folder, make sure to switch the view to Android view.

Convert Application module to library module

If your existing application module contains all the code you want to reuse, you can convert it into a library module according to the following steps:

1. Open the build.gradle file of the existing application module. You should see the following at the top:

2. Change the plug-in assignment as follows:

3. Click sync project with gradle files.

It's that simple. The entire structure of the module remains the same, but now it will run as an Android library, and the build will also create an AAR file instead of an APK.

Generate AAR

In Android studio 1.4, for new project, first create an application module by default, then File > New > new module, select Android Library > next, and specify library name and module name.

The biggest difference between the new library and the normal application is that in module build.gradle, the application plugin: 'com. Android. Library' is not the application plugin: 'com. Android. Application'

Build > make project to automatically generate AAR files.

Add your library as a dependency

To use the code of your Android Library in another application module, follow these steps:

1. Add a library to your project in one of two ways (if you created a library module in the same project, the module already exists, you can skip this step):

To add a compiled AAR (or jar) file:

1. Click File > new module.

2. Click Import. Jar /. AAR package and next.

3. Enter the location of the AAR or jar file and click finish.

To import library modules into your project:

1. Click File > New > import module.

2. Enter the location of the library module directory and click finish.

The library module will be copied into your project, so you can edit the library code. If you want to maintain a version of the library code, this method may not be what you want. You should import the compiled AAR file as described above.

2. Make sure the library is listed at the top of your settings.gradle file, as shown in the following library named "my library module":

3. Open the build.gradle file of the application module and add a new line of code to the dependencies block, as shown in the following fragment:

4. Click sync project with gradle files.

In the above example, the Android library module named my library module becomes the build dependency of the module where the build.gradle file is located.

Your application module can now access any code and resources in the Android library, and the library AAR file has been bundled into your APK at the time of construction.

However, if you want to share the AAR file separately, you can find it in project name / module name / build / outputs / AAR /, or you can rebuild the file by clicking build > make project.

Select the resource to make public

All resources in the library are exposed by default. To make all resources implicitly private, you must define at least one specific property as public. Resources include all files in the RES / directory of your project, such as images. To prevent your library users from accessing resources for internal use only, you should use this automatic private identification mechanism by declaring one or more public resources.

To delete a public resource, add a declaration to the public. XML file of your library. If you have not added public resources before, you need to create a public.xml file in the RES / values / directory of your library.

The following example code can create two names, mylib_ app_ Name and mylib_ public_ Public string resources of string:

If you want any resources to remain visible to developers using your library, you should make them public. For example, although most of the resources in the V7 appcompat library are private, in order to support material design, the properties of the control toolbar widget should be exposed.

Implicitly making properties private not only prevents your library users from getting code autocompletion advice from internal library resources, but also allows you to rename or remove private resources without interrupting your library client. Private resources are outside the scope of code autocompletion and theme editor, and lint displays a warning if you try to reference private resources.

Development considerations

When developing your library modules and related applications, please pay attention to the following behaviors and restrictions.

After adding library module references to your Android application modules, you can set their relative priority. During construction, the library will be merged with the application one at a time, and the priority order will be from low to high.

Resource merge conflict

The build tool will merge the resources in the library module with the resources of the related application module. If a given resource ID is defined in both modules, the resource in the application will be used.

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

To avoid resource conflicts for common resource IDS, use a prefix or other consistent naming scheme that is unique in the module (or in all project modules).

Library modules can contain jar libraries

You can develop a library module that contains its own jar library; However, you need to manually edit the build path of the relevant application module and add the path of the jar file.

Library modules can rely on external jar libraries

You can develop a library module that depends on an external library, such as the maps external library. In this case, the relevant application must be built for the target containing an external library, such as the Google API plug-in. Also note that both library modules and related applications must declare external libraries in the < uses - Library > element of their manifest file.

Library modules must not contain original resources

The tool does not support the use of original resource files (saved in assets / directory) in library modules. Any original resources used by the application must be stored in the assets / directory of the application module itself.

The minsdkversion of the application module must be greater than or equal to the version of the library definition

The library is compiled as part of the relevant application module. Therefore, the API used in the library module must be compatible with the platform version supported by the application module.

Each library module creates its own R class

When you build the relevant application module, the library module will be compiled into the AAR file first, and then added to the application module. Therefore, each library has its own R class and is named according to the package name of the library. The R class generated from the main module and library module will be created in all required software packages, including the package of the main module and the package of the library.

Library modules may contain their own Proguard configuration files

You can enable code compression on your own library by adding a Proguard configuration file to the library that contains its Proguard directives. The build tool embeds this file into the generated AAR file for the library module. When you add a library to an application module, the Proguard file of the library is attached to the Proguard configuration file (Proguard. Txt) of the application module.

By embedding the Proguard file into your library module, you can ensure that application modules that depend on this library can use the library without manually updating their Proguard file. When Proguard runs on an Android Application module, it uses instructions from both the application module and the library, so you should not run Proguard only on the library.

To specify the configuration file name of your library, add it to the consumerproguardfiles method, which is located in the defaultconfig block of your library's build.gradle file. For example, the following fragment sets lib-proguard-rules.txt as the Proguard configuration file of the Library:

By default, application modules are built using the release of the library, even when using the debug build type of the application module. To use different build types in the library, you must add dependencies to the dependencies block of the applied build.gradle file and set publishnondefault to true in the library's build.gradle file. For example, the following code snippet in the build.gradle file of your application will make the application use the debug build type of the library when the application module is built in debug mode and the release build type of the library when the application module is built in release mode:

You must also add the following lines of code in the Android block of your library's build.gradle file to show the non published configuration of this library to projects that use it:

Note, however, that setting publishnondefault increases the build time.

In order to ensure that the Proguard rules of your library will not impose unexpected compression side effects on the application module, please include only the appropriate rules and disable the Proguard functions that are not applicable to this library. Rules that try to assist developers may conflict with existing code in the application module or its other libraries, so they should not be included. For example, the Proguard file of your library can specify the code that needs to be retained during the compression of the application module.

Note: Jack tool chain only supports some compression and blur options of Proguard.

AAR file details

The file extension of the AAR file is. AAR, and the Maven artifact type should also be AAR. The file itself is a zip file with the following mandatory entries:

/AndroidManifest.xml

/classes.jar

/res/

/R.txt

In addition, the AAR file may contain one or more of the following optional entries:

/assets/

/LIBS / name.jar

/JNI / ABI name / name. So (where ABI name is one of the ABIS supported by Android)

/proguard.txt

/lint.jar

Let's introduce the generation method and usage of. AAR file in Android

preface

Whether you use eclipse or Android studio for Android development, you will contact the jar package. The full name should be: Java archive, that is, Java archive.

In the process of using as, you will find that AAR is an Android archive. I think its full name should be Android archive.

What's the difference between them?

Personal understanding:

Jar is a compressed package of class files compiled from java files.

AAR is the total compressed package of all resource files and compiled Java files in Android module.

The difference is that AAR contains not only the class file, but also the resource file.

How to import AAR files in Android studio?

Take the AAR file of recyclerview as an example.

1. AAR file found:. \ Android_ SDK_ Windows\extras\android\m2repository\com\android\support\recyclerview-v7\24.0.0\recyclerview-v7-24.0.0.aar

Copy to the LIBS directory in the Android studio project.

2. Then enter the following in the build.gradle file of the module corresponding to Android Studio:

3. Then rebuild the whole project.

How to export a module as an AAR file in Android studio?

Take testlibrary as an example:

1. Execute the assemblyrelease command of the corresponding module:

@H_ 502_ 306@

Remember from the above figure that if it is the main module of the project, i.e. app, the APK package is generated.

2. In the build \ outputs \ AAR \ directory of the corresponding module:

@H_ 502_ 306@

What if you want to import the AAR file into eclipse?

@H_ 502_ 306@

As shown in the figure: after opening the AAR file with WinRAR and other compression software, you will find that it is the above structure. Classes.jar is the jar package of class file, and others are Android related resource files.

If you want to import into eclipse, you need to convert all the resources and class files into a library project.

Create a new library project with eclipse, import res resources, manifest files and publish compiled Java files.

summary

The above is the method of creating Android library and the generation method and usage of Android. AAR file introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>