How to package and publish Android Gradle modules to local Maven repository

Article directory

The author's operating environment:

  • Android Studio Flamingo | 2022.2.1

  • Android SDK 33

  • Gradle 8.0.1

  • JDK 17

  Android Gradle projects are different from ordinary Gradle projects. Therefore, for packaging and publishing Gradle modules to the local Maven repository, the methods applicable to ordinary Gradle projects are not applicable to Android projects.

  Because the ordinary Gradle project is packaged and generated as a JAR package, while the Android project is packaged and generated as an AAR package. But in the end, an AAR package is the same as a JAR package, which is a compressed package. However, the AAR package is a secondary compression based on the JAR package. Specifically, the AAR package is a JAR package generated by compiling the source code and then putting it together with the Android manifest file to create a new compressed package.

  However, this difference leads to some differences in the process of publishing to the Maven warehouse.

specific process

  1. First, you need to install Maven, otherwise Gradle will not be aware of the location of the local Maven repository. Regarding this aspect, please see another blog of the author:

    Maven 的下载安装教程:
    https://blog.csdn.net/wangpaiblog/article/details/112689500

  2. Here we take the process of Android packaging and publishing to the local Maven repository as an example, and assume that the reader is using a Gradle multi-module project. What is demonstrated here is to package and publish one of the modules to the local Maven repository.

  3. In the module that needs to be packaged and published to the local Maven repository build.gradle, add the following code.

    plugins {
        id 'maven-publish'
    }
    
    task sourceJar(type: Jar) {
        from android.sourceSets.main.java.srcDirs
        archiveClassifier = "sources"
    }
    
    publishing {
        publications {
            maven(MavenPublication) {
                groupId = 'Maven 模块的 groupId'
                artifactId = 'Maven 模块的 artifactId'
                version = 'Maven 模块的 version'
    
                // 上传 AAR 包
                afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }
                // 向 Maven 仓库中上传源码
                artifact sourceJar
            }
        }
    }
    

    Among them, readers need to change the groupId, artifactId, and version in the above code to actual values.


    【hint】

      One might use additional configuration like this:

    publishing {
        // ...省略其它内容...
        repositories {
            maven {
                url = "http://my.org/repo"
            }
        }
    }
    

      This configuration is required when sending Gradle modules to the Maven network repository. For this article, this is unnecessary.


  4. Click in the Gradle panel on Android publishToMavenLocalto complete the release.

    Insert image description here


    [Reminder for stepping into pitfalls]

      Some readers may encounter publishToMavenLocalthe problem that the Gradle panel does not have this option. Regarding this aspect, please see another blog of the author:

      解决 Android Studio 的 Gradle 面板上只有关于测试的 task 的问题:
    https://blog.csdn.net/wangpaiblog/article/details/132124402


  5. After successfully publishing to the local Maven repository, Gradle will create three files in the corresponding locations in the Maven repository:

    • artifactId-version.aar

    • artifactId-version.pom

    • artifactId-version-sources.jar

  6. Then, add the following code in the root module of the Gradle project settings.gradleto introduce the local Maven repository. In this way, Gradle will additionally look for it in the local Maven repository when resolving dependencies.

    dependencyResolutionManagement {
        repositories {
            mavenLocal()
        }
    }
    
  7. Now, in the Gradle project, you can use it like other dependencies implementationto reference the dependency just published to the local Maven repository.

Guess you like

Origin blog.csdn.net/wangpaiblog/article/details/132155859