如何使用 gradle 将所有风格的 apk 上传到 nexus?

How can I upload all the flavor apks to nexus using gradle?

所以我已经成功地使用 uploadArchives 任务将 aar 库上传到 Nexus。 我现在想用 apks 做同样的事情,但目前还没有真正做到正确。 我几乎在做同样的事情,但包装不同。但这并不能解决问题。

我现在想知道使用 uploadArchives 任务是否真的是这样做的方法,或者我是否应该使用不同的任务。 这是我现在使用的代码,让我上传 aar 库:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: getPropertyValue('mUrl')+"/content/repositories/releases/") {
                authentication(userName: getPropertyValue('mUserName'), password: getPropertyValue('mPassword'))
            }

            def version = getPropertyValue('version')
            _productFlavors.each { name, config ->
                def releaseName = name+"Release"
                addFilter(releaseName) { artifact, file ->
                    artifact.attributes.classifier.equals(releaseName)
                }
                pom(releaseName).artifactId = "artifact"
                pom(releaseName).version = version
                pom(releaseName).groupId = "com.example."+name
                pom(releaseName).packaging = "apk"
            }
        }
    }
}

有点晚了,但我设法使用旧的普通 httpclient 使它工作。

def uploadToRepository(File file, String folder, String url, String userName, String password){
if(url != null && !url.isEmpty()){
    def client = new DefaultHttpClient()
    def post = new HttpPost("${url}/content/repositories/releases/${folder}/myapk.apk");
    def entity = new MultipartEntity()
    def fileBody = new FileBody(file, "application/vnd.android.package-archive", file.name)
    entity.addPart("file", fileBody)
    post.entity = entity

    def basicAuthString = "Basic " + "${userName}:${password}".bytes.encodeBase64().toString()
    post.addHeader('Authorization', basicAuthString);

    post.setEntity(entity);
    def response = client.execute(post);
    if(response.getStatusLine().getStatusCode() == 201)
        println "${file.name} uploaded"
}
}

uploadArchives {
    repositories {
        mavenDeployer {
            def version = getPropertyValue('version')
            def buildVersion = ''

            _productFlavors.each { name, config ->
                def releaseName = name+"Release"
                addFilter(releaseName) { artifact, file ->
                    artifact.attributes.classifier.equals(releaseName)
                }
                def fileName = "${project.buildDir}\outputs\apk\something.app-${name}-release.apk"
                def file = new File(fileName)
                uploadToRepository(
                        file,
                        "com/something/${name.replaceAll('_','/')}/something/${version+buildVersion}/",
                        getPropertyValue('mUrl'),
                        getPropertyValue('mUserName'),
                        getPropertyValue('mPassword')
                )
            }
        }
    }
}