Skip to content

Commit

Permalink
fix: 修复“签名后的文件输出目录”不生效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiaoyong committed Jan 11, 2024
1 parent 99f6690 commit 6c19a7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/main/kotlin/io/github/jixiaoyong/pages/signapp/SignApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,21 @@ fun PageSignApp(
onChangePage(Routes.SignInfo)
})

val errorTips = "请先选择签名文件输出目录"
InfoItemWidget(
"签名文件输出目录",
signedDirectory ?: "请先选择签名文件输出目录",
"签名后的文件输出目录",
signedDirectory ?: errorTips,
buttonTitle = "修改目录",
onClick = {
scope.launch {
val outputDirectory =
FileChooseUtil.chooseSignDirectory(
window,
"请先选择签名文件输出目录",
signedDirectory?:currentApkFilePath
errorTips,
signedDirectory ?: currentApkFilePath
)
if (outputDirectory.isNullOrBlank()) {
scaffoldState.snackbarHostState.showSnackbar("请先选择签名文件输出目录")
scaffoldState.snackbarHostState.showSnackbar(errorTips)
} else {
settings.save(StorageKeys.SIGNED_DIRECTORY, outputDirectory)
scaffoldState.snackbarHostState.showSnackbar("修改成功")
Expand Down Expand Up @@ -285,6 +286,7 @@ fun PageSignApp(
localSelectedSignInfo.keyAlias,
localSelectedSignInfo.keyStorePassword,
localSelectedSignInfo.keyPassword,
signedApkDirectory = signedDirectory,
zipAlign = false,
signVersions = SignType.ALL_SIGN_TYPES.filter {
apkSignType.contains(
Expand Down
18 changes: 12 additions & 6 deletions src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,23 @@ object ApkSigner {
}

/**
* 对齐并签名APK文件
* 对齐并签名APK文件,签名后的文件添加_signed后缀,即x_signed.apk
* @param apkFilePath 待签名的apk文件绝对路径
* @param keyStorePath 签名文件路径
* @param keyAlias 表示 signer 在密钥库中的私钥和证书数据的别名的名称
* @param keyStorePwd 包含 signer 私钥和证书的密钥库的密码
* @param keyPwd signer 私钥的密码。
* @param zipAlign 是否需要对齐
* @param signedApkPath 签名之后的文件输出路径,默认为apkFilePath对应的x.apk添加_signed后缀,即x_signed.apk
* @param signedApkDirectory 签名之后的文件输出路径,默认为apkFilePath对应的x.apk所在的文件夹
* @return 返回结果 CommandResult 成功或失败,及信息
*/
fun alignAndSignApk(
apkFilePath: String, keyStorePath: String, keyAlias: String,
keyStorePwd: String, keyPwd: String, signedApkPath: String? = null,
apkFilePath: String,
keyStorePath: String,
keyAlias: String,
keyStorePwd: String,
keyPwd: String,
signedApkDirectory: String? = null,
zipAlign: Boolean = true,
signVersions: List<SignType> = SignType.DEF_SIGN_TYPES,
onProgress: (String) -> Unit
Expand All @@ -121,7 +125,9 @@ object ApkSigner {
return CommandResult.Error("请先初始化相关配置")
}

val outPutFilePath = signedApkPath ?: apkFilePath.replace(".apk", "_signed.apk")
val outputDirectory = signedApkDirectory ?: apkFilePath.substringBeforeLast(File.separator)
val outPutFilePath = outputDirectory + File.separator + apkFilePath.substringAfterLast(File.separator)
.replace(".apk", "_signed.apk")
val alignedApkFilePath = if (zipAlign) {
zipAlignApk(apkFilePath, onProgress = onProgress)
} else {
Expand All @@ -131,7 +137,7 @@ object ApkSigner {
try {
// 创建ProcessBuilder对象并设置相关属性
val signVersionParams = signVersions.flatMap {
arrayListOf("--v${it.type}-signing-enabled","true")
arrayListOf("--v${it.type}-signing-enabled", "true")
}.toTypedArray()
val processBuilder = ProcessBuilder()
processBuilder.command(
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/io/github/jixiaoyong/utils/SettingsTool.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jixiaoyong.utils

import com.google.gson.reflect.TypeToken
import com.russhwolf.settings.ExperimentalSettingsApi
import com.russhwolf.settings.ObservableSettings
import com.russhwolf.settings.Settings
import com.russhwolf.settings.coroutines.getStringOrNullFlow
Expand Down Expand Up @@ -37,16 +38,17 @@ interface KeyValueStorage {


enum class StorageKeys {
APK_SIGNER_PATH,
ZIP_ALIGN_PATH,
SIGN_INFO_SELECT,
SIGN_INFO_LIST,
SIGNED_DIRECTORY,
SIGN_TYPE_LIST;
APK_SIGNER_PATH, // 签名工具路径
ZIP_ALIGN_PATH, // 压缩工具路径
SIGN_INFO_SELECT, // 选中的签名信息
SIGN_INFO_LIST, // 签名信息(密钥/密码等)列表
SIGNED_DIRECTORY, // 签名后文件保存路径
SIGN_TYPE_LIST; // 签名类型列表

val key get() = this.name
}

@OptIn(ExperimentalSettingsApi::class)
class SettingsTool(private val scope: CoroutineScope) : KeyValueStorage {
private val settings: Settings by lazy { Settings() }
private val observableSettings: ObservableSettings by lazy { settings as ObservableSettings }
Expand Down

0 comments on commit 6c19a7e

Please sign in to comment.