Skip to content

Instantly share code, notes, and snippets.

@caad1229
Created July 13, 2016 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caad1229/b657d9b7c456f159edd1742a38bbf067 to your computer and use it in GitHub Desktop.
Save caad1229/b657d9b7c456f159edd1742a38bbf067 to your computer and use it in GitHub Desktop.
task for android to launching apk with build
def getSdkPath = {
def sdkDir
// local.propertiesからまずは読み込む
def localProperties = new File(rootDir, "local.properties")
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream { instr ->
properties.load(instr)
}
sdkDir = properties.getProperty('sdk.dir')
} else {
sdkDir = System.env.ANDROID_HOME;
}
return sdkDir;
}
def getConnectedDevice = {
def sdkDir = getSdkPath();
def adb = sdkDir + "/platform-tools/adb"
Process peco = 'peco'.execute()
Process devices = "$adb devices".execute()
Process sed = ["sed", "-e", "1,1d"].execute()
def device;
// pecoがある場合は選択できるようにする
if ("which peco".execute().text.isEmpty()) {
def list = devices.pipeTo(sed).text;
list.eachLine { line, no ->
if (!line.isEmpty()) {
device = line.split()[0] // <device name> device
return true;
}
}
} else {
def select = devices.pipeTo(sed).pipeTo(peco).text
if (!select.isEmpty()) {
device = select.split()[0] // <device name> device
}
}
return device;
}
task installToDevice(dependsOn: 'assembleDebug') << {
android.applicationVariants.all { variant ->
if (!variant.install) { return }
// set command
def sdkDir = getSdkPath();
def adb = sdkDir + "/platform-tools/adb"
def apkPath;
variant.outputs.each { output ->
if (output.zipAlign.outputFile.path.endsWith('.apk')) {
apkPath = output.zipAlign.outputFile.path;
return true;
}
}
if (apkPath.isEmpty()) { return; }
// set property
ext.device = getConnectedDevice();
ext.apkPath = apkPath;
if (device != null) {
println "$adb -s $device install -r $apkPath".execute().text
}
}
}
task startDebug(dependsOn: installToDevice) << {
android.applicationVariants.all { variant ->
if (!variant.install) { return }
if (ext.device == null) { return; }
def device = ext.device;
// set command
def sdkDir = getSdkPath();
def aapt = sdkDir + "/build-tools/$project.android.buildToolsRevision/aapt"
def adb = sdkDir + "/platform-tools/adb"
// get apk info
def result = "$aapt dump badging $apkPath".execute().text
// パッケージ名
def packageLine = result.readLines().find { it.startsWith "package:" }
def packageName
packageLine.eachMatch(".*name='(.*?)'.*") { all, match ->
packageName = match
}
// 起動アクティビティ
def launchableActivityLine = result.readLines().find {
it.startsWith "launchable-activity:"
}
def activityName = "com.hoge.example.MainActivity" // 取れなかった時のために初期化
if (launchableActivityLine != null) {
launchableActivityLine.eachMatch(".*name='(.*?)'.*") { all, match ->
activityName = match
}
}
// 起動のための情報
def intentAction = "android.intent.action.MAIN"
def intentCategory = "android.intent.category.LAUNCHER"
def activity = packageName + "/" + activityName
"$adb -s $device shell am start -a $intentAction -c $intentCategory -n $activity".execute()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment