Gradle打包上传Maven配置方案

本文详细介绍了如何使用Gradle配置发布任务,将Android库项目打包为AAR,并上传到Maven仓库。步骤包括创建publish.gradle文件、配置local.properties和gradle.properties、设置不同构建变体,以及执行上传任务。通过此流程,开发者可以便捷地管理和分享自己的Android库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在项目根目录创建 publish.gradle

apply plugin: 'maven-publish'

Properties properties = new Properties()
def file = new File(rootProject.projectDir,'local.properties')
if (null != file && file.exists()) {
    properties.load(file.newDataInputStream())
}

publishing {
    repositories {
        maven {//Maven仓库地址
            url 'http://xxxxxx/xxxxxx/xxxxxx/'
            credentials {
                username properties.getProperty('maven.username')
                password properties.getProperty('maven.password')
            }
        }
    }
}

2. 在local.properties配置自己Maven用户名密码

maven.username=littonishir
maven.password=xxxxxxxxxxxx

3. 在gradle.properties配置一些字段方便后续的管理。

IS_APPLICATION=false
ARTIFACTID=flavor
GROUPID=com.litton.ishir
VERSION=0.0.1.test001

4. 在需要配置上传aar的项目build.gradle中,做最后的配置。

  1. 引入 apply from: ‘…/publish.gradle’
  2. 编写afterEvaluate
  3. myflavor这里换成你自己libary的名字
if (IS_APPLICATION.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
apply plugin: 'kotlin-android'
apply from: '../publish.gradle'

android {
    compileSdk 31
    defaultConfig {
        if (IS_APPLICATION.toBoolean()) {
            applicationId "com.litton.myflavor"
        }
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
	...
    flavorDimensions "channel", "isPay"
    productFlavors {
        Huawei {
            dimension "channel"
        }
        Xiaomi {
            dimension "channel"
        }
        Pay {
            dimension "isPay"
        }
        Free {
            dimension "isPay"
        }
    }

    afterEvaluate {
        publishing {
            publications {
                myflavor(MavenPublication) {
                    from components.HuaweiPayRelease
                    artifactId ARTIFACTID
                    groupId GROUPID
                    version VERSION
                    println("Platform is ${groupId}:${artifactId}:${version}")
                }
            }
        }
    }
}

5. 执行Task上传即可 publishXXXPublicationToMavenRepository

PublicationToMavenRepository.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值