Skip to content

Commit

Permalink
fix: 添加ApkSigner的时候添加提示和校验,fixes #6
Browse files Browse the repository at this point in the history
校验其内容是否包含The Android Open Source Project字样,有的话才尝试执行脚本
  • Loading branch information
jixiaoyong committed Jan 17, 2024
1 parent 95d1ec0 commit b1b9ce9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
31 changes: 18 additions & 13 deletions src/main/kotlin/io/github/jixiaoyong/pages/settings/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,26 @@ fun PageSettingInfo(window: ComposeWindow, settings: SettingsTool) {
)
}

InfoItemWidget("apk signer目录", apkSign ?: "尚未初始化", onClick = {
scope.launch {
val chooseFileName =
FileChooseUtil.chooseSignFile(window, "请选择apksigner文件")
if (chooseFileName.isNullOrBlank()) {
scaffoldState.snackbarHostState.showSnackbar("请选择apksigner文件")
} else {
val result = ApkSigner.setupApkSigner(chooseFileName)
saveApkSigner(settings, ApkSigner.apkSignerPath)
scaffoldState.snackbarHostState.showSnackbar(result ?: "修改成功")
InfoItemWidget("apk signer目录",
apkSign ?: "尚未初始化",
description = "请选择Android SDK中build-tools目录apksigner文件",
onClick = {
scope.launch {
val chooseFileName =
FileChooseUtil.chooseSignFile(window, "请选择apksigner文件")
if (chooseFileName.isNullOrBlank()) {
scaffoldState.snackbarHostState.showSnackbar("请选择apksigner文件")
} else {
val result = ApkSigner.setupApkSigner(chooseFileName)
saveApkSigner(settings, ApkSigner.apkSignerPath)
scaffoldState.snackbarHostState.showSnackbar(result ?: "修改成功")
}
}
}

})
InfoItemWidget("zip align目录", zipAlign ?: "尚未初始化", onClick = {
})
InfoItemWidget("zipalign目录", zipAlign ?: "尚未初始化",
description = "请选择Android SDK中build-tools目录zipalign文件",
onClick = {
scope.launch {
val chooseFileName = FileChooseUtil.chooseSignFile(window, "请选择zipAlign文件")
if (chooseFileName.isNullOrBlank()) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object ApkSigner {

private const val ANDROID_BUILD_TOOLS_DIR_EXAMPLE =
"/some/directory/to/your/android/sdk/build-tools/34.0.0/"
private const val AOSP_NAME = "The Android Open Source Project"

private lateinit var apkSignerCmdPath: String
private lateinit var zipAlignCmdPath: String
Expand Down Expand Up @@ -68,6 +69,14 @@ object ApkSigner {
}

fun setupApkSigner(apkSignerPath: String): String? {
// 校验文件是否为AOSP提供的apk signer,而非本APP避免误操作导致无限循环启动
val apkSignerFile = File(apkSignerPath)
if (!apkSignerFile.exists()) {
return "apkSigner命令不存在,请重新选择。"
} else if (!apkSignerFile.readText().contains(AOSP_NAME)) {
return "apkSigner命令不是${AOSP_NAME}官方提供的,请重新选择。"
}

return if (RunCommandUtil.runCommand(
"$apkSignerPath version",
"apk signer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object RunCommandUtil {

process.waitFor()
} catch (e: Exception) {
Logger.error("$tag error: $e")
-1
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/io/github/jixiaoyong/widgets/InfoItemWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Info
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -27,7 +30,8 @@ import androidx.compose.ui.unit.sp
@Composable
fun InfoItemWidget(
title: String,
description: String?,
value: String?,
description: String? = null,
buttonTitle: String? = null,
showChangeButton: Boolean = true,
onClick: (() -> Unit)? = null
Expand All @@ -50,6 +54,15 @@ fun InfoItemWidget(
Text(
title, style = TextStyle(fontWeight = FontWeight.ExtraBold, fontSize = 18.sp)
)
if (!description.isNullOrBlank()) HoverableTooltip(
description = description
) { modifier ->
Icon(
Icons.Default.Info,
contentDescription = "description information",
modifier = modifier
)
}
Spacer(modifier = Modifier.weight(1f))
if (showChangeButton) ButtonWidget(
{ onClick?.invoke() },
Expand Down

0 comments on commit b1b9ce9

Please sign in to comment.