Module: Pindo::BaseAndroidHelper
- Included in:
- AndroidBuildHelper, ApkHelper, BuildHelper, GradleHelper
- Defined in:
- lib/pindo/module/android/base_helper.rb
Instance Method Summary collapse
- #find_android_subproject(project_path) ⇒ Object
- #get_build_tools ⇒ Object
-
#get_ext_values(project_path) ⇒ Object
新增方法:从 ext 配置文件中获取变量值.
- #get_keystore_config(project_path, debug = false) ⇒ Object
-
#get_main_module(project_path) ⇒ Object
xml_content = File.read(strings_xml_path) doc = Nokogiri::XML(xml_content) doc.at_xpath(“//string”)&.text end.
- #modify_il2cpp_config(project_path) ⇒ Object
- #remove_desktop_google_service(project_path) ⇒ Object
- #unity_android_project?(project_path) ⇒ Boolean
Instance Method Details
#find_android_subproject(project_path) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/pindo/module/android/base_helper.rb', line 277 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 |
#get_build_tools ⇒ Object
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.(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 配置文件中获取变量值
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/pindo/module/android/base_helper.rb', line 234 def get_ext_values(project_path) ext_values = {} # 查找可能的 ext 配置文件 ext_files = [ File.join(project_path, "buildScripts/cfg/ext_signing.gradle"), 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 # 解析 KEY=VALUE 格式 content.scan(/^(\w+)\s*=\s*["']?([^"'\n]+)["']?/m).each do |key, value| ext_values[key] = value end end ext_values end |
#get_keystore_config(project_path, debug = false) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 157 158 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/pindo/module/android/base_helper.rb', line 89 def get_keystore_config(project_path, debug = false) main_module = get_main_module(project_path) return nil unless main_module gradle_path = File.join(main_module, "build.gradle") return nil unless File.exist?(gradle_path) content = File.read(gradle_path) # 直接读取整个文件内容,不使用正则表达式截取 android 块 puts "读取 build.gradle 文件" # 查找 signingConfigs 块的开始和结束位置 signing_configs_start = content.index(/signingConfigs\s*\{/) return nil unless signing_configs_start # 从开始位置查找匹配的闭合大括号 open_braces = 0 signing_configs_end = nil content[signing_configs_start..-1].each_char.with_index do |char, i| if char == '{' open_braces += 1 elsif char == '}' open_braces -= 1 if open_braces == 0 signing_configs_end = signing_configs_start + i break end end end return nil unless signing_configs_end # 提取完整的 signingConfigs 块 signing_configs_block = content[signing_configs_start..signing_configs_end] # 查找配置名称,如 Android_Sign config_name_match = signing_configs_block.match(/signingConfigs\s*\{\s*(\w+)\s*\{/) config_name = config_name_match ? config_name_match[1] : nil return nil unless config_name # 检查 buildTypes 中使用的签名配置 config_type = debug ? 'debug' : 'release' build_type_match = content.match(/buildTypes\s*\{.*?#{config_type}\s*\{(.*?)}/m) if build_type_match && build_type_match[1] =~ /signingConfig\s+signingConfigs\.(\w+)/ config_name = $1 end # 提取特定配置块的内容 config_start = signing_configs_block.index(/#{config_name}\s*\{/) return nil unless config_start # 从配置开始位置查找匹配的闭合大括号 open_braces = 0 config_end = nil signing_configs_block[config_start..-1].each_char.with_index do |char, i| if char == '{' open_braces += 1 elsif char == '}' open_braces -= 1 if open_braces == 0 config_end = config_start + i break end end end return nil unless config_end # 提取完整的配置块 config_block = signing_configs_block[config_start..config_end] puts "解析 signing config:" puts "=" * 50 puts config_block puts "=" * 50 # 从配置块中提取各项参数 store_file_match = config_block.match(/storeFile\s+file\(["']?\${([^}]+)}["']?\)/m) store_password_match = config_block.match(/storePassword\s+["']?\${([^}]+)}["']?/m) key_alias_match = config_block.match(/keyAlias\s+["']?\${([^}]+)}["']?/m) key_password_match = config_block.match(/keyPassword\s+["']?\${([^}]+)}["']?/m) # 如果使用了外部变量,尝试从 ext_signing.gradle 或其他配置文件中获取实际值 if store_file_match || store_password_match || key_alias_match || key_password_match ext_values = get_ext_values(project_path) puts "尝试从 ext values 中获取" # 获取 store_file 并处理 $rootDir 路径 store_file = store_file_match ? ext_values[store_file_match[1]] : nil if store_file && store_file.include?('$rootDir') # 替换 $rootDir 为项目根目录的绝对路径 # 查找项目根目录(包含settings.gradle的目录) root_dir = project_path while root_dir && !File.exist?(File.join(root_dir, "settings.gradle")) parent_dir = File.dirname(root_dir) # 防止无限循环 break if parent_dir == root_dir root_dir = parent_dir end store_file = store_file.gsub('$rootDir', root_dir) puts "处理后的 store_file 路径: #{store_file}" end keystore_config = { store_file: store_file, store_password: store_password_match ? ext_values[store_password_match[1]] : nil, key_alias: key_alias_match ? ext_values[key_alias_match[1]] : nil, key_password: key_password_match ? ext_values[key_password_match[1]] : nil } # 确保所有值都不为 nil if keystore_config.values.any?(&:nil?) puts "警告: 部分 keystore 配置为 nil: #{keystore_config}" end return keystore_config else # 直接从配置块中提取硬编码的值 store_file = config_block.match(/storeFile\s+file\(['"]([^'"]+)['"]\)/m)&.[](1) store_password = config_block.match(/storePassword\s+['"]([^'"]+)['"]/m)&.[](1) key_alias = config_block.match(/keyAlias\s+['"]([^'"]+)['"]/m)&.[](1) key_password = config_block.match(/keyPassword\s+['"]([^'"]+)['"]/m)&.[](1) keystore_config = { store_file: store_file, store_password: store_password, key_alias: key_alias, key_password: key_password } # 确保所有值都不为 nil if keystore_config.values.any?(&:nil?) puts "警告: 部分 keystore 配置为 nil: #{keystore_config}" end return keystore_config end 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 |
# 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") return nil unless File.exist?(settings_gradle_path) content = File.read(settings_gradle_path) modules = content.scan(/include\s+['"]([^'"]+)['"]/).flatten main_module = modules.find do |m| module_name = m.split(':').last gradle_path = File.join(project_path, module_name, "build.gradle") if File.exist?(gradle_path) File.read(gradle_path).include?("apply plugin: 'com.android.application'") || File.read(gradle_path).include?("id '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 |
#unity_android_project?(project_path) ⇒ Boolean
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/pindo/module/android/base_helper.rb', line 263 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 是否存在 unity_gradle_path = File.join(unity_library_path, "build.gradle") return false unless File.exist?(unity_gradle_path) # 检查 build.gradle 中是否包含 Unity 特有的配置 content = File.read(unity_gradle_path) content.include?("com.android.library") && content.include?("BuildIl2Cpp") end |