Module: Pindo::ApkHelper

Includes:
BaseAndroidHelper
Included in:
AndroidBuildHelper
Defined in:
lib/pindo/module/android/apk_helper.rb

Instance Method Summary collapse

Methods included from BaseAndroidHelper

#find_android_subproject, #get_build_tools, #get_ext_values, #get_keystore_config, #get_main_module, #modify_il2cpp_config, #remove_desktop_google_service, #unity_android_project?

Instance Method Details

#build_aab(project_path, debug) ⇒ Object



85
86
87
88
89
# File 'lib/pindo/module/android/apk_helper.rb', line 85

def build_aab(project_path, debug)
  Dir.chdir(project_path) do
    system("./gradlew bundle#{debug ? 'Debug' : 'Release'}")
  end
end

#build_apk(project_path, debug) ⇒ Object

Raises:

  • (ArgumentError)


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?

  # 构建 AAB 文件
  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")
  }

  # 检查 bundle 文件是否存在
  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}"

  # 构建 APK
  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

  # 解压 APKs 文件
  unless system("unzip", "-o", paths[:output_apks], "-d", output_dir)
    raise RuntimeError, "APKS 解压失败"
  end

  # 返回生成的 APK 路径
  unless File.exist?(paths[:universal_apk])
    raise RuntimeError, "未找到生成的 APK 文件"
  end

  paths[:universal_apk]
end