Android SDK development aar from 0 to 1

Record the pits that were developed by aar.

Previous period:

1Create a new project using Android Studio

2File--New--New Module creates a module (develop your aar logic in this module)

1. If you do not have a dependent third-party library in the generated aar, you can use this method:

After writing the code, execute the Build--Rebuild project. After the reset is completed, build in the module-----outputs-----aar folder will be generated.

Aar is the suffix of the stuff. Copy it to the libs of the main module and configure it in build.gradle

Note: You can call the method in aar by the above method.

2. If you have dependent third-party dependencies in your aar, you will not be able to do so in the above way (third-party dependencies cannot be entered into aar)

The first one: the local maven repository form, suitable for self-development and use (if aar copy is used by others, others can still not rely on third parties)

Configured in the build.gradle of the module

Note: Make sure your environment variable ANDROID_HOME is available (how to configure ANDROID_HOME Baidu)

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/" + "你的SDK安装目录" +"/extras/android/m2repository/");
            pom.version = '版本'
            pom.groupId = '生成的aar目录包名'
            pom.artifactId = 'aar名称'
        }
    }
}

// 源代码一起打包.
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}
artifacts {
    archives androidSourcesJar
}

There is a Gradle point on the right side of the Studio window. Find the module you wrote, and there are Tasks---upload--uploadArchives.

After double-clicking uploadArchives to run, you will generate aar under your SDK installation directory /extras/android/m2repository/ under your own package name.

Referenced in the build.gradle of the main Module

implementation 'com.chint.uniqlosdk:uniqlosdk:1.0-release'(就这一行就可以)

The second type: aar copy to others, through the GitHub method to create a remote maven repository (to solve the problem of third-party dependencies can not be used)

Create a new warehouse on GitHub

Clone the library down through Studio

Configured in the build.gradle of the module (this is not a cloned project)

uploadArchives {
    repositories {
        mavenDeployer {
            //这个地址为你clone下来库的地址
            repository(url: "file://E:\\Github_Repository\\Project\\HxhSDK")

            pom.version = '1.0-release'//版本
            pom.groupId = 'com.hxh.hxhsdk'//aar在安装的SDK目录下输出的路径
            pom.artifactId = 'hxhdk'//
        }
    }
}

// 源代码一起打包.
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}
artifacts {
    archives androidSourcesJar
}

Execute the command in Studio's Terminal gradlew uploadArchives if you see BUILD SUCCESS

At this point there will be a folder in the clone project. This folder contains all the generated aar stuff. Upload everything in this folder to GitHub.

Add a dependency in the main module after uploading

The first parameter: groupId

The second parameter: artifactId

The third parameter: version

implementation 'com.hxh.hxhsdk:hxhsdk:1.0-release'

Add in the project's build.gradle

maven { url "https://raw.githubusercontent.com/GitHub用户名/GitHub上的库名/master" }

Example:

After configuring these two synchronization projects, GitHub hosting does not need to copy aar to libs.

Note: There is also a relatively low method:

Copy apar and generate it into libs. If you use a third-party library, manually add the dependency in the main module.