安卓分享Androidmaven

发布AAR至Maven仓库

2022-07-07  本文已影响0人  Hz_

一、打包配置

1、编辑脚本

在需要打包的module下创建

publish.gradle

apply plugin: 'maven-publish'

task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier 'sources'
}

afterEvaluate {
    publishing {
        publications {
            Production(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                groupId = GROUP_ID
                artifactId = ARTIFACT_ID
                version = VERSION
                // 上传source,这样使用放可以看到方法注释
                artifact generateSourcesJar
            }
        }
        repositories {
            // 定义一个 maven 仓库
            maven {
                url = VERSION.endsWith('SNAPSHOT') ? SNAPSHOT_URL : RELEASE_URL
                // 仓库用户名密码
                credentials {
                    username = USER_NAME
                    password = PASSWORD
                }
                // 允许通过不安全的HTTP连接与仓库通信
                allowInsecureProtocol(true)
            }
        }
    }
}

gradle.properties(配置打包脚本中依赖的资源)

RELEASE_URL=http://120.66.66.66:6666/repository/maven-releases/
SNAPSHOT_URL=http://120.66.66.66:6666/repository/maven-snapshots/

GROUP_ID=com.test.library
ARTIFACT_ID=library

# 在nexus配置的用户名
USER_NAME=userName
# 在nexus配置的用户密码
PASSWORD=password
# 版本号
VERSION=1.0.1-SNAPSHOT

2、其他配置

apply from : "publish.gradle"

3、执行打包

在Android studio的Terminal中执行命令

./gradlew publish

二、使用配置

maven {
    // maven仓库地址
    url 'http://120.66.66.66:6666/repository/maven-public/'
    // 允许使用http访问的maven仓库,如果是https则不用配置
    allowInsecureProtocol = true
 }
// com.test.library 是groupId
// library 是artifactId
// 1.0.1-SNAPSHOT 是版本号
implementation 'com.test.library:library:1.0.1-SNAPSHOT'

三、报错集合

does not allow updating artifact

Artifact updating: Repository ='releases:Releases' does not allow updating artifact='/com/github/120011676/vine/maven-metadata.xml.sha256'

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.

 Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.
# 指定Gradle依赖的JAVA版本
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
上一篇 下一篇

猜你喜欢

热点阅读