Gradle8.x上传aar到maven仓库

  • ~2.72K 字

为什么要调整?

Gradle升级后之前将本地library推送到maven的插件从maven变成了maven-publish,两者在使用上有一些区别

变更

  1. 修改前

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    uploadArchives{
    configuration = configurations.archives
    repositories {
    mavenDeployer {
    snapshotRepository(url: MAVEN_REPO_SNAPSHOT_URL) {
    authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
    }
    repository(url: MAVEN_REPO_RELEASE_URL) {
    authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
    }
    pom.project {
    version maven_version.name
    artifactId maven_version.id
    groupId GROUP_ID
    packaging TYPE
    description "XXX -> xxx@163.com"

    }
    }
    }
    }

    还需要手动编译sourceJar

    1
    2
    3
    4
    5
    6
    7
    task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
    }
    artifacts {
    archives sourcesJar
    }

    使用方法直接在右侧Gradle工具栏打开Tasks - upload 双击uploadArchives即可

等待脚本执行完毕即可在仓库中看到我们需要的aar和其他资源

  1. 修改后

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    afterEvaluate {
    publishing {
    publications {
    release(MavenPublication) {
    from components.release

    groupId = GROUP_ID
    artifactId = insQuality_maven_version.id
    version = insQuality_maven_version.name
    }
    }

    repositories {
    maven {
    allowInsecureProtocol true
    url MAVEN_REPO_RELEASE_URL
    credentials {
    username = NEXUS_USERNAME
    password = NEXUS_PASSWORD
    }
    }
    }
    }
    }

直接写from components.release在执行是会报错,因此需要在android节点下新增publishing相关配置

1
2
3
4
5
6
7
8
9
android {
...
publishing {
singleVariant("release") {
withSourcesJar()
withJavadocJar()
}
}
}

之前的sourcesJar方法也不需要了

直接双击publish或者选择其他脚本将相关library推送到maven,最终结果如下:

比使用旧插件生成的项目更多了。

最后在补充一个,升级到8.x以后,并且studio更新了,之前我们可以直接获取依赖里面的R文件获取资源数据,并且可以通过switch case使用R.id.xxx,因为id是常量,后续谷歌为了提升编译效率,调整了这些使用限制,我们项目中如果用到比较多的这种用法,需要如下调整:

gradle.properties文件中增加两行

1
2
3
4
# 可以直接使用依赖库的R文件
android.nonTransitiveRClass=false
# 可以只用常量R.id
android.nonFinalResIds=false

另外,升级之后,也不允许在AndroidManifest.xml文件中直接写 package="com.xxx.xxxx"了,需要在build.gradle文件中写namespace 'com.xxx.xxxx',BuildConfig也需要显式开启,在build.gradleandroid节点下的buildFeatures里手动开启

1
2
3
4
5
6
7
android {
...
buildFeatures{
buildConfig true
viewBinding true
}
}
赞助喵
非常感谢您的喜欢!
赞助喵
分享这一刻
让朋友们也来瞅瞅!