pindo 5.10.6 → 5.10.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pindo/base/funlog.rb +12 -1
  3. data/lib/pindo/base/pindocontext.rb +3 -0
  4. data/lib/pindo/command/android/autobuild.rb +62 -9
  5. data/lib/pindo/command/android/build.rb +6 -6
  6. data/lib/pindo/command/android/debug.rb +9 -9
  7. data/lib/pindo/command/android.rb +1 -1
  8. data/lib/pindo/command/appstore.rb +1 -1
  9. data/lib/pindo/command/deploy/build.rb +2 -4
  10. data/lib/pindo/command/deploy/cert.rb +4 -5
  11. data/lib/pindo/command/deploy/configproj.rb +3 -3
  12. data/lib/pindo/command/deploy/confusecode.rb +1 -1
  13. data/lib/pindo/command/deploy/confuseproj.rb +1 -1
  14. data/lib/pindo/command/deploy/pem.rb +3 -4
  15. data/lib/pindo/command/dev/autobuild.rb +1 -1
  16. data/lib/pindo/command/dev/build.rb +1 -1
  17. data/lib/pindo/command/dev/feishu.rb +1 -1
  18. data/lib/pindo/command/gplay/pullconfig.rb +48 -0
  19. data/lib/pindo/command/gplay.rb +7 -6
  20. data/lib/pindo/command/ios/adhoc.rb +6 -5
  21. data/lib/pindo/command/ios/autobuild.rb +24 -24
  22. data/lib/pindo/command/ios/build.rb +7 -6
  23. data/lib/pindo/command/ios/debug.rb +1 -0
  24. data/lib/pindo/command/ios.rb +1 -1
  25. data/lib/pindo/command/ipa/import.rb +2 -3
  26. data/lib/pindo/command/ipa/output.rb +2 -3
  27. data/lib/pindo/command/jps/upload.rb +6 -5
  28. data/lib/pindo/command/unity/apk.rb +19 -2
  29. data/lib/pindo/command/unity/autobuild.rb +58 -63
  30. data/lib/pindo/command/unity/initpack.rb +1 -1
  31. data/lib/pindo/command/unity/ipa.rb +17 -14
  32. data/lib/pindo/command/unity/pack.rb +1 -1
  33. data/lib/pindo/command/unity/upload.rb +1 -1
  34. data/lib/pindo/command/unity/web.rb +2 -2
  35. data/lib/pindo/command/utils/icon.rb +1 -1
  36. data/lib/pindo/command/utils/renewcert.rb +1 -2
  37. data/lib/pindo/command/utils/renewproj.rb +9 -10
  38. data/lib/pindo/command/web/autobuild.rb +2 -2
  39. data/lib/pindo/command/web/run.rb +1 -1
  40. data/lib/pindo/command.rb +2 -1
  41. data/lib/pindo/module/android/android_build_config_helper.rb +267 -35
  42. data/lib/pindo/module/android/android_build_helper.rb +300 -0
  43. data/lib/pindo/module/android/android_project_helper.rb +279 -0
  44. data/lib/pindo/module/android/gp_compliance_helper.rb +33 -87
  45. data/lib/pindo/module/android/gradle_helper.rb +524 -255
  46. data/lib/pindo/module/android/java_env_helper.rb +633 -0
  47. data/lib/pindo/module/android/keystore_helper.rb +1118 -0
  48. data/lib/pindo/module/appselect.rb +109 -0
  49. data/lib/pindo/module/appstore/appstore_in_app_purchase.rb +1 -1
  50. data/lib/pindo/module/build/{buildhelper.rb → build_helper.rb} +1 -2
  51. data/lib/pindo/module/build/{commonconfuseproj.rb → confuse_xcodeproj.rb} +2 -2
  52. data/lib/pindo/module/cert/cert_helper.rb +245 -0
  53. data/lib/pindo/module/cert/keychain_helper.rb +152 -0
  54. data/lib/pindo/module/cert/pem_helper.rb +67 -0
  55. data/lib/pindo/module/cert/xcodecerthelper.rb +2 -2
  56. data/lib/pindo/module/pgyer/pgyerhelper.rb +21 -1
  57. data/lib/pindo/module/{build/unityhelper.rb → unity/unity_helper.rb} +0 -17
  58. data/lib/pindo/module/xcode/{xcodereshandler.rb → res/xcode_res_handler.rb} +1 -1
  59. data/lib/pindo/module/xcode/{xcodebuildconfig.rb → xcode_build_config.rb} +7 -6
  60. data/lib/pindo/module/xcode/{xcodebuildhelper.rb → xcode_build_helper.rb} +4 -3
  61. data/lib/pindo/module/xcode/{xcodehelper.rb → xcode_project_helper.rb} +4 -2
  62. data/lib/pindo/module/xcode/{xcodereshelper.rb → xcode_res_helper.rb} +12 -9
  63. data/lib/pindo/version.rb +185 -7
  64. data/lib/pindo.rb +14 -9
  65. metadata +24 -23
  66. data/lib/pindo/module/android/apk_helper.rb +0 -138
  67. data/lib/pindo/module/android/base_helper.rb +0 -964
  68. data/lib/pindo/module/android/build_helper.rb +0 -128
  69. data/lib/pindo/module/android/so_helper.rb +0 -18
  70. data/lib/pindo/module/cert/certhelper.rb +0 -246
  71. data/lib/pindo/module/cert/keychainhelper.rb +0 -150
  72. data/lib/pindo/module/cert/pemhelper.rb +0 -65
  73. /data/lib/pindo/module/android/{androidreshelper.rb → android_res_helper.rb} +0 -0
  74. /data/lib/pindo/module/appstore/{iap_tier.json → appstore_iap_tier.json} +0 -0
  75. /data/lib/pindo/module/build/{swarkhelper.rb → swark_helper.rb} +0 -0
  76. /data/lib/pindo/module/build/{versionhelper.rb → version_helper.rb} +0 -0
  77. /data/lib/pindo/module/cert/{provisioninghelper.rb → provisioning_helper.rb} +0 -0
  78. /data/lib/pindo/module/unity/{nugethelper.rb → nuget_helper.rb} +0 -0
  79. /data/lib/pindo/module/xcode/{xcoderesconstant.rb → res/xcode_res_constant.rb} +0 -0
@@ -0,0 +1,300 @@
1
+ require 'singleton'
2
+ require_relative 'android_project_helper'
3
+ require_relative 'gradle_helper'
4
+ require_relative 'gp_compliance_helper'
5
+ require_relative 'java_env_helper'
6
+ require_relative 'keystore_helper'
7
+ require 'fileutils'
8
+
9
+ module Pindo
10
+ class AndroidBuildHelper
11
+ include Singleton
12
+
13
+
14
+
15
+ class << self
16
+ def share_instance
17
+ instance
18
+ end
19
+ end
20
+
21
+
22
+ def auto_build_apk(project_dir, debug = false, ignore_sub = false)
23
+ raise ArgumentError, "项目目录不能为空" if project_dir.nil?
24
+ raise ArgumentError, "项目目录不存在" unless File.directory?(project_dir)
25
+
26
+ # 设置 Java Home 环境变量
27
+ Pindo::JavaEnvHelper.setup_java_home_from_project(project_dir)
28
+
29
+ # 检查并配置 Gradle Wrapper
30
+ Pindo::GradleHelper.check_gradle_with_project(project_dir)
31
+
32
+ # 1. 处理独立的 Unity 导出工程
33
+ if Pindo::AndroidProjectHelper.unity_android_project?(project_dir)
34
+ puts "处理独立的 Unity 导出工程..."
35
+ Pindo::GradleHelper.update_build_gradle(project_dir)
36
+ Pindo::AndroidProjectHelper.add_unity_namespace(project_dir)
37
+ Pindo::AndroidProjectHelper.modify_il2cpp_config(project_dir)
38
+ Pindo::AndroidProjectHelper.remove_desktop_google_service(project_dir)
39
+
40
+ # 2. 处理 Unity 作为 lib 的原生工程(Unity 放在 Unity 目录下)
41
+ elsif Pindo::AndroidProjectHelper.unity_as_lib_android_project?(project_dir)
42
+ puts "处理 Unity 作为 lib 的原生工程..."
43
+ unity_dir = File.join(project_dir, "Unity")
44
+
45
+ if File.directory?(unity_dir)
46
+ puts "找到 Unity 模块目录: Unity/"
47
+
48
+ # 如果 Unity 子目录是完整的 Unity 导出工程,对其进行处理
49
+ if Pindo::AndroidProjectHelper.unity_android_project?(unity_dir)
50
+ puts "Unity 子目录是完整的 Unity 导出工程,执行 Unity 特有处理..."
51
+
52
+ # 更新 Gradle 配置 - update_build_gradle 现在会自动处理主工程和 Unity 模块
53
+ Pindo::GradleHelper.update_build_gradle(project_dir)
54
+
55
+ # Unity 特有的处理
56
+ Pindo::AndroidProjectHelper.add_unity_namespace(unity_dir)
57
+ Pindo::AndroidProjectHelper.modify_il2cpp_config(unity_dir)
58
+ Pindo::AndroidProjectHelper.remove_desktop_google_service(unity_dir)
59
+ end
60
+
61
+ # 构建 SO 库(如果使用 IL2CPP)
62
+ # 检查是否存在 IL2CPP 输出目录(表示使用 IL2CPP 而非 Mono)
63
+ il2cpp_dir = File.join(unity_dir, "unityLibrary/src/main/Il2CppOutputProject")
64
+ if File.directory?(il2cpp_dir)
65
+ puts "检测到 IL2CPP 项目,构建 Unity 模块的 SO 库..."
66
+ unless build_so_library(unity_dir)
67
+ raise RuntimeError, "编译 Unity 模块 SO 库失败"
68
+ end
69
+ copy_so_files(unity_dir, project_dir)
70
+
71
+ # SO 库拷贝完成后,重新配置主工程的 Java Home 和 Gradle 环境
72
+ puts "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
73
+ puts "Unity SO库编译完成,切换到主工程编译环境"
74
+ puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
75
+ Pindo::JavaEnvHelper.setup_java_home_from_project(project_dir, use_main: true)
76
+ Pindo::GradleHelper.check_gradle_with_project(project_dir, configure_main: true)
77
+
78
+ # 检查并配置主工程的 local.properties
79
+ Pindo::AndroidProjectHelper.check_main_local_properties(project_dir)
80
+ else
81
+ puts "未检测到 IL2CPP 目录,可能使用 Mono 后端,跳过 SO 库构建"
82
+ end
83
+ else
84
+ puts "警告:Unity 作为 lib 的工程,但未找到 Unity 目录"
85
+ end
86
+
87
+ # 3. 处理 Android 原生工程
88
+ else
89
+ puts "处理 Android 原生工程..."
90
+ # 更新 Gradle 配置,确保使用兼容的版本
91
+ Pindo::GradleHelper.update_build_gradle(project_dir)
92
+ end
93
+
94
+ # 配置 Keystore 签名
95
+ build_type = debug ? "debug" : "release"
96
+ puts "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
97
+ puts "配置 Keystore 签名 (#{build_type})"
98
+ puts "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
99
+
100
+ if Pindo::KeystoreHelper.apply_keystore_config(project_dir, build_type)
101
+ puts "✓ Keystore 签名配置成功"
102
+ else
103
+ puts "⚠ Keystore 签名配置跳过(使用工程原有配置)"
104
+ end
105
+
106
+ # 构建 APK
107
+ build_apk(project_dir, debug)
108
+ end
109
+
110
+ def build_apk(project_path, debug)
111
+ raise ArgumentError, "项目路径不能为空" if project_path.nil? || project_path.empty?
112
+
113
+ # 构建 AAB 文件
114
+ unless build_aab(project_path, debug)
115
+ raise RuntimeError, "AAB 构建失败"
116
+ end
117
+
118
+ # 获取必要的配置信息
119
+ main_module = Pindo::AndroidProjectHelper.get_main_module(project_path)
120
+ raise ArgumentError, "无法找到主模块" unless main_module
121
+
122
+ keystore_config = Pindo::KeystoreHelper.get_keystore_config_from_project(project_path, debug)
123
+ raise ArgumentError, "无法从 build.gradle 中获取 keystore 信息" unless keystore_config
124
+
125
+ build_tools = Pindo::AndroidProjectHelper.get_build_tools
126
+ unless build_tools
127
+ Funlog.error("无法继续构建:缺少必要的构建工具")
128
+ return nil
129
+ end
130
+
131
+ bundle_tool = build_tools[:bundle_tool]
132
+ unless bundle_tool && File.exist?(bundle_tool)
133
+ Funlog.error("无法找到 bundletool.jar")
134
+ return nil
135
+ end
136
+
137
+ # 准备输出路径
138
+ build_type = debug ? 'debug' : 'release'
139
+ main_module_name = File.basename(main_module)
140
+ output_dir = File.join(project_path, "build/apks")
141
+
142
+ # 清理已存在的文件
143
+ FileUtils.rm_rf(output_dir)
144
+ FileUtils.mkdir_p(output_dir)
145
+
146
+ # 构建路径配置
147
+ paths = {
148
+ output_apks: File.join(output_dir, "app.apks"),
149
+ bundle: File.join(main_module, "build/outputs/bundle/#{build_type}/#{main_module_name}-#{build_type}.aab"),
150
+ universal_apk: File.join(output_dir, "universal.apk")
151
+ }
152
+
153
+ # 检查 bundle 文件是否存在
154
+ unless File.exist?(paths[:bundle])
155
+ raise RuntimeError, "找不到 AAB 文件: #{paths[:bundle]}"
156
+ end
157
+
158
+ # --- Google Play 合规检测 (包含包体积、Target SDK、ELF对齐检测) ---
159
+ begin
160
+ compliance_result = Pindo::GPComplianceHelper.check_aab_compliance(paths[:bundle])
161
+
162
+ # 合规检测结果已在 gp_compliance_helper.rb 中输出,这里只做简单的状态提示
163
+ if compliance_result.compliant?
164
+ puts "\e[32m\e[1m✓ AAB 包符合 Google Play 最新合规要求,可以正常提交\e[0m"
165
+ else
166
+ puts "\e[31m\e[1m✗ AAB 包不符合 Google Play 提交标准,需要修复合规问题才能提交!!!\e[0m"
167
+ end
168
+
169
+ # 添加合规检测说明
170
+ puts "\e[36m\e[1m📋 合规检测说明:\e[0m"
171
+ puts "\e[33m • 仅供测试用的包可以忽略合规检测问题\e[0m"
172
+ puts "\e[33m • 提交至 Google Play 的正式用包必须处理所有合规检测问题\e[0m"
173
+ puts "\e[33m • 建议在正式发布前解决所有合规问题以确保顺利上架\e[0m"
174
+
175
+ rescue => e
176
+ puts "\e[31mGoogle Play 合规检测失败: #{e.message}\e[0m"
177
+ puts "\e[33m建议手动检查包体积、Target SDK 版本和共享库对齐情况\e[0m"
178
+ end
179
+ # --- END ---
180
+
181
+ # puts "解析 keystore 配置"
182
+ ks = keystore_config[:store_file]
183
+ # puts "读取 keystore path = #{ks}"
184
+ ks_pass = keystore_config[:store_password]
185
+ # puts "读取 keystore pass = #{ks_pass}"
186
+ key_alias = keystore_config[:key_alias]
187
+ # puts "读取 key alias = #{key_alias}"
188
+ key_pass = keystore_config[:key_password]
189
+ # puts "读取 key pass = #{key_pass}"
190
+
191
+ # 构建 APK
192
+ # 确保使用正确的 Java 版本 (Java 11+)
193
+ java_cmd = Pindo::JavaEnvHelper.find_java_command
194
+
195
+ # 验证 Java 版本
196
+ unless Pindo::JavaEnvHelper.verify_java_version(java_cmd)
197
+ raise RuntimeError, "Java 版本不符合要求,bundletool 需要 Java 11+,当前使用的 Java: #{java_cmd}"
198
+ end
199
+
200
+ # 使用数组形式构建命令,正确处理包含空格的路径
201
+ bundletool_cmd = [
202
+ java_cmd, "-jar", bundle_tool, "build-apks",
203
+ "--bundle=#{paths[:bundle]}",
204
+ "--output=#{paths[:output_apks]}",
205
+ "--ks=#{ks}",
206
+ "--ks-pass=pass:#{ks_pass}",
207
+ "--ks-key-alias=#{key_alias}",
208
+ "--key-pass=pass:#{key_pass}",
209
+ "--mode=universal"
210
+ ]
211
+
212
+ # puts bundletool_cmd
213
+ # 验证关键文件是否存在
214
+ unless File.exist?(paths[:bundle])
215
+ raise RuntimeError, "AAB 文件不存在: #{paths[:bundle]}"
216
+ end
217
+
218
+ unless File.exist?(ks)
219
+ raise RuntimeError, "Keystore 文件不存在"
220
+ end
221
+
222
+ unless system(*bundletool_cmd)
223
+ # 检查是否是 Java 版本问题
224
+ if java_cmd == 'java'
225
+ raise RuntimeError, "APKS 构建失败。可能是 Java 版本不兼容,bundletool 需要 Java 11+,请检查 JAVA_HOME 环境变量或安装正确的 Java 版本"
226
+ else
227
+ raise RuntimeError, "APKS 构建失败。使用的 Java 命令: #{java_cmd}"
228
+ end
229
+ end
230
+
231
+ # 解压 APKs 文件
232
+ unless system("unzip", "-o", paths[:output_apks], "-d", output_dir)
233
+ raise RuntimeError, "APKS 解压失败"
234
+ end
235
+
236
+ # 返回生成的 APK 路径
237
+ unless File.exist?(paths[:universal_apk])
238
+ raise RuntimeError, "未找到生成的 APK 文件"
239
+ end
240
+
241
+ paths[:universal_apk]
242
+ end
243
+
244
+ def build_aab(project_path, debug)
245
+ Dir.chdir(project_path) do
246
+ system("./gradlew bundle#{debug ? 'Debug' : 'Release'}")
247
+ end
248
+ end
249
+
250
+ def build_so_library(project_path)
251
+ # 编译so库
252
+ Dir.chdir(project_path) do
253
+ system("./gradlew unityLibrary:BuildIl2CppTask")
254
+ end
255
+ end
256
+
257
+ def copy_so_files(source_path, target_path)
258
+ puts "拷贝 Unity 编译产物到主工程..."
259
+
260
+ # 需要拷贝的目录列表(参考 copy_unity_export.sh)
261
+ copy_dirs = [
262
+ "unityLibrary/libs", # Java/Kotlin 库(包含 unity-classes.jar)
263
+ "unityLibrary/src/main/assets", # Unity 资源文件
264
+ "unityLibrary/src/main/jniLibs" # SO 库文件(IL2CPP 编译产物)
265
+ ]
266
+
267
+ copy_dirs.each do |dir|
268
+ src_dir = File.join(source_path, dir)
269
+ dst_dir = File.join(target_path, dir)
270
+
271
+ if File.directory?(src_dir)
272
+ puts " ✓ 拷贝: #{dir}"
273
+
274
+ # 确保目标目录存在
275
+ FileUtils.mkdir_p(dst_dir)
276
+
277
+ # 拷贝目录内容(不包括目录本身)
278
+ Dir.glob(File.join(src_dir, "*")).each do |src_item|
279
+ dst_item = File.join(dst_dir, File.basename(src_item))
280
+
281
+ if File.directory?(src_item)
282
+ # 递归拷贝子目录
283
+ FileUtils.cp_r(src_item, dst_item, remove_destination: true)
284
+ else
285
+ # 拷贝文件
286
+ FileUtils.cp(src_item, dst_item)
287
+ end
288
+ end
289
+ else
290
+ puts " ⚠ 跳过: #{dir} (目录不存在)"
291
+ end
292
+ end
293
+
294
+ puts "Unity 编译产物拷贝完成\n"
295
+ end
296
+
297
+
298
+ end
299
+ end
300
+
@@ -0,0 +1,279 @@
1
+ module Pindo
2
+ class AndroidProjectHelper
3
+ class << self
4
+
5
+
6
+ def unity_android_project?(project_path)
7
+ # 检查 unityLibrary 模块是否存在
8
+ unity_library_path = File.join(project_path, "unityLibrary")
9
+ return false unless File.directory?(unity_library_path)
10
+
11
+ # 检查 unityLibrary 的 build.gradle 或 build.gradle.kts 是否存在
12
+ unity_gradle_path = File.join(unity_library_path, "build.gradle")
13
+ unity_gradle_kts_path = File.join(unity_library_path, "build.gradle.kts")
14
+ if File.exist?(unity_gradle_kts_path)
15
+ unity_gradle_path = unity_gradle_kts_path
16
+ elsif !File.exist?(unity_gradle_path)
17
+ return false
18
+ end
19
+
20
+ # 检查 build.gradle 中是否包含 Unity 特有的配置
21
+ content = File.read(unity_gradle_path)
22
+ content.include?("com.android.library") && content.include?("BuildIl2Cpp")
23
+ end
24
+
25
+ def unity_as_lib_android_project?(project_path)
26
+ # Unity 作为 lib 的判断条件简化版:
27
+ # 1. 不是独立的 Unity 导出工程
28
+ return false if unity_android_project?(project_path)
29
+
30
+ # 2. 检查是否存在 Unity 目录
31
+ unity_dir = File.join(project_path, "Unity")
32
+ return false unless File.directory?(unity_dir)
33
+
34
+ # 3. 检查 Unity 目录下是否有 unityLibrary 目录
35
+ unity_library_dir = File.join(unity_dir, "unityLibrary")
36
+ return false unless File.directory?(unity_library_dir)
37
+
38
+ # 4. 检查 Unity/unityLibrary 目录下是否有 build.gradle 或 build.gradle.kts
39
+ gradle_path = File.join(unity_library_dir, "build.gradle")
40
+ gradle_kts_path = File.join(unity_library_dir, "build.gradle.kts")
41
+
42
+ File.exist?(gradle_path) || File.exist?(gradle_kts_path)
43
+ end
44
+
45
+
46
+ def get_build_tools
47
+ # 获取 gem 资源文件路径
48
+ pindo_dir ||= File.expand_path(ENV['PINDO_DIR'] || '~/.pindo')
49
+ pindo_common_configdir ||= File.join(pindo_dir, "pindo_common_config")
50
+ tools_dir = File.join(pindo_common_configdir, 'android_tools')
51
+
52
+ # 检查工具目录是否存在
53
+ unless File.directory?(tools_dir)
54
+ Funlog.error("Android 构建工具目录不存在: #{tools_dir}")
55
+ Funlog.error("请执行以下命令更新 Pindo 配置:")
56
+ Funlog.error(" pindo setup")
57
+ Funlog.error("或手动克隆配置仓库到 ~/.pindo/pindo_common_config")
58
+ return nil
59
+ end
60
+
61
+ # 定义必要的工具
62
+ required_tools = {
63
+ bundle_tool: 'bundletool.jar',
64
+ gradlew: 'gradlew',
65
+ gradle_wrapper: 'gradle-wrapper.jar'
66
+ }
67
+
68
+ tools = {}
69
+ missing_tools = []
70
+
71
+ # 检查每个工具是否存在
72
+ required_tools.each do |key, filename|
73
+ path = File.join(tools_dir, filename)
74
+ if File.exist?(path)
75
+ tools[key] = path
76
+ else
77
+ missing_tools << filename
78
+ end
79
+ end
80
+
81
+ # 如果有缺失的工具,提供友好的错误信息
82
+ unless missing_tools.empty?
83
+ Funlog.error("缺少以下 Android 构建工具:")
84
+ missing_tools.each do |tool|
85
+ Funlog.error(" - #{tool}")
86
+ end
87
+ Funlog.error("")
88
+ Funlog.error("解决方案:")
89
+ Funlog.error(" 1. 执行 'pindo setup' 更新配置")
90
+ Funlog.error(" 2. 或手动下载缺失的工具到: #{tools_dir}")
91
+ Funlog.error("")
92
+ Funlog.error("如果问题持续存在,请检查:")
93
+ Funlog.error(" - 网络连接是否正常")
94
+ Funlog.error(" - Git 仓库是否可访问")
95
+ Funlog.error(" - 目录权限是否正确")
96
+ return nil
97
+ end
98
+
99
+ tools
100
+ end
101
+
102
+ def modify_il2cpp_config(project_path)
103
+ # 设置Il2CppOutputProject可执行权限
104
+ system("chmod", "-R", "777",
105
+ File.join(project_path, "unityLibrary/src/main/Il2CppOutputProject"))
106
+
107
+ il2cpp_config_path = File.join(project_path, "unityLibrary/src/main/Il2CppOutputProject/IL2CPP/libil2cpp/il2cpp-config.h")
108
+ content = File.read(il2cpp_config_path)
109
+ content.gsub!("il2cpp::vm::Exception::Raise", "//il2cpp::vm::Exception::Raise")
110
+ File.write(il2cpp_config_path, content)
111
+ end
112
+
113
+ def remove_desktop_google_service(project_path)
114
+ # 删除google-services-desktop.json
115
+ desktop_google_service_path = File.join(project_path,
116
+ "unityLibrary/src/main/assets/google-services-desktop.json")
117
+ File.delete(desktop_google_service_path) if File.exist?(desktop_google_service_path)
118
+ end
119
+
120
+ def add_unity_namespace(project_path)
121
+ # 在 unityLibrary/build.gradle 中添加 namespace(AGP 7.x+ 需要)
122
+ unity_build_gradle = File.join(project_path, "unityLibrary/build.gradle")
123
+ return unless File.exist?(unity_build_gradle)
124
+
125
+ content = File.read(unity_build_gradle)
126
+
127
+ # 检查是否已经有 namespace
128
+ if content =~ /namespace\s+['"][\w.]+['"]/
129
+ puts " ✓ unityLibrary 已配置 namespace"
130
+ return
131
+ end
132
+
133
+ # 在 android { 块的开始位置添加 namespace
134
+ if content =~ /(android\s*\{)/
135
+ # Unity 默认使用 com.unity3d.player 作为 namespace
136
+ namespace_line = "\n namespace 'com.unity3d.player'\n"
137
+ content.sub!(/(android\s*\{)/, "\\1#{namespace_line}")
138
+
139
+ File.write(unity_build_gradle, content)
140
+ puts " ✓ 已添加 namespace 到 unityLibrary/build.gradle"
141
+ else
142
+ puts " ⚠ 无法在 unityLibrary/build.gradle 中找到 android 块"
143
+ end
144
+ end
145
+
146
+ def check_main_local_properties(project_path)
147
+ # 检查并配置主工程的 local.properties 文件
148
+ main_local_properties = File.join(project_path, "local.properties")
149
+
150
+ # 获取期望的 SDK 路径(从 Unity 模块、环境变量或默认路径)
151
+ expected_sdk_dir = get_expected_sdk_dir(project_path)
152
+
153
+ unless expected_sdk_dir
154
+ puts " ⚠ 无法找到有效的 Android SDK 路径"
155
+ return
156
+ end
157
+
158
+ if File.exist?(main_local_properties)
159
+ # 文件存在,检查 sdk.dir 是否正确
160
+ puts " 检查主工程 local.properties..."
161
+ content = File.read(main_local_properties)
162
+
163
+ if content =~ /sdk\.dir\s*=\s*(.+)/
164
+ current_sdk_dir = $1.strip
165
+
166
+ if current_sdk_dir == expected_sdk_dir
167
+ puts " ✓ SDK 路径正确: #{current_sdk_dir}"
168
+ elsif File.directory?(current_sdk_dir)
169
+ puts " ✓ SDK 路径有效: #{current_sdk_dir}"
170
+ else
171
+ # SDK 路径无效,更新为期望的路径
172
+ puts " ⚠ SDK 路径无效: #{current_sdk_dir}"
173
+ puts " 更新为: #{expected_sdk_dir}"
174
+ content.gsub!(/sdk\.dir\s*=\s*.+/, "sdk.dir=#{expected_sdk_dir}")
175
+ File.write(main_local_properties, content)
176
+ puts " ✓ 已更新 SDK 路径"
177
+ end
178
+ else
179
+ # 文件存在但没有 sdk.dir,添加它
180
+ puts " ⚠ local.properties 中未找到 sdk.dir"
181
+ File.write(main_local_properties, "#{content}\nsdk.dir=#{expected_sdk_dir}\n")
182
+ puts " ✓ 已添加 SDK 路径: #{expected_sdk_dir}"
183
+ end
184
+ else
185
+ # 文件不存在,创建它
186
+ puts " 创建主工程 local.properties..."
187
+ File.write(main_local_properties, "sdk.dir=#{expected_sdk_dir}\n")
188
+ puts " ✓ 已创建 local.properties,SDK 路径: #{expected_sdk_dir}"
189
+ end
190
+ end
191
+
192
+ def get_expected_sdk_dir(project_path)
193
+ # 优先使用 Android Studio 的 SDK 路径(主工程使用)
194
+ android_studio_sdk = File.expand_path("~/Library/Android/sdk")
195
+ return android_studio_sdk if File.directory?(android_studio_sdk)
196
+
197
+ # 尝试从环境变量获取
198
+ sdk_dir = ENV['ANDROID_HOME'] || ENV['ANDROID_SDK_ROOT']
199
+ return sdk_dir if sdk_dir && File.directory?(sdk_dir)
200
+
201
+ # 尝试从 Unity 模块的 local.properties 获取 SDK 路径
202
+ unity_local_properties = File.join(project_path, "Unity/local.properties")
203
+
204
+ if File.exist?(unity_local_properties)
205
+ unity_content = File.read(unity_local_properties)
206
+ if unity_content =~ /sdk\.dir\s*=\s*(.+)/
207
+ sdk_dir = $1.strip
208
+ return sdk_dir if File.directory?(sdk_dir)
209
+ end
210
+ end
211
+
212
+ # 最后尝试 Unity 内置的 SDK 路径
213
+ unity_sdk = "/Applications/Unity/Hub/Editor/2022.3.61f1/PlaybackEngines/AndroidPlayer/SDK"
214
+ return unity_sdk if File.directory?(unity_sdk)
215
+
216
+ nil
217
+ end
218
+
219
+ def get_main_module(project_path)
220
+ settings_gradle_path = File.join(project_path, "settings.gradle")
221
+ settings_gradle_kts_path = File.join(project_path, "settings.gradle.kts")
222
+
223
+ # 优先使用 settings.gradle.kts,如果不存在则使用 settings.gradle
224
+ if File.exist?(settings_gradle_kts_path)
225
+ settings_gradle_path = settings_gradle_kts_path
226
+ elsif !File.exist?(settings_gradle_path)
227
+ return nil
228
+ end
229
+
230
+ content = File.read(settings_gradle_path)
231
+ # 兼容 include ':app' 和 include(":app")
232
+ modules = content.scan(/include\s*\(?\s*['\"]?([:\w\-]+)['\"]?\s*\)?/).flatten
233
+
234
+ main_module = modules.find do |m|
235
+ module_name = m.split(':').last
236
+ gradle_path = File.join(project_path, module_name, "build.gradle")
237
+ gradle_kts_path = File.join(project_path, module_name, "build.gradle.kts")
238
+
239
+ # 优先使用 build.gradle.kts,如果不存在则使用 build.gradle
240
+ if File.exist?(gradle_kts_path)
241
+ gradle_path = gradle_kts_path
242
+ end
243
+
244
+ if File.exist?(gradle_path)
245
+ gradle_content = File.read(gradle_path)
246
+ gradle_content.include?("apply plugin: 'com.android.application") ||
247
+ gradle_content.include?("id 'com.android.application") ||
248
+ (gradle_content.include?("plugins {") && gradle_content.include?("com.android.application"))
249
+ end
250
+ end
251
+ return nil unless main_module
252
+
253
+ module_name = main_module.split(':').last
254
+ File.join(project_path, module_name)
255
+ end
256
+
257
+
258
+
259
+
260
+ def find_android_subproject(project_path)
261
+ android_dir = File.join(project_path, "Unity")
262
+ return nil unless File.directory?(android_dir)
263
+
264
+ main_module = get_main_module(android_dir)
265
+ return nil unless main_module
266
+
267
+ src_main = File.join(main_module, "src/main")
268
+ return nil unless File.directory?(src_main)
269
+
270
+ manifest = File.join(src_main, "AndroidManifest.xml")
271
+ return nil unless File.exist?(manifest)
272
+
273
+ android_dir
274
+ end
275
+
276
+ private
277
+ end
278
+ end
279
+ end