Module: Pindo::BaseAndroidHelper

Included in:
AndroidBuildHelper, ApkHelper, BuildHelper, GradleHelper
Defined in:
lib/pindo/module/android/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#extract_config_block_groovy(signing_configs_block, config_type) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/pindo/module/android/base_helper.rb', line 229

def extract_config_block_groovy(signing_configs_block, config_type)
  # 支持 xxx { ... }
  all_blocks = signing_configs_block.scan(/([A-Za-z0-9_]+)\s*\{([\s\S]*?)^\s*\}/m)
  priority_names = [config_type, "Android_Sign", "release", "debug"]
  config_block = nil
  priority_names.each do |name|
    block = all_blocks.find { |n, b| n == name && b.include?("storeFile") }
    if block
      config_block = block[1]
      break
    end
  end
  if config_block.nil?
    all_blocks.each do |name, block|
      if block.include?("storeFile")
        config_block = block
        break
      end
    end
  end
  config_block
end

#extract_config_block_kts(signing_configs_block, config_type) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pindo/module/android/base_helper.rb', line 204

def extract_config_block_kts(signing_configs_block, config_type)
  # 支持 create("xxx") { ... }
  all_blocks = signing_configs_block.scan(/create\(["']?([A-Za-z0-9_]+)["']?\)\s*\{([\s\S]*?)^\s*\}/m)
  # 优先 release/debug/Android_Sign/自定义名
  priority_names = [config_type, "Android_Sign", "release", "debug"]
  config_block = nil
  priority_names.each do |name|
    block = all_blocks.find { |n, b| n == name && b.include?("storeFile") }
    if block
      config_block = block[1]
      break
    end
  end
  # 没找到就取第一个有 storeFile 的块
  if config_block.nil?
    all_blocks.each do |name, block|
      if block.include?("storeFile")
        config_block = block
        break
      end
    end
  end
  config_block
end

#extract_keystore_fields_common(fields, ext_values) ⇒ Object

通用字段递归解析



264
265
266
# File 'lib/pindo/module/android/base_helper.rb', line 264

def extract_keystore_fields_common(fields, ext_values)
  fields.map { |val| extract_var_or_value(val, ext_values) }
end

#extract_keystore_fields_groovy(config_block, ext_values) ⇒ Object



268
269
270
271
272
273
274
275
# File 'lib/pindo/module/android/base_helper.rb', line 268

def extract_keystore_fields_groovy(config_block, ext_values)
  # 只处理 groovy 语法
  store_file = config_block[/storeFile\s+file\(["']?([^\)"']+)["']?\)/, 1]
  store_password = config_block[/storePassword\s+["']?([^\)"']+)["']?/, 1]
  key_alias = config_block[/keyAlias\s+["']?([^\)"']+)["']?/, 1]
  key_password = config_block[/keyPassword\s+["']?([^\)"']+)["']?/, 1]
  extract_keystore_fields_common([store_file, store_password, key_alias, key_password], ext_values)
end

#extract_keystore_fields_kts(config_block, ext_values) ⇒ Object



277
278
279
280
281
282
283
284
285
# File 'lib/pindo/module/android/base_helper.rb', line 277

def extract_keystore_fields_kts(config_block, ext_values)
  # 只处理 kts 语法
  store_file = config_block[/storeFile\s*=\s*file\(["']?([^\)"']+)["']?\)/, 1] ||
               config_block[/storeFile\s*=\s*rootProject\.file\(["']?([^\)"']+)["']?\)/, 1]
  store_password = config_block[/storePassword\s*=\s*["']?([^\)"']+)["']?/, 1]
  key_alias = config_block[/keyAlias\s*=\s*["']?([^\)"']+)["']?/, 1]
  key_password = config_block[/keyPassword\s*=\s*["']?([^\)"']+)["']?/, 1]
  extract_keystore_fields_common([store_file, store_password, key_alias, key_password], ext_values)
end

#extract_signing_configs_groovy(content) ⇒ Object



198
199
200
201
202
# File 'lib/pindo/module/android/base_helper.rb', line 198

def extract_signing_configs_groovy(content)
  # 提取 signingConfigs { ... },支持多层嵌套
  match = content.match(/signingConfigs\s*\{([\s\S]*?)^\}/m)
  match ? match[1] : nil
end

#extract_signing_configs_kts(content) ⇒ Object

— 工具方法 —



192
193
194
195
196
# File 'lib/pindo/module/android/base_helper.rb', line 192

def extract_signing_configs_kts(content)
  # 提取 signingConfigs { ... },支持多层嵌套
  match = content.match(/signingConfigs\s*\{([\s\S]*?)^\}/m)
  match ? match[1] : nil
end

#extract_var_or_value(val, ext_values, depth = 0) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/pindo/module/android/base_helper.rb', line 287

def extract_var_or_value(val, ext_values, depth=0)
  return nil if val.nil? || val.empty? || depth > 5
  # 匹配 ${KEY}、$KEY、"${KEY}"、'${KEY}'
  if val =~ /^\$\{?([A-Za-z0-9_]+)\}?$/
    key = $1
    v = ext_values[key]
    v ? extract_var_or_value(v, ext_values, depth+1) : nil
  elsif val =~ /^"?\$\{([A-Za-z0-9_]+)\}"?$/
    key = $1
    v = ext_values[key]
    v ? extract_var_or_value(v, ext_values, depth+1) : nil
  else
    val
  end
end

#find_android_subproject(project_path) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/pindo/module/android/base_helper.rb', line 376

def find_android_subproject(project_path)
  android_dir = File.join(project_path, "Unity")
  return nil unless File.directory?(android_dir)

  main_module = get_main_module(android_dir)
  return nil unless main_module

  src_main = File.join(main_module, "src/main")
  return nil unless File.directory?(src_main)

  manifest = File.join(src_main, "AndroidManifest.xml")
  return nil unless File.exist?(manifest)

  android_dir
end

#fix_store_file_path(store_file, project_path) ⇒ Object



312
313
314
315
316
317
318
319
320
# File 'lib/pindo/module/android/base_helper.rb', line 312

def fix_store_file_path(store_file, project_path)
  return nil if store_file.nil? || store_file.empty?
  # 兼容 $rootDir、${rootDir}、project.rootDir、rootProject.file
  store_file = store_file.gsub(/^\$?\{?rootDir\}?\/?/, project_path + "/")
  store_file = store_file.gsub(/^project\.rootDir\/?/, project_path + "/")
  store_file = store_file.gsub(/^rootProject\.file\(['"](.+?)['"]\)/, project_path + '/\\1')
  store_file = File.expand_path(store_file, project_path) unless File.exist?(store_file)
  store_file
end

#get_build_toolsObject



4
5
6
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
# File 'lib/pindo/module/android/base_helper.rb', line 4

def get_build_tools
  # 获取 gem 资源文件路径
  
  pindo_dir ||= File.expand_path(ENV['PINDO_DIR'] || '~/.pindo')
  pindo_common_configdir ||= File.join(pindo_dir, "pindo_common_config")
  tools_dir = File.join(pindo_common_configdir, 'android_tools')

  # 如果开发环境找不到,尝试在 gem 安装目录找
  unless File.directory?(tools_dir)
    raise "缺少必要的构建工具: #{tools_dir},请执行pindo setup更新pindo配置"
  end

  # 检查必要的工具是否存在
  required_tools = {
    bundle_tool: 'bundletool.jar',
    gradlew: 'gradlew',
    gradle_wrapper: 'gradle-wrapper.jar'
  }

  tools = {}
  required_tools.each do |key, filename|
    path = File.join(tools_dir, filename)
    unless File.exist?(path)
      raise "缺少必要的构建工具: #{filename},请执行pindo setup更新pindo配置"
    end
    tools[key] = path
  end

  tools
end

#get_ext_values(project_path) ⇒ Object

新增方法:从 ext 配置文件中获取变量值



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/pindo/module/android/base_helper.rb', line 323

def get_ext_values(project_path)
  ext_values = {}

  ext_files = [
    File.join(project_path, "buildScripts/cfg/ext_signing.gradle"),
    File.join(project_path, "buildScripts/cfg/ext_signing.gradle.kts"),
    File.join(project_path, "gradle.properties"),
    File.join(project_path, "local.properties")
  ]

  ext_files.each do |file_path|
    next unless File.exist?(file_path)

    content = File.read(file_path)

    # 支持 ext.KEY = "VALUE"
    content.scan(/ext\.(\w+)\s*=\s*["']([^"']+)["']/m).each do |key, value|
      ext_values[key] = value
    end

    # 支持 ext["KEY"] = "VALUE"
    content.scan(/ext\[\s*["'](\w+)["']\s*\]\s*=\s*["']([^"']+)["']/m).each do |key, value|
      ext_values[key] = value
    end

    # 支持 KEY = "VALUE"
    content.scan(/^(\w+)\s*=\s*["']([^"']+)["']/m).each do |key, value|
      ext_values[key] = value
    end
  end

  ext_values
end

#get_keystore_config(project_path, debug = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pindo/module/android/base_helper.rb', line 106

def get_keystore_config(project_path, debug = false)
  main_module = get_main_module(project_path)
  return nil unless main_module

  gradle_kts_path = File.join(main_module, "build.gradle.kts")
  gradle_path     = File.join(main_module, "build.gradle")

  if File.exist?(gradle_kts_path)
    puts "KTS 项目,读取 #{File.basename(gradle_kts_path)} 文件"
    get_keystore_config_kts(gradle_kts_path, project_path, debug)
  elsif File.exist?(gradle_path)
    puts "Groovy 项目,读取 #{File.basename(gradle_path)} 文件"
    get_keystore_config_groovy(gradle_path, project_path, debug)
  else
    puts "未找到 build.gradle 或 build.gradle.kts 文件"
    nil
  end
end

#get_keystore_config_groovy(gradle_path, project_path, debug) ⇒ Object

处理 Groovy DSL (build.gradle)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pindo/module/android/base_helper.rb', line 159

def get_keystore_config_groovy(gradle_path, project_path, debug)
  content = File.read(gradle_path)
  puts "读取 #{File.basename(gradle_path)} 文件"
  signing_configs_block = extract_signing_configs_groovy(content)
  # puts "==== signingConfigs block ===="
  # puts signing_configs_block
  # puts "==============================" if signing_configs_block
  return nil unless signing_configs_block

  config_type = debug ? 'debug' : 'release'
  config_block = extract_config_block_groovy(signing_configs_block, config_type)
  # puts "==== config_block ===="
  # puts config_block
  # puts "=====================" if config_block
  return nil unless config_block

  config_block = remove_groovy_comments(config_block)
  ext_values = get_ext_values(project_path)
  store_file, store_password, key_alias, key_password = extract_keystore_fields_groovy(config_block, ext_values)
  store_file = fix_store_file_path(store_file, project_path)
  keystore_config = {
    store_file: store_file,
    store_password: store_password,
    key_alias: key_alias,
    key_password: key_password
  }
  if keystore_config.values.any?(&:nil?)
    puts "警告: 部分 keystore 配置为 nil: #{keystore_config}"
  end
  keystore_config
end

#get_keystore_config_kts(gradle_kts_path, project_path, debug) ⇒ Object

处理 Kotlin DSL (build.gradle.kts)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pindo/module/android/base_helper.rb', line 126

def get_keystore_config_kts(gradle_kts_path, project_path, debug)
  content = File.read(gradle_kts_path)
  puts "读取 #{File.basename(gradle_kts_path)} 文件"
  signing_configs_block = extract_signing_configs_kts(content)
  # puts "==== signingConfigs block ===="
  # puts signing_configs_block
  # puts "==============================" if signing_configs_block
  return nil unless signing_configs_block

  config_type = debug ? 'debug' : 'release'
  config_block = extract_config_block_kts(signing_configs_block, config_type)
  # puts "==== config_block ===="
  # puts config_block
  # puts "=====================" if config_block
  return nil unless config_block

  config_block = remove_kts_comments(config_block)
  ext_values = get_ext_values(project_path)
  store_file, store_password, key_alias, key_password = extract_keystore_fields_kts(config_block, ext_values)
  store_file = fix_store_file_path(store_file, project_path)
  keystore_config = {
    store_file: store_file,
    store_password: store_password,
    key_alias: key_alias,
    key_password: key_password
  }
  if keystore_config.values.any?(&:nil?)
    puts "警告: 部分 keystore 配置为 nil: #{keystore_config}"
  end
  keystore_config
end

#get_main_module(project_path) ⇒ Object

xml_content = File.read(strings_xml_path)

doc = Nokogiri::XML(xml_content)
doc.at_xpath("//string[@name='app_name']")&.text

end



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pindo/module/android/base_helper.rb', line 68

def get_main_module(project_path)
  settings_gradle_path = File.join(project_path, "settings.gradle")
  settings_gradle_kts_path = File.join(project_path, "settings.gradle.kts")
  
  # 优先使用 settings.gradle.kts,如果不存在则使用 settings.gradle
  if File.exist?(settings_gradle_kts_path)
    settings_gradle_path = settings_gradle_kts_path
  elsif !File.exist?(settings_gradle_path)
    return nil
  end

  content = File.read(settings_gradle_path)
  # 兼容 include ':app' 和 include(":app")
  modules = content.scan(/include\s*\(?\s*['\"]?([:\w\-]+)['\"]?\s*\)?/).flatten

  main_module = modules.find do |m|
    module_name = m.split(':').last
    gradle_path = File.join(project_path, module_name, "build.gradle")
    gradle_kts_path = File.join(project_path, module_name, "build.gradle.kts")
    
    # 优先使用 build.gradle.kts,如果不存在则使用 build.gradle
    if File.exist?(gradle_kts_path)
      gradle_path = gradle_kts_path
    end
    
    if File.exist?(gradle_path)
      gradle_content = File.read(gradle_path)
      gradle_content.include?("apply plugin: 'com.android.application") ||
        gradle_content.include?("id 'com.android.application") ||
        (gradle_content.include?("plugins {") && gradle_content.include?("com.android.application"))
    end
  end
  return nil unless main_module

  module_name = main_module.split(':').last
  File.join(project_path, module_name)
end

#modify_il2cpp_config(project_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/pindo/module/android/base_helper.rb', line 35

def modify_il2cpp_config(project_path)
  # 设置Il2CppOutputProject可执行权限
  system("chmod", "-R", "777",
  File.join(project_path, "unityLibrary/src/main/Il2CppOutputProject"))

  il2cpp_config_path = File.join(project_path, "unityLibrary/src/main/Il2CppOutputProject/IL2CPP/libil2cpp/il2cpp-config.h")
  content = File.read(il2cpp_config_path)
  content.gsub!("il2cpp::vm::Exception::Raise", "//il2cpp::vm::Exception::Raise")
  File.write(il2cpp_config_path, content)
end

#remove_desktop_google_service(project_path) ⇒ Object



46
47
48
49
50
51
# File 'lib/pindo/module/android/base_helper.rb', line 46

def remove_desktop_google_service(project_path)
  # 删除google-services-desktop.json
  desktop_google_service_path = File.join(project_path,
    "unityLibrary/src/main/assets/google-services-desktop.json")
  File.delete(desktop_google_service_path) if File.exist?(desktop_google_service_path)
end

#remove_groovy_comments(block) ⇒ Object



257
258
259
260
261
# File 'lib/pindo/module/android/base_helper.rb', line 257

def remove_groovy_comments(block)
  # 去除 // 和 /* ... */ 注释
  block = block.gsub(/\/\*[\s\S]*?\*\//, "")
  block.lines.reject { |line| line.strip.start_with?("//") }.join
end

#remove_kts_comments(block) ⇒ Object



252
253
254
255
# File 'lib/pindo/module/android/base_helper.rb', line 252

def remove_kts_comments(block)
  # 去除 // 注释
  block.lines.reject { |line| line.strip.start_with?("//") }.join
end

#resolve_var(val, ext_values, depth = 0) ⇒ Object



303
304
305
306
307
308
309
310
# File 'lib/pindo/module/android/base_helper.rb', line 303

def resolve_var(val, ext_values, depth=0)
  return val if val.nil? || depth > 5
  if ext_values[val]
    resolve_var(ext_values[val], ext_values, depth+1)
  else
    val
  end
end

#unity_android_project?(project_path) ⇒ Boolean

Returns:

  • (Boolean)


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/pindo/module/android/base_helper.rb', line 357

def unity_android_project?(project_path)
  # 检查 unityLibrary 模块是否存在
  unity_library_path = File.join(project_path, "unityLibrary")
  return false unless File.directory?(unity_library_path)

  # 检查 unityLibrary 的 build.gradle 或 build.gradle.kts 是否存在
  unity_gradle_path = File.join(unity_library_path, "build.gradle")
  unity_gradle_kts_path = File.join(unity_library_path, "build.gradle.kts")    
  if File.exist?(unity_gradle_kts_path)
    unity_gradle_path = unity_gradle_kts_path
  elsif !File.exist?(unity_gradle_path)
    return false
  end

  # 检查 build.gradle 中是否包含 Unity 特有的配置
  content = File.read(unity_gradle_path)
  content.include?("com.android.library") && content.include?("BuildIl2Cpp")
end