Search code examples
androidgradleandroid-studiobuild-script

Android Gradle Ant Scp Task


I'm moving to Android Studio so I'm migrating my existing ANT build process to Gradle. I'm new to Gradle though, so I'm hitting some issues in the process.

My current ANT script deploys my APK to a server using the scp task. I'd like for my Gradle script to do the same, but when I attempt to use the scp task in my Gradle script I receive the following error message:

Error:(58, 0) Problem: failed to create task or type scp Cause: the class org.apache.tools.ant.taskdefs.optional.ssh.Scp was not found. This looks like one of Ant's optional components. Action: Check that the appropriate optional JAR exists in -ANT_HOME/lib -the IDE Ant configuration dialogs

Do not panic, this is a common problem. The commonest cause is a missing JAR.

This is not a bug; it is a configuration problem

When I go to my ANT_HOME/lib directory, ant-jsch.jar is already there.

How can I use the ant scp task in my Gradle build script for Android?

Thanks for your help!

Edit to add existing ANT script

<!-- Deploy to server -->
<target name="deployAPK">
    <input
        addproperty="userName"
        message="Username for ${serverName}: " />

    <exec executable="scp" >
        <arg value="${out.absolute.dir}/${fileName}" />
        <arg value="${userName}@${serverName}:${deployDirectory}" />
    </exec>
</target>

ANT Dependency After further research, it seems that Gradle uses its own instance of ANT. This would explain why the ant-jsch.jar in my ANT_HOME isn't used, but I'm still not having any luck adding it to my dependencies.


Solution

  • The reason my dependencies for the ant scp task weren't working is because they were in the wrong place. After playing with my script some more, I got it working by moving my dependencies above my tasks and below my plugins. I learned something new about Gradle :).

    Hope this helps someone else!

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.12.+'
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    configurations {
        sshAntTask
    }
    
    apply plugin: 'android'
    
    dependencies {
        compile 'com.google.android.gms:play-services:4.+'
        androidTestCompile 'com.jayway.android.robotium:robotium-solo:3.5.1'
        sshAntTask 'org.apache.ant:ant-jsch:1.9.4'
    }
    
    android {
        compileSdkVersion 19
        buildToolsVersion '20.0.0'
    
        defaultConfig {
            // private
        }
    
        signingConfigs {
            release {
                // private
            }
        }
    
        buildTypes {
            debug {
                applicationVariants.all { variant ->
                    String filePath = outputFile.absolutePath
                    ant {
                        ant.taskdef(
                                name: 'scp',
                                classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp',
                                classpath: configurations.sshAntTask.asPath)
                        scp(file: filePath, todir: "DEPLOY_DIR_HERE")
                    }
                }
            }
    

    Just wanted to provide an update on this solution. After upgrading to Android Studio 1.0 and the latest version of Gradle, scp stopped working for me. The app would build successfully, but when it attempt to deploy would throw a "NoSuchMethodError" for the method "com.jcraft.jsch.JSchException".

    Ultimately, I found that Gradle now included its own jsch jar, so we no longer needed to include one in our dependencies. The fix was to change our sshAntTask dependency to:

    sshAntTask 'org.apache.ant:ant-jsch:1.9.4'

    Hope this helps someone!