Created
July 13, 2016 06:11
-
-
Save caad1229/b657d9b7c456f159edd1742a38bbf067 to your computer and use it in GitHub Desktop.
task for android to launching apk with build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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