Android 发布远程库作为依赖

606 阅读1分钟

前言

最近想搞个远程库玩玩,顺便把项目里的一些工具类封装进去,记录一下发布过程。

创建项目

创建一个新项目,并创建一个新Module。 image.png 项目中的APP模块可以删掉。 image.png

配置maven-publish Gradle 插件

首先要导入插件 image.png image.png

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'maven-publish'
}
group = 'com.github.zebraof'
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = 'com.github.zebraof'
                artifactId = 'origin'
                version = '1.0.0-beta'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = 'com.github.zebraof'
                artifactId = 'origin-debug'
                version = '1.0.0-beta'
            }
        }
    }
}

发布

首先在github上创建标签发布一个新版本。 image.pngjitpack处进行发布 image.png

其他问题

Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0. To opt-in to the future behavior, set the Gradle property android.disableAutomaticComponentCreation=true in the `gradle.properties` file or use the new publishing DSL.

解决方法:

添加  
android.disableAutomaticComponentCreation=true
至gradle.properties文件中。
在gradle中添加
android {
    ...
    publishing {
        singleVariant("release") {
            withSourcesJar()
        }
    }
}

传送门

官网教程:https://jitpack.io/docs/ANDROID/