1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| task downloadApk { doLast { def remoteApkPath = "/home/jason/AndroidStudioProjects/ncmusic/app/build/outputs/apk/debug/app-debug.apk" def localApkDir = System.getProperty("user.home") + "/Downloads/" def localApkPath = localApkDir + "app-debug.apk"
new File(localApkDir).mkdirs()
def command = "sshpass -p 'password' scp jason@192.168.1.100:${remoteApkPath} ${localApkPath}" println "Executing command: ${command}"
exec { commandLine 'bash', '-c', command } } }
task installAndStart { dependsOn downloadApk doLast { def localApkPath = System.getProperty("user.home") + "/Downloads/app-debug.apk"
def adbPath = '/Users/jason/Library/Android/sdk/platform-tools/adb' if (!new File(adbPath).exists()) { throw new GradleException("adb command not found at ${adbPath}! Please ensure Android SDK is properly installed.") }
def installCommand = "${adbPath} install -r ${localApkPath}"
try { exec { commandLine 'bash', '-c', installCommand } } catch (Exception e) { throw new GradleException("Failed to install APK: ${e.message}") }
def manifestFile = file("${rootProject.projectDir}/app/src/main/AndroidManifest.xml") def packageName = "" if (manifestFile.exists()) { manifestFile.eachLine { line -> if (line.contains("package=")) { packageName = line.split('package=')[1].split('"')[1] } } if (packageName == "") { throw new GradleException("Failed to extract package name from AndroidManifest.xml") } } else { throw new GradleException("AndroidManifest.xml file not found!") }
def launchCommand = "${adbPath} shell monkey -p ${packageName} -c android.intent.category.LAUNCHER 1" println "Executing command: ${launchCommand}"
try { exec { commandLine 'bash', '-c', launchCommand } } catch (Exception e) { throw new GradleException("Failed to launch application: ${e.message}") } } }
task downloadBuild { doLast { subprojects.each { subproject -> def remoteRootPath = "/home/jason/AndroidStudioProjects/" def remoteDirPath = remoteRootPath + "${rootProject.name}/${subproject.name}/build" def localDirPath = "${subproject.projectDir}"
new File(localDirPath).mkdirs()
def command = "sshpass -p 'password' rsync -avz --delete jason@192.168.1.100:${remoteDirPath} ${localDirPath}" println "Executing command: ${command}"
exec { commandLine 'bash', '-c', command } } } }
task remoteBuild { doLast { def command = "sshpass -p 'password' ssh jason@192.168.1.100 'cd /home/jason/AndroidStudioProjects/ncmusic/ && ./gradlew assembleDebug'" exec { commandLine 'bash', '-c', command } } }
task buildDownloadInstallAndStart { dependsOn remoteBuild finalizedBy installAndStart, downloadBuild }
|