1.3, Android Studio creates an Android Library

An Android Library has the same structure as the Android app module. It can contain everything needed to build an app, including consummation, resource files and AndroidManifest.xml. However, instead of being compiled into an APK running on the device, an Android Library compiled into other Android app modules can be used as AAR files since.
A Library module is useful in the following situations:
1. When you build multiple apps that use the same components, such as activities, services, or UI layouts.
2. There are multiple APK files in the APP you build. Such as free version and paid version APK.
In these cases, simply move the files you want to reuse to the Library module and add Library as a dependency of the APP module.

Create a Library module

To create a new Library module into your project, follow the steps below:
1. Click File> New> New Module
2. When the Create New Module window appears, click Android Library. Then click Next.
There is also an option to create a Java Library, which can build a traditional JAR file. JAR files are available in many projects. Especially if you want to share code with other platforms, it is not allowed to include Android resource files or AndroidMainfest.xml. This is very useful in Android projects, so we focus on creating Android Library.
3. Set a name for your Library and choose the smallest SDK, then click Finish.
Once the Gradle project is synchronized. The Library module is displayed in the left panel of Project. If you don't see the new module folder, make sure to adapt to the Android view.

Convert App module to Library module

If you have an existing app module and you want to reuse all the code, you can convert it into a Library module through the following parts.
1. Open the build.gradle file, you can see the following statement:
apply plugin:'com.android.application'
2. Change to the following:
apply plugin:'com.android.library'
3. Click Sync Project with Gradle Files

It's that simple, the whole module does not need to be changed. But now it can be used as a Library and generate AAR files instead of APKs.

Add your Library as a dependency

In order to use your Android Library code in other app modules, the operations are as follows:
1. There are two ways to add library to your project.
To add the compiled AAR (or JAR) file:
1) Click File> New Module.
2) Click Import .JAR/.AAR Package and click Next.
3) Enter the location of the AAR or JAR file, and then click Finish.
Import your Library module to your project:
1) Click File> New> Import Module
2) Enter the location of the Library module and click Finish.
The Library module is copied to your project so you can edit the Library code.
2. Make sure that Library is listed at the top of the setting.gradel file.
include':app',':my-library-module'
3. Open the build.gradle file of the app module and add a new dependency to dependencies:
dependencies {
compile project(“:my-library-module”)
}
4. Click Sync Project with Gradle Files.
Now, any code and resources in your Android Library can be accessed by your app module, and the AAR file of the Library will be added when your APK is packaged.

Pay attention to the following issues

1. Resource merging may cause conflicts.
2. Library modules can contain JAR packages.
3. The library module cannot use raw resources.
4. The minimum SDK version of the App module must be equal to or higher than the version in the Library.
5. Each Library module creates its own R class.

Author: Song Zhihui
personal micro-blog: Click to enter

Guess you like

Origin blog.csdn.net/song19891121/article/details/51721790