android create aar package

1. Background

        Since the newly hired company is doing hardware access projects, it needs to access a variety of hardware, and the functions of several apps are not the same. Therefore, modular development is required, and many things are reusable (such as network framework, log, shareprefrence, permission application, etc.). So here is a summary of the creation and reference of aar.

2.aar creation

1. Create a normal project

        Since each project must reference this aar, it is recommended to create a new project directly, then generate an aar, and directly reference it in each project.

        Select "add no activity", click next,

         Set the package name, project name, etc. on the configuration project interface.

         Click finish to complete the project creation. This step is to create the project normally, there is nothing to say.

2. Change normal project to aar project

This step is also very simple, just a few steps of operation.

        The first is to modify the apply plugin: apply plugin: 'com.android.application'

Change to apply plugin: 'com.android.library' , as follows

(The directory is in app's bulid.gradle)

Then just comment out the applicationId line, as follows:

(The directory is still app's bulid.gradle)

         Then modify the list configuration file: application, specifically: modify the application attribute:

         Also, if you did not select "add no activity" when creating the project above, then you need to remove the "main" declaration here, as follows:

 The modification is basically completed here.

3. Package the aar package and share it with other projects.

        It is also divided into debug.aar and release.aar.

You need to build the debug package: click "build" directly, select "Make Module 'app' ", wait for the compilation to complete, and you can see the aar package in the build directory (the specific directory is: your project directory\app\build\outputs\ aar)

Need to build the release package: gradle interface, select the current project --> Tasks --> build, double-click "assembleRelease",

Wait for the compilation to complete, and you can see the aar package in the build directory.

Remarks: Some of these cannot find assembleRelease in the build, so you can look for it in other.

3.aar quotation

Citing in two steps

1. Copy the packaged aar to the "libs" of your project

2. Add dependencies to app 's bulid.gradle :

implementation(name: 'app-debug', ext: 'aar'), where: app-debug is the name of your packaged aar package.

3. Add in app 's bulid.gradle :

// 这是添加aar依赖需要的
repositories {
    flatDir {
        dirs 'libs'
    }
}

If it does not increase, an error will be reported: ERROR: Unable to resolve dependency for xxx.

4. Configure in AndroidManifest.xml:

 This is basically a reference.

4. Items to be noted

1. It is best not to use the default aar name (that is, app-debug), so as not to cause conflicts with the referenced third-party SDK; it is best to name it with a function description.

2. If the main module refers to the module in the aar package, then the dependency on the module in the aar package should use api instead of

implementation, otherwise an error will be reported. This rule also applies to referencing modules, etc.

Guess you like

Origin blog.csdn.net/set_one_name/article/details/128533508