Skip to content

Instantly share code, notes, and snippets.

@kimukou
Last active October 10, 2015 09:28
Show Gist options
  • Save kimukou/3669015 to your computer and use it in GitHub Desktop.
Save kimukou/3669015 to your computer and use it in GitHub Desktop.
android_groovyToyScript
android build.xml のオペレーションラッパー
ADT20環境で
 android update-project /android update test-project
を新規に行った環境を想定しています
target-project
 local.properties
 project.properties
  一部編集
test-project
 ant.properties
  tested.project.dir=XXX
 local.properties
 project.properties
が作成されている事を想定
//println("path => " + this.class.protectionDomain.codeSource.location.path)
//println("fileName => " + new File(this.class.protectionDomain.codeSource.location.file).name)
def loc=this.getClass().getProtectionDomain().getCodeSource().getLocation()
println "===start($loc)==="
def testClass =['com.nihon0tc.example.test.AdTest',
'com.nihon0tc.example.test.AdManagerTest#testManager']
def prop0 = new Properties()
fn = new FileInputStream('ant.properties')
prop0.load(fn)
def config_ant = new ConfigSlurper().parse(prop0)
def target_path = config_ant.tested.project.dir
println target_path
fn.close()
def manifestXml_target = new XmlSlurper().parse(new File("$target_path/build.xml"))
target_apkName = "$target_path/bin/${[email protected]()}-debug.apk"
println target_apkName
def prop1 = new Properties()
prop1.load(new FileInputStream("$target_path/local.properties"))
def config = new ConfigSlurper().parse(prop1)
def adb_home="${config.sdk.dir}/platform-tools"
def manifestXml = new XmlSlurper().parse(new File("AndroidManifest.xml"))
//println manifestXml.dump()
pkgName = [email protected]()
println pkgName
//println manifestXml.instrumentation.text()
targetName=manifestXml.instrumentation.'@android:targetPackage'.text()
println targetName
runnnerName=manifestXml.instrumentation.'@android:name'.text()
println runnnerName
def ant = new AntBuilder()
ant.buildnumber()
println ant.dump()
//read build.xml
import org.apache.tools.ant.Project
import org.apache.tools.ant.ProjectHelper
def antFile = new File("./build.xml")
def project = new Project()
project.init()
ProjectHelper.configureProject(project, antFile);
//project.executeTarget(project.defaultTarget);
project.executeTargets(['clean', 'debug'] as Vector) //[1]
//project.executeTargets(['debug'] as Vector) //[2]
//project.executeTargets(['clean', 'debug','install'] as Vector)//[3]複数端末は不可。
//see http://www.dzeta.jp/~junjis/code_reading/index.php?ant%2F%A5%D3%A5%EB%A5%C9%A5%D7%A5%ED%A5%BB%A5%B9%A4%F2%C6%C9%A4%E0
//=== after-copy ==
org_file = project.getProperty('out.final.file')
//=== after-copy-install ==
cmd=''
ant.exec(outputproperty:"cmdOut",
errorproperty: "cmdErr",
resultproperty:"cmdExit",
failonerror: "false",
dir:".",
executable: "${adb_home}/adb") {
arg(line:"devices")
}
println "[$cmd]<${ant.project.properties.cmdExit}>=${ant.project.properties.cmdOut}"
def deviceList = []
def txt =ant.project.properties.cmdOut
println txt
txt.split('\n').each{
if(""== it.trim() || it=~/List*/ )return;//same continure
device = it.split('\t')[0]
println device
deviceList.add device
}
println deviceList.dump()
//see http://developer.android.com/intl/ja/reference/android/test/InstrumentationTestRunner.html
//adb shell am instrument -w -e class com.android.foo.FooTest#testFoo com.android.foo/android.test.InstrumentationTestRunner
cmd=''
cnt =2
deviceList.each{
/*
cnt++
cmd ="-s $it uninstall $pkgName"
ant.exec(outputproperty:"cmdOut$cnt",
errorproperty: "cmdErr$cnt",
resultproperty:"cmdExit$cnt",
dir:".",
failonerror: "false",
executable: "${adb_home}/adb") {
arg(line:cmd)
}
println "[$cmd]<" + ant.project.properties."cmdExit$cnt" + ">=" + ant.project.properties."cmdOut$cnt"
//println "stderr: ${ant.project.properties.cmdErr2}"
*/
def apkarr =[org_file,target_apkName]
//insatall apk
apkarr.each{file->
cnt++
cmd = "-s $it install -r $file"
ant.exec(outputproperty:"cmdOut$cnt",
errorproperty: "cmdErr$cnt",
resultproperty:"cmdExit$cnt",
dir:".",
failonerror: "false",
executable: "${adb_home}/adb") {
arg(line:cmd)
}
println "[$cmd]<" + ant.project.properties."cmdExit$cnt" + ">=" + ant.project.properties."cmdOut$cnt"
}
//test run
testClass.each{testCls->
cnt++
cmd = "-s $it shell am instrument -w -e class $testCls $pkgName/$runnnerName"
println "[pre] === $cmd ==="
ant.exec(outputproperty:"cmdOut$cnt",
errorproperty: "cmdErr$cnt",
resultproperty:"cmdExit$cnt",
dir:".",
failonerror: "false",
executable: "${adb_home}/adb") {
arg(line:cmd)
}
println "[$cmd]<" + ant.project.properties."cmdExit$cnt" +">="+ ant.project.properties."cmdOut$cnt"
//println "stderr: ${ant.project.properties.cmdErr4}"
}
}
println "===end($loc)==="
def loc=this.getClass().getProtectionDomain().getCodeSource().getLocation()
println "===start($loc)==="
def prop1 = new Properties()
prop1.load(new FileInputStream('local.properties'))
def config = new ConfigSlurper().parse(prop1)
def adb_home="${config.sdk.dir}//platform-tools"
def apkName= "${config.release.app.name}_${config.release.app.version}.apk"
println apkName
def manifestXml = new XmlSlurper().parse(new File("AndroidManifest.xml"))
//println manifestXml.dump()
pkgName = [email protected]()
println pkgName
cmd = "${adb_home}/adb devices"
println cmd
def proc = cmd.execute()
proc.waitFor()
def deviceList = []
def txt =proc.in.text
println txt
txt.split('\n').each{it->
if(""== it.trim() || it=~/List*/ )return;//same continure
device = it.split('\t')[0]
println device
deviceList.add device
}
println deviceList.dump()
deviceList.each{it->
cmd = "${adb_home}/adb -s $it uninstall $pkgName"
proc = cmd.execute()
proc.waitFor()
println "[$cmd]<${proc.exitValue()}>=${proc.in.text}"
cmd = "${adb_home}/adb -s $it install $apkName"
proc = cmd.execute()
proc.waitFor()
println "[$cmd]<${proc.exitValue()}>=${proc.in.text}"
}
println "===end($loc)==="
def loc=this.getClass().getProtectionDomain().getCodeSource().getLocation()
println "===start($loc)==="
def prop1 = new Properties()
prop1.load(new FileInputStream('local.properties'))
def config = new ConfigSlurper().parse(prop1)
def adb_home="${config.sdk.dir}/platform-tools"
def apkName= "${config.release.app.name}_${config.release.app.version}.apk"
println apkName
def manifestXml = new XmlSlurper().parse(new File("AndroidManifest.xml"))
//println manifestXml.dump()
pkgName = [email protected]()
println pkgName
//def allActivity = manifestXml.application.activity
//allActivity.each{
// println it.dump()
//}
activity = manifestXml.application.activity[0].@'android:name'.text()
println activity
//exit(0)
def ant = new AntBuilder()
println ant.dump()
//read build.xml
import org.apache.tools.ant.Project
import org.apache.tools.ant.ProjectHelper
def antFile = new File("./build.xml")
def project = new Project()
project.init()
ProjectHelper.configureProject(project, antFile);
//project.executeTarget(project.defaultTarget);
project.executeTargets(['clean', 'release'] as Vector) //release-build
//see http://www.dzeta.jp/~junjis/code_reading/index.php?ant%2F%A5%D3%A5%EB%A5%C9%A5%D7%A5%ED%A5%BB%A5%B9%A4%F2%C6%C9%A4%E0
//=== after-copy ==
org_file = project.getProperty('out.final.file')
to_file="./${project.getProperty('out.dir')}/../${apkName}"
println "org_file=$org_file"
ant.echo "to_file=$to_file"
ant.copy(file:org_file,tofile:to_file,overwrite:true)
//=== after-copy-install ==
cmd=''
ant.exec(outputproperty:"cmdOut",
errorproperty: "cmdErr",
resultproperty:"cmdExit",
failonerror: "false",
dir:".",
executable: "${adb_home}/adb"){
arg(line:"devices")
}
println "[$cmd]<${ant.project.properties.cmdExit}>=${ant.project.properties.cmdOut}"
def deviceList = []
def txt =ant.project.properties.cmdOut
println txt
txt.split('\n').each{
if(""== it.trim() || it=~/List*/ )return;//same continure
device = it.split('\t')[0]
println device
deviceList.add device
}
println deviceList.dump()
cnt=1
cmd=''
deviceList.each{
//[TODO] uninstall apk section
cnt++
cmd ="-s $it uninstall $pkgName"
ant.exec(outputproperty:"cmdOut$cnt",
errorproperty: "cmdErr$cnt",
resultproperty:"cmdExit$cnt",
dir:".",
failonerror: "false",
executable: "${adb_home}/adb") {
arg(line:cmd)
}
println "[$cmd]<" + ant.project.properties."cmdExit$cnt" +">="+ ant.project.properties."cmdOut$cnt"
//println "stderr: ${ant.project.properties.cmdErr2}"
//[TODO] install apk section
cnt++
cmd = "-s $it install $apkName"
ant.exec(outputproperty:"cmdOut$cnt",
errorproperty: "cmdErr$cnt",
resultproperty:"cmdExit$cnt",
dir:".",
failonerror: "false",
executable: "${adb_home}/adb") {
arg(line:cmd)
}
println "[$cmd]<" + ant.project.properties."cmdExit$cnt" +">="+ ant.project.properties."cmdOut$cnt"
//println "stderr: ${ant.project.properties.cmdErr3}"
//[TODO] install apk rising section
cnt++
cmd = "-s $it shell am start -a install android.intent.action.MAIN -n ${pkgName}/${activity}"
ant.exec(outputproperty:"cmdOut$cnt",
errorproperty: "cmdErr$cnt",
resultproperty:"cmdExit$cnt",
dir:".",
failonerror: "false",
executable: "${adb_home}/adb") {
arg(line:cmd)
}
println "[$cmd]<" + ant.project.properties."cmdExit$cnt" +">="+ ant.project.properties."cmdOut$cnt"
}
println "===end($loc)==="
@kimukou
Copy link
Author

kimukou commented Sep 7, 2012

android build.xml のオペレーションラッパー

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment