How to FTP a file from an Android Gradle build?

2019-02-13 15:24发布

问题:

I'm trying to FTP the signed APK after a Gradle build. I've already added the new build config that will sign the APK, but I'm stuck trying to figure out how to invoke an FTP task.

I found an official looking sample at section 59.6, however it complains that it cannot resolve dependency org.apache.ant:ant-commons-net:1.8.4. So apparently I'm missing something obvious here, like where to put a given jar file or reference it, although I thought maven would handle this sort of thing?

For reference, here is the linked sample which fails with a message about the dependency:

configurations {
    ftpAntTask
}

dependencies {
    ftpAntTask("org.apache.ant:ant-commons-net:1.8.4") {
        module("commons-net:commons-net:1.4.1") {
            dependencies "oro:oro:2.0.8:jar"
        }
    }
}

task ftp << {
    ant {
        taskdef(name: 'ftp',
                classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
                classpath: configurations.ftpAntTask.asPath)
        ftp(server: "ftp.apache.org", userid: "anonymous", password: "me@myorg.com") {
            fileset(dir: "htdocs/manual")
        }
    }
}

This fails with the message:

> Could not find org.apache.ant:ant-commons-net:1.8.4.

Here is my complete gradle.build file, with some sensitive information removed:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}

apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 17
    }

    signingConfigs {
        signed {
            storeFile file("(removed)")
            storePassword "(removed)"
            keyAlias "(removed)"
            keyPassword "(removed)"
        }
     }

    buildTypes {
        signed {
            debuggable false
            jniDebugBuild false
            signingConfig signingConfigs.signed
        }
    }
}

configurations {
    ftpAntTask
}

dependencies {
    ftpAntTask("org.apache.ant:ant-commons-net:1.8.4") {
        module("commons-net:commons-net:1.4.1") {
            dependencies "oro:oro:2.0.8:jar"
        }
    }
}

task ftp << {
    ant {
        taskdef(name: 'ftp',
                classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
                classpath: configurations.ftpAntTask.asPath)
        ftp(server: "(removed)", userid: "(removed)", password: "(removed)", remoteDir: "(removed)") {
            fileset(dir: "(removed)") {
                include(name: "(removed)")
            }
        }
    }
}

回答1:

You havn't declared a repository that can be used to resolve the declared artifacts. Try adding the following snippet to your build.gradle file:

repositories{
    mavenCentral()
}

cheers,

René



回答2:

You can also use the native ant get task which supports FTP, works with no outside dependencies:

  ant {
    get(src: "ftp://<hostname>/remote/path/to/file.jar", dest: "/local/path/to/file", username: 'anonymous', password: 'anonymous')
  }