Android Gradle 7.0 使用maven-publish发布组件

这篇博客详细记录了在使用Android Studio Arctic Fox和Gradle 7.0时遇到的问题及其解决方案。包括:1)如何配置允许使用不安全的http仓库;2)解决私有仓库组件依赖失败的问题,需要在settings.gradle中添加仓库配置;3)由于apply plugin: 'maven'不再支持,更新了使用maven-publish插件的发布脚本。作者提供了完整的配置代码示例,帮助开发者顺利进行项目构建和组件发布。

环境

Android Studio Arctic Fox | 2020.3.1 Patch 2
Build #AI-203.7717.56.2031.7678000, built on August 27, 2021
Runtime version: 11.0.10+0-b96-7249189 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.4.0-89-generic

问题1:http协议的仓库不安全

Gradle 7.0默认强制使用https的仓库地址,直接使用的话,会报错如下:

Could not resolve all dependencies for configuration ‘:classpath’.
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository ‘maven(http://...:8081/repository/maven-public/)’ to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.0.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.

解决1:设置allowInsecureProtocol

通过设置 allowInsecureProtocol 为 true,来支持http的仓库地址。详情见官方说明:UrlArtifactRepository:allowInsecureProtocol

buildscript {
    repositories {
        maven {
            allowInsecureProtocol true
            url 'http://xxx.xx.xx.xx:8081/repository/maven-public/'
        }
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

问题2:私有仓库组件依赖不成功

远程进行依赖时,旧版本可以依赖成功的一个组件,报错:

Failed to resolve: com.xx.xx:utils:1.0.0

1,检查project 的 build.gradle,明明已经设置好了maven地址引用。

buildscript {
    repositories {
        maven {
            allowInsecureProtocol true
            url 'http://xxx.xx.xx.xx:8081/repository/maven-public/'
            credentials {
                username = NEXUS_USERNAME
                password = NEXUS_PASSWORD
            }
        }
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

发现个问题,旧版本一般都有的allprojects {},不见了!!!,莫非是换了人间,经查询果然是!!!

2,找到“新人间”,根目录的settings.gradle

修改前

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
rootProject.name = "xxx"
include ':demo'
include ':Library'

修改后

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven {
            allowInsecureProtocol true
            url 'http://xxx.xxx.xxx.xxx:8081/repository/maven-public/'
        }
        google()
        mavenCentral()
    }
}
rootProject.name = "xxx"
include ':demo'
include ':Library'

问题3:apply plugin: ‘maven’ 不再支持,原先的上传脚本废了。

原来的脚本

//打包发布

apply plugin: 'maven'

//打包main目录下代码和资源的 task
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

uploadArchives {
    repositories.mavenDeployer {
        name = 'mavenCentralReleaseDeployer'
        repository(url: MAVEN_URL) {
            authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
        }
        snapshotRepository(url: MAVEN_SNAPSHOT_URL) {
            authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
        }
        pom.version = "1.1.2-SNAPSHOT"
        pom.artifactId = "xxx"
        pom.groupId = GROUP_ID
        pom.name = "xxx"
        pom.packaging = 'aar'

    }
}

artifacts {
    archives androidSourcesJar
}

新的Publish脚本,确实费了一番周折,参考了android官网 : Use the Maven Publish plugin 和 gradle官网 :Example 8. Publishing a Java library

//打包发布

apply plugin: 'maven-publish'

//打包main目录下代码和资源的 task
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

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 = GROUP_ID
                artifactId = 'xxxx'
                version = '1.0.0'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = GROUP_ID
                artifactId = 'xxx'
                version = '1.0.0-SNAPSHOT'
            }
        }
        repositories {
            maven {
                name = "nexus"
                allowInsecureProtocol true
                url MAVEN_GROUP_URL
                //nexus3没有开启匿名用户访问的话,添加认证信息
                credentials {
                    username = NEXUS_USERNAME
                    password = NEXUS_PASSWORD
                }
                def releasesRepoUrl = MAVEN_URL
                def snapshotsRepoUrl = MAVEN_SNAPSHOT_URL
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            }
        }
    }
}

至此,可以正常使用了。

呜谢

Maven Publish Plugin

publishing_maven

MavenArtifactRepository

maven-publish插件的使用

Android使用自己封装的maven-publish插件(maven 和 maven-publish),发布到自己或者公司的私有仓库流程

Android Studio Arctic Fox | 2020.3.1、Gradle 7.0升级记录

Android Studio Arctic Fox gradle 配置

Gradle 7.X Maven publish 适配

gradle7.0之后,android库发布至MavenCentral流程变化。

这是模块可以拍起来的libs配置[versions] android-gradle = "8.8.0" colorslider = "2.0.7" constraintlayout = "2.2.0" espresso = "3.6.1" junit = "4.13.2" junit-androidx = "1.2.1" kotlin = "2.1.0" material = "1.12.0" maven-publish = "0.30.0" recyclerview = "1.4.0" viewpager2 = "1.1.0" appcompat = "1.6.1" activity = "1.8.0" [libraries] colorslider = { module = "com.github.naz013:ColorSlider", version.ref = "colorslider" } constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" } espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" } junit = { module = "junit:junit", version.ref = "junit" } junit-androidx = { module = "androidx.test.ext:junit", version.ref = "junit-androidx" } material = { module = "com.google.android.material:material", version.ref = "material" } recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" } viewpager2 = { module = "androidx.viewpager2:viewpager2", version.ref = "viewpager2" } appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } activity = { group = "androidx.activity", name = "activity", version.ref = "activity" } [plugins] android-application = { id = "com.android.application", version.ref = "android-gradle" } android-library = { id = "com.android.library", version.ref = "android-gradle" } kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } maven-publish = { id = "com.vanniktech.maven.publish", version.ref = "maven-publish" },有哪些是对用于alias(libs.plugins.android.library)的
09-09
你提供的 `libs.versions.toml` 文件内容非常完整,已经包含了模块构建所需的大部分依赖和插件配置。 你问的是: > 哪些是对用于 `alias(libs.plugins.android.library)` 的? --- ## ✅ 答案 ```kotlin alias(libs.plugins.android.library) ``` 对应的是 `libs.versions.toml` 中的这个配置: ```toml [plugins] android-library = { id = "com.android.library", version.ref = "android-gradle" } ``` 也就是说,**`android-library` 是别名,它引用的是 `com.android.library` 插件,版本来自 `android-gradle`**。 --- ## 🔍 详细解释 在你的 `build.gradle.kts` 中: ```kotlin plugins { alias(libs.plugins.android.library) } ``` 这段代码使用了 **类型安全访问器(Type-safe Project Accessor)**,它是 Gradle 7.0+ 推出的功能,用来替代传统的 `id("xxx")` 写法。 - `libs.plugins.android.library` 是 Gradle 自动生成的访问器,指向你在 `libs.versions.toml` 中定义的插件别名。 - `android.library` 是你自己定义的别名,对应的是: ```toml android-library = { id = "com.android.library", version.ref = "android-gradle" } ``` Gradle 会自动将 `android.library` 转换为 `com.android.library` 插件,并使用 `android-gradle` 指定的版本(即 `"8.8.0"`)。 --- ## 📌 所以关键点是: | 用法 | 对应配置 | |------|----------| | `alias(libs.plugins.android.library)` | `android-library` 插件别名 | | 插件 ID | `com.android.library` | | 版本号 | `android-gradle = "8.8.0"` | --- ## ✅ 总结 你当前的 `libs.versions.toml` 中: ```toml [plugins] android-library = { id = "com.android.library", version.ref = "android-gradle" } ``` 就是用于支持: ```kotlin alias(libs.plugins.android.library) ``` 的那一行配置。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值