Skip to content

Instantly share code, notes, and snippets.

@ogaclejapan
Created January 12, 2013 13:56
Show Gist options
  • Save ogaclejapan/4518163 to your computer and use it in GitHub Desktop.
Save ogaclejapan/4518163 to your computer and use it in GitHub Desktop.
It is the build tool to install the apk from WebDAV. adb shell set tool/forMac or tool/forWindows extracted from the AndroidSDK.
buildscript {
repositories {
mavenCentral()
maven {
url 'http://sardine.googlecode.com/svn/maven/'
}
}
dependencies {
classpath 'com.googlecode.sardine:sardine:314'
}
}
project.ext {
baseUrl = 'http://192.168.0.1/apks/' //WebDAV URL of the apk is stored
sardine = SardineFactory.begin();
defaultApk = 'sample-debug-SNAPSHOT.apk'
}
import com.googlecode.sardine.SardineFactory
import org.apache.tools.ant.taskdefs.condition.Os
task wrapper(type: Wrapper) {
gradleVersion = '1.2'
}
//The console displays a list of apk that exist in directory WebDAV
task show(description: 'gradle[w] show') << {
showApks()
}
//Download the apk that exist in directory WebDAV
task download(description: 'gradle[w] download -[Ptarget=(filename)]') << {
def apk = project.defaultApk
if (project.hasProperty('target')) {
apk = target
}
downloadApk(apk)
}
//ADB installed directly over a device that is connected to the PC the apk that exist in WebDAV directory
task install(description: 'gradle[w] install [-Ptarget=(filename)] -Pforce=true') << {
def apk = project.defaultApk
if (project.hasProperty('target')) {
apk = target
}
def isForce = false
if (project.hasProperty('force')) {
isForce = force
}
installApk(apk, isForce)
}
boolean isApk(mime) {
mime.equals('application/vnd.android.package-archive')
}
void showApks() {
def dav = project.sardine
dav.list(project.baseUrl).collect {
if (isApk(it.contentType)) {
println "[${it.modified}] ${it.name}"
}
}
}
void downloadApk(name) {
downloadApk(name, true)
}
void downloadApk(name, overwrite) {
def out = new File(projectDir, name)
if (out.exists()) {
if (!overwrite) return
out.delete()
}
def apk = "${project.baseUrl}${name}"
def dav = project.sardine
if (!dav.exists(apk)) {
logger.error("file not found. ${apk}")
throw new StopActionException()
}
logger.lifecycle("download.. ${apk}")
out.withOutputStream { stream ->
dav.get(apk).eachByte { b ->
stream.write(b as int)
}
}
}
void installApk(name, isForce) {
downloadApk(name, isForce)
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
installApkForWindows(name, isForce)
return
}
if (Os.isFamily(Os.FAMILY_MAC)) {
installApkForMac(name, isForce)
return
}
logger.error("not supported os. must be windows or mac")
throw new StopActionException()
}
void installApkForWindows(name, isForce) {
adbInstall("${projectDir}\\tool\\forWindows\\adb", name, isForce)
}
void installApkForMac(name, isForce) {
adbInstall("${projectDir}/tool/forMac/adb", name, isForce)
}
void adbInstall(adb, apk, isForce) {
logger.lifecycle("adb install ${apk}")
def stdout = new StringBuffer()
def stderr = new StringBuffer()
def cmd = "${adb} install"
if (isForce) {
cmd = "${cmd} -r" //reinstall option
}
def proc = "${cmd} ${apk}".execute()
proc.consumeProcessOutput(stdout, stderr)
proc.waitForOrKill(1000 * 60) //wait for 1min
if (stdout.length() > 0) {
logger.lifecycle(stdout.toString())
}
if (stderr.length() > 0) {
logger.error(stderr.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment