build gradle 7.x 上传moudle打包的aar到远程仓库,能同时把本地的引用的一个aar一起发布上传吗

2023-07-14by@zhaozhenguoxyz-xrA6J&@ChatGPT.Model: gpt-3.5
zhaozhenguoxyz-xrA6J

build gradle 7.x 上传moudle打包的aar到远程仓库,能同时把本地的引用的一个aar一起发布上传吗

Avatar
Avatar

是的,你可以使用Gradle来上传一个moudle打包的aar到远程仓库,并同时发布上传本地引用的aar。以下是一个示例的Gradle配置:

  1. 在根目录的build.gradle文件中配置上传到远程仓库的任务:
buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.x'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }
}

apply plugin: 'maven'
apply plugin: 'com.github.dcendents.android-maven'

group = 'com.example'
version = '1.0.0'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'http://your_remote_repository') {
                authentication(userName: 'your_username', password: 'your_password')
            }
            pom.project {
                name 'Your Library Name'
                packaging 'aar'
                description 'Your library description'
                url 'http://your_library_url'
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'your_developer_id'
                        name 'Your Name'
                    }
                }
            }
        }
    }
}
  1. 在需要上传的module的build.gradle文件中,配置依赖关系和打包任务:
apply plugin: 'com.android.library'

android {
    // 配置Android构建信息
}

dependencies {
    // 配置本地引用的aar路径
    implementation files('libs/local_aar_library.aar')
    // 配置其他依赖关系
}

task packageAar(type: Copy) {
    from('build/outputs/aar')
    into('libs')
    include('*.aar')
}

// 定义上传aar任务依赖于打包aar任务
uploadArchives.dependsOn packageAar
  1. 在命令行中执行上传任务:
./gradlew uploadArchives

这样就可以将moudle打包的aar和本地引用的aar一起上传到远程仓库中了。请根据实际情况修改配置中的URL、用户名、密码、开发人员信息等。

zhaozhenguoxyz-xrA6J

uploadArchives 这是6.x的用法 7.x用的publishing

Avatar
Avatar

对不起,我之前提供的是旧版本的Gradle用法。确实,在Gradle 7.x版本中,uploadArchives任务已被废弃,应使用publishing插件来上传aar到远程仓库。以下是在Gradle 7.x版本中使用publishing插件的示例配置:

  1. 在根目录的build.gradle文件中配置发布到远程仓库的任务:
plugins {
    id 'maven-publish'
}

publishing {
    publications {
        mavenAar(MavenPublication) {
            groupId = 'com.example'
            artifactId = 'your_library_name'
            version = '1.0.0'
            
            // 配置aar文件
            artifact("$buildDir/outputs/aar/your_library_name-release.aar")
            
            // 配置pom文件
            pom {
                name 'Your Library Name'
                description 'Your library description'
                url 'http://your_library_url'
                
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                
                developers {
                    developer {
                        id 'your_developer_id'
                        name 'Your Name'
                    }
                }
            }
        }
    }
    
    repositories {
        maven {
            url 'http://your_remote_repository'
            credentials {
                username = 'your_username'
                password = 'your_password'
            }
        }
    }
}
  1. 在需要上传的module的build.gradle文件中,配置依赖关系和打包任务:
apply plugin: 'com.android.library'

android {
    // 配置Android构建信息
}

dependencies {
    // 配置本地引用的aar路径
    implementation files('libs/local_aar_library.aar')
    // 配置其他依赖关系
}

task packageAar(type: Copy) {
    from('build/outputs/aar')
    into('libs')
    include('*.aar')
}

// 定义发布aar任务依赖于打包aar任务
publish.dependsOn packageAar
  1. 在命令行中执行发布任务:
./gradlew publish

这样就可以将moudle打包的aar和本地引用的aar一起发布上传到远程仓库中了。请根据实际情况修改配置中的URL、用户名、密码、开发人员信息等。

zhaozhenguoxyz-xrA6J

本地引用的aar 一起上传后怎么引用呢

Avatar
Avatar

一旦将本地引用的aar一起上传到远程仓库,你可以通过在项目的build.gradle文件中添加相应的依赖来引用它。

  1. 在项目的build.gradle文件中,添加远程仓库的URL:
allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url 'http://your_remote_repository'
            credentials {
                username = 'your_username'
                password = 'your_password'
            }
        }
    }
}
  1. 在模块的build.gradle文件中,添加对aar的依赖:
dependencies {
    implementation 'com.example:your_library_name:1.0.0'
    // 其他依赖关系
}

请确保在添加依赖时使用正确的groupIdartifactIdversion来匹配上传的aar的信息。

然后,重新同步项目,Gradle会从远程仓库下载并引用上传的aar文件。这样,你就可以在项目中使用上传的aar库了。

如果你在本地开发时不想使用远程仓库的aar,可以将本地引用的aar修改为远程引用,如:

dependencies {
    implementation 'com.example:your_local_aar_library:1.0.0'
    // 其他依赖关系
}

然后,在发布版本时再切换回远程仓库的引用。

记得在使用远程仓库的用户名和密码时,确保保密性和安全性,不要将敏感信息直接暴露在脚本中。

zhaozhenguoxyz-xrA6J

上传本地饮用的aar到远程仓库怎么在添加依赖时使用正确的groupId、artifactId和version来匹配上传的aar的信息

Avatar
Avatar

当你上传本地引用的aar到远程仓库时,你需要确保在添加依赖时使用正确的groupIdartifactIdversion来匹配上传的aar的信息。以下是一些步骤来确保匹配正确的信息:

  1. 在本地引用的aar的build.gradle文件中,确保设置了正确的grouparchivesBaseNameversion属性。例如:
group = 'com.example'
archivesBaseName = 'your_local_aar_library'
version = '1.0.0'
  1. 在上传aar到远程仓库的时候,在根目录的build.gradle文件中,确保设置了正确的groupIdartifactIdversion属性。例如:
group = 'com.example'
version = '1.0.0'
  1. 在上传aar的时候,使用正确的groupIdartifactIdversion属性来配置MavenPublication的信息。例如:
publishing {
    publications {
        mavenAar(MavenPublication) {
            groupId = 'com.example'
            artifactId = 'your_local_aar_library'
            version = '1.0.0'
            
            // 其他配置
        }
    }
    
    // 其他配置
}
  1. 最后,在项目的build.gradle文件中使用正确的groupIdartifactIdversion来添加依赖。例如:
dependencies {
    implementation 'com.example:your_local_aar_library:1.0.0'
    // 其他依赖关系
}

确保在这些步骤中使用相同的groupIdartifactIdversion来匹配上传的aar的信息,这样就能正确引用上传到远程仓库的aar了。