Android Studio 发布 Maven Gradle 7.0+

伴随着 Gradle 7.0 出来以后, 老方式的 maven 上传已经不能用, 所以在这做一下笔记, 记录 maven 上传的一些注意事项。

博主博客

  • https://blog.uso6.com
  • https://blog.csdn.net/dxk539687357

一、升级 JDK(可跳过阅读)

1.1 自己下载

因为 Gradle 7.0 以上需要 JDK 11 环境, 所以需要升级一下 JDK, 但这里要注意一点, 使用 OpenJDK。 我一开始去官网下载 JDK 11, 后来朋友提醒 JDK 8 以后要收费, 查了一堆资料, 最后发现还是 OpenJDK 安全一点, 点击 这里 进入 OpenJDK 官网。

1.2使用 Android Studio 自带的

菜单栏 File->Settings, 在 Build,Execution,Depoyment>Build Tools>Gradle 模块里面, 可以看到 Gradle SDK, 设置为 Embedded JDK 即可。

如果没有, 把 Android Studio 升级到最新版本。

二、Gradle 7.0 以下上传 maven (可跳过阅读)

2.1 Maven配置

Library 模块的 build.gradle 中添加以下代码:

uploadArchives {
    apply plugin: 'maven'
    // 读取本地配置文件
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newInputStream())
    def userName = properties.getProperty('maven.user')
    def password = properties.getProperty('maven.password')
    def mavenUrl = properties.getProperty('maven.url')

    repositories.mavenDeployer {
        repository(url: mavenUrl) {
            authentication(userName: userName, password: password)
        }
        pom.project {
            // 注意:这里要修改一下你的库。比如:com.baidu:lib:1.0.0
            groupId 'com.uso6.test'
            artifactId 'MavenTest'
            version '1.0' // 发布版本
            packaging 'aar'
        }
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.sourceFiles
    }

    artifacts {
        archives androidSourcesJar
    }
}

在上面可以看出我用户名、密码和Maven地址都是通过读取本地配置获取, 如果想简化, 可以直接替换成字符串。

2.2 本地配置

local.properties 中加入以下代码:

maven.url=http://192.168.0.254:10001/repository/maven-releases/
maven.user=nukix
maven.password=123456789

根据自己需求, 改成自己的用户名、密码和Maven地址即可。

2.3 上传

在 Gradle 菜单里面会出现 Tasks>upload>uploadArchives, 点击等待即可看到 maven 服务器上存在上传的内容。

2.4 使用

在项目的 build.gradle 中加入下列代码:

allprojects {
    repositories {
    	...
		maven {
            credentials {
                username 'nukix'
                password '123456789'
            }
            url "http://192.168.0.254:10001/repository/maven-public/"
        }
		...
    }
}

根据自己实际填即可。

最后在需要导入的模块中 implementation/api 进行导入。

2.5 本地仓库

这里指的是发布到本机特定位置, 不上传到 Maven 服务器。

2.5.1 Maven配置

Library 模块的 build.gradle 中添加以下代码:

apply plugin: 'maven'

uploadArchives{
    repositories.mavenDeployer{
        // Ubuntu 本地仓库路径, Windows 为(url:"file://D://***/***/***/")
        repository(url: uri("../../library"))
        // 唯一标识
        pom.groupId = "com.uso6.test"
        // 项目名称
        pom.artifactId = "MavenTest"
        // 版本号
        pom.version = "1.0"
    }
}

2.5.2 使用

在项目的 build.gradle 中加入下列代码:

def mavenUrl = "D:/AndroidStudioProjects/mavenTest/library" // Windows 路径
//def mavenUrl  = "/Users/nukix/mavenTest/library" // Linux 路径
allprojects {
    repositories {
    	...
    	// println(file(mavenUrl))
		maven {
            url uri(mavenUrl)
        }
		...
    }
}

根据自己实际填即可, 不知道路径可以打开注释打印出来看是否正确。

最后在需要导入的模块中 implementation/api 进行导入。

如果 library 间有嵌套, 可以使用下面代码进行嵌套

implementation('com.uso6.test:aaa:1.0') {
        exclude group: 'com.uso6.test', module: 'bbb'
}
implementation('com.uso6.test:bbb:1.0')

三、Gradle 7.0 +上传 maven (正文)

3.1 Maven配置

Library 模块的 build.gradle 中添加以下代码:

plugins {
    id 'com.android.library'
    id 'maven-publish'
}
...
afterEvaluate {
    publishing {

        // 读取本地配置文件
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newInputStream())
        def userName = properties.getProperty('maven.user')
        def passWord = properties.getProperty('maven.password')
        def mavenUrl = properties.getProperty('maven.url')

        repositories {
            maven {
                credentials {
                    username userName // 仓库发布用户名
                    password passWord // 仓库发布用户密码
                }
                url mavenUrl // 仓库地址
                allowInsecureProtocol = true // 是否允许不安全协议
            }
        }

        publications {
            release(MavenPublication) {
                from components.release
                groupId 'com.uso6.test'
                artifactId 'MavenTest'
                version '1.0' // 发布版本
                description 'This is a maven library test' // 说明描述
            }
        }
    }
}
...

在上面可以看出我用户名、密码和Maven地址都是通过读取本地配置获取, 如果想简化, 可以直接替换成字符串。

上面使用最精简的导入, 如果需要看详细一点的可以看下面代码, 不需要的跳过:

plugins {
    id 'com.android.library'
    id 'maven-publish'
}
...
task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
}

afterEvaluate {
    publishing {

        // 读取本地配置文件
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newInputStream())
        def userName = properties.getProperty('maven.user')
        def passWord = properties.getProperty('maven.password')
        def mavenUrl = properties.getProperty('maven.url')

        repositories {
            maven {
                credentials {
                    username userName // 仓库发布用户名
                    password passWord // 仓库发布用户密码
                }
                url mavenUrl // 仓库地址
                allowInsecureProtocol = true // 是否允许不安全协议
            }

        }

        publications {
            release(MavenPublication) {
                from components.release
                artifact androidSourcesJar

                groupId 'com.uso6.test'
                artifactId 'MavenTest'
                version '1.0' // 发布版本
                description 'This is a maven library test' // 说明描述
                pom { // pom 里面想看更详细的, 可以查看我参考文献
                    licenses {
                        license {
                            name = 'The Apache License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                }
            }

            debug(MavenPublication) {
                from components.debug
                groupId 'com.uso6.test'
                artifactId 'MavenTest'
                version '1.0' // 发布版本
                description 'This is a maven library test' // 说明描述
            }
        }
    }
}
...

这里加入了 artifact androidSourcesJar, 打包时会把源码打进去。 具体可以查看我下面的图。
Android Studio 发布 Maven Gradle 7.0+_第1张图片
在源代码没有加入 maven 的情况下, 编译器会出现 Shoose Sources… 按钮, 并且显示的代码时反编译之后的。 加了源代码后, 直接就显示源代码。

3.2 本地配置

local.properties 中加入以下代码:

maven.url=http://192.168.0.254:10001/repository/maven-releases/
maven.user=nukix
maven.password=123456789

根据自己需求, 改成自己的用户名、密码和Maven地址即可。

3.3 上传

在 Gradle 菜单里面会出现 Tasks>publishing>publish, 点击等待即可看到 maven 服务器上存在上传的内容。

3.4 使用

在项目的 settings.gradle 中加入下列代码:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon

        maven {
            credentials {
                username 'nukix'
                password '123456789'
            }
            url "http://192.168.0.254:10001/repository/maven-public/"
            allowInsecureProtocol = true // 是否允许不安全协议
        }
    }
}

根据自己实际填即可。

  • 注:Gradle 7.0+ 在项目的 build.gradle 中去除 allprojects, 然后放到了 settings.gradle 里面。

最后在需要导入的模块中 implementation/api 进行导入。

3.5 问题(没有遇到可以跳过)

1.Using insecure protocols with repositories, without explicit opt-in, has been deprecated. This is scheduled to be removed in Gradle 7.0. Switch Maven repository ‘m
aven(http://maven.aliyun.com/nexus/content/groups/public/)’ to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/6.8.3/dsl/or
g.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.
Configuration on demand is an incubating feature.

看上面, 加入 allowInsecureProtocol = true 就能解决。

2.A problem occurred evaluating root project ‘mavenTest’.
Build was configured to prefer settings repositories over project repositories but repository ‘maven’ was added by build file ‘build.gradle’

看上面, 去掉 allprojectssettings.gradle 加入 dependencyResolutionManagement 就能解决。

参考文献

  • Publish a multi-module Java/Android library to Maven Central + GitHub CI automation at 2021
  • 使用 Maven Publish 插件
  • Maven Publish Plugin

你可能感兴趣的:(android,maven,android,java,gradle,7.0+)