Android Studio uploads aar to private maven repository

Since using Android Studio for project development, gradle has become a new project management tool, which is very convenient for the management of third-party libraries or plugins, and we want our own libraries to be used like this:

    implementation 'xxxx:xxxx:1.0.0'

We need to upload our own library to jcenter() or our own maven server.

Win10 upload aar to private maven server

First we download the corresponding version of nexus: https://www.sonatype.com/download-oss-sonatype

write picture description here

After downloading and decompressing, first close the antivirus software and all applications that may occupy port 8081. I can't get in if I don't do this at first; then I go to the nexus-3.11.0-01/bin directory, use the administrator to open powerShell, and you can also use cmd. Remember to open it as an administrator. Execute the commands in sequence:

.\nexus.exe /install
.\nexus.exe /start

In this way, the service is turned on, and we can view the running status of the service through .\nexus.exe /status. Then we can go directly to http://localhost:8081/ to open the maven server.

write picture description here
We select the login option in the upper right corner, the default username: admin, password: admin123. Then we will see several default libraries. To make the tutorial easier, we will not create new libraries. We will eventually upload the aar to the maven-release library. Click the copy button on the right side of the library to get the URL.
write picture description here

Next we enter Android Studio, where we create an application module called app and a library module called mylibrary. I won't say more about the steps. If you are an Android developer, you will definitely know it. Then we write a random class in the source directory of mylibrary:
CoreClass.java

public class CoreClass {
    public static String Core(){
        return "This is my core function";
    }
}

Next we add the upload_local_maven.gradle file in the root directory of mylibrary. and enter the following. Like uploading Jcenter, we also upload four files: pom, aar, source, and doc.

apply plugin: 'maven'


//def定义的变量才会在project中
def POM_NAME='core'

def POM_VERSION='1.0.0'

def POM_ARTIFACTID="core"//项目名称id

def POM_GROUPID='com.example.mylibrary'  //项目组id

def POM_PACKAGING='aar'

def POM_DESCRIPTION='lib for Android'

def NEXUS_USERNAME='admin'   //用户名

def NEXUS_PASSWORD='admin123'  //密码

def NEXUS_REPOSITORY_URL='http://localhost:8081/repository/ttsx/'  //repository的URL



task androidJavadocs(type: Javadoc) {
    options.encoding = "utf-8"
    source = android.sourceSets.main.java.srcDirs
    //添加classpath,解析类型
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

//打包doc
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc' //分类器,区分jar
    from androidJavadocs.destinationDir
}

//打包源码

task androidSourcesJar(type: Jar) {
    classifier = 'sources'//分类器,区分jar
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: NEXUS_REPOSITORY_URL) {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
            }
            pom.project {
                name POM_NAME
                version POM_VERSION
                artifactId POM_ARTIFACTID
                groupId POM_GROUPID
                packaging POM_PACKAGING
                description POM_DESCRIPTION
            }
        }
    }
}

The uploaded code here refers to https://www.jianshu.com/p/efddad9d4f84 . Final directory structure
write picture description here
Finally , we add the following code at the end of build.gradle under mylibrary and synchronize gradle.

apply from: 'upload_local_maven.gradle'

Finally, we find the corresponding upload task in the task window of gradle and execute it.
write picture description here
After the execution is successful, we can find the successfully uploaded file.
write picture description here
Finally, let's try to reference this module in the app and add the following code to the app's build.gradle. The URL of the maven library needs to be specified.

repositories{
    maven{
        url "http://localhost:8081/repository/maven-releases/"
    }
}
dependencies {

    implementation 'com.example.mylibrary:core:1.0.0'

}

Next, you can directly reference the code and resources in the aar.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569803&siteId=291194637