Skip to content

Commit

Permalink
fix: 修改添加、校验apksinger等命令的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiaoyong committed Jan 18, 2024
1 parent 6b690a1 commit ce6b230
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
23 changes: 10 additions & 13 deletions src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object ApkSigner {
return "指定的Android SDK中build-tools目录无效或不存在,请重新选择。\n该目录一般为${ANDROID_BUILD_TOOLS_DIR_EXAMPLE}"
}

val apkSignerPath = "$androidBuildToolsDir/apksigner"
val apkSignerPath = "$androidBuildToolsDir${File.separator}apksigner"
// windows追加bat
val apkSignerPathWithBat = if (System.getProperties().getProperty("os.name").contains("Windows")) {
"$apkSignerPath.bat"
Expand Down Expand Up @@ -77,14 +77,10 @@ object ApkSigner {
return "apkSigner命令不是${AOSP_NAME}官方提供的,请重新选择。"
}

return if (RunCommandUtil.runCommand(
"$apkSignerPath version",
"apk signer",
true,
false
) != 0
) {
"apkSigner命令不存在,请重新选择。"
val result = RunCommandUtil.runCommand("$apkSignerPath version", "apk signer")

return if (result != null) {
"apkSigner命令检查失败,请重试(${result.message}"
} else {
apkSignerCmdPath = apkSignerPath
null
Expand All @@ -93,14 +89,15 @@ object ApkSigner {

fun setupZipAlign(zipAlignPath: String): String? {
// check os is mac/linux or windows
val isCommandExits = if (System.getProperties().getProperty("os.name").contains("Windows")) {
val result = if (System.getProperties().getProperty("os.name").contains("Windows")) {
File(zipAlignPath).exists()
null
} else {
RunCommandUtil.runCommand("command -v $zipAlignPath", "zip align", true, false) == 0
RunCommandUtil.runCommand("command -v $zipAlignPath", "zip align")
}

return if (!isCommandExits) {
"zipAlign命令不存在,请重新选择。"
return if (null != result) {
"zipAlign命令检查失败,请重试(${result.message}"
} else {
zipAlignCmdPath = zipAlignPath
null
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/io/github/jixiaoyong/utils/FileChooseUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ object FileChooseUtil {
jFileChooser.currentDirectory = oldDirectory?.let { File(it) }
jFileChooser.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
jFileChooser.showDialog(window.glassPane, "选择")
return jFileChooser.currentDirectory?.absolutePath
// macos 选择文件夹只需点中,而不能打开,否则就会出现选中的是/xxx/a但下面返回的是/xxx/a/a的情况
return jFileChooser.selectedFile?.absolutePath
}
}
25 changes: 21 additions & 4 deletions src/main/kotlin/io/github/jixiaoyong/utils/RunCommandUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,43 @@ import java.io.InputStreamReader
*/
object RunCommandUtil {

fun runCommand(command: String, tag: String = "", printLog: Boolean = true, printError: Boolean = true): Int {
/**
* 执行bash命令
* @return 成功返回null,失败返回Exception类型错误原因
*/
fun runCommand(
command: String,
tag: String = "",
printLog: Boolean = true,
printError: Boolean = true
): Exception? {
val logTag = if (tag.isBlank()) "" else "$tag: "

Logger.info("$tag command: $command")

return try {
val process = Runtime.getRuntime().exec(command)
val logBuffer = StringBuffer()

if (printLog) {
BufferedReader(InputStreamReader(process.inputStream)).useLines {
it.forEach { line ->
Logger.info("${logTag}$line")
logBuffer.append(line).append("\n")
}
}
}

process.waitFor()
val result = process.waitFor()
if (result == 0) {
null
} else {
Exception("${logTag}exit code: $result\n${logBuffer}")
}

} catch (e: Exception) {
Logger.error("$tag error: $e")
-1
if (printError) Logger.error("$tag error: $e")
e
}
}
}

0 comments on commit ce6b230

Please sign in to comment.