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
|
# File 'lib/pindo/module/android/apk_helper.rb', line 7
def build_apk(project_path, debug)
raise ArgumentError, "项目路径不能为空" if project_path.nil? || project_path.empty?
unless build_aab(project_path, debug)
raise RuntimeError, "AAB 构建失败"
end
main_module = get_main_module(project_path)
raise ArgumentError, "无法找到主模块" unless main_module
keystore_config = get_keystore_config(project_path, debug)
raise ArgumentError, "无法从 build.gradle 中获取 keystore 信息" unless keystore_config
bundle_tool = get_build_tools[:bundle_tool]
raise ArgumentError, "找不到 bundletool" unless File.exist?(bundle_tool)
build_type = debug ? 'debug' : 'release'
main_module_name = File.basename(main_module)
output_dir = File.join(project_path, "build/apks")
FileUtils.rm_rf(output_dir)
FileUtils.mkdir_p(output_dir)
paths = {
output_apks: File.join(output_dir, "app.apks"),
bundle: File.join(main_module, "build/outputs/bundle/#{build_type}/#{main_module_name}-#{build_type}.aab"),
universal_apk: File.join(output_dir, "universal.apk")
}
unless File.exist?(paths[:bundle])
raise RuntimeError, "找不到 AAB 文件: #{paths[:bundle]}"
end
puts "解析 keystore 配置"
ks = keystore_config[:store_file]
puts "读取 keystore path = #{ks}"
ks_pass = keystore_config[:store_password]
puts "读取 keystore pass = #{ks_pass}"
key_alias = keystore_config[:key_alias]
puts "读取 key alias = #{key_alias}"
key_pass = keystore_config[:key_password]
puts "读取 key pass = #{key_pass}"
bundletool_cmd = [
"java -jar #{bundle_tool} build-apks",
"--bundle=#{paths[:bundle]}",
"--output=#{paths[:output_apks]}",
"--ks=#{ks}",
"--ks-pass=pass:#{ks_pass}",
"--ks-key-alias=#{key_alias}",
"--key-pass=pass:#{key_pass}",
"--mode=universal"
].join(" ")
unless system(bundletool_cmd)
raise RuntimeError, "APKS 构建失败"
end
unless system("unzip", "-o", paths[:output_apks], "-d", output_dir)
raise RuntimeError, "APKS 解压失败"
end
unless File.exist?(paths[:universal_apk])
raise RuntimeError, "未找到生成的 APK 文件"
end
paths[:universal_apk]
end
|