Class: Pindo::AndroidBuildConfigHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/android/android_build_config_helper.rb

Class Method Summary collapse

Class Method Details

.add_application_id_based_scheme(project_dir: nil) ⇒ Boolean

添加基于 Application ID 的 Scheme 读取当前项目的 Application ID 并添加对应的 Scheme

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

Returns:

  • (Boolean)

    是否成功添加



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 540

def self.add_application_id_based_scheme(project_dir: nil)
    # 参数验证
    if project_dir.nil? || !File.directory?(project_dir)
        Funlog.instance.fancyinfo_error("项目路径无效: #{project_dir}")
        return false
    end

    begin
        # 获取主模块
        main_module = Pindo::AndroidProjectHelper.get_main_module(project_dir)

        # 支持多种项目结构
        module_paths = []
        module_paths << File.join(project_dir, main_module) if main_module
        module_paths << File.join(project_dir, "app")
        module_paths << File.join(project_dir, "unityLibrary/launcher")
        module_paths << File.join(project_dir, "launcher")

        gradle_file = nil
        module_paths.each do |module_path|
            next unless File.exist?(module_path)

            if File.exist?(File.join(module_path, "build.gradle"))
                gradle_file = File.join(module_path, "build.gradle")
                break
            elsif File.exist?(File.join(module_path, "build.gradle.kts"))
                gradle_file = File.join(module_path, "build.gradle.kts")
                break
            end
        end

        if gradle_file.nil?
            Funlog.instance.fancyinfo_error("未找到build.gradle文件,无法读取 Application ID")
            return false
        end

        # 读取gradle文件,提取当前的 Application ID
        content = File.read(gradle_file)
        application_id = nil

        # 匹配各种 applicationId 格式
        if content =~ /applicationId\s+["']([^"']+)["']/
            application_id = $1
        elsif content =~ /applicationId\s*=\s*["']([^"']+)["']/
            application_id = $1
        end

        if application_id.nil?
            Funlog.instance.fancyinfo_error("无法从 gradle 文件中读取 Application ID")
            return false
        end

        # 将 Application ID 转换为合法的 scheme 名称(只保留字母和数字)
        bundle_scheme = application_id.gsub(/[^a-zA-Z0-9]/, '').downcase

        # 添加 Scheme
        success_scheme = add_test_scheme(
            project_dir: project_dir,
            scheme_name: bundle_scheme
        )

        if success_scheme
            puts "  ✓ 已添加Scheme: #{bundle_scheme} (基于 Application ID: #{application_id})"
            return true
        else
            Funlog.instance.fancyinfo_error("添加 Scheme 失败: #{bundle_scheme}")
            return false
        end

    rescue StandardError => e
        Funlog.instance.fancyinfo_error("添加基于 Application ID 的 Scheme 失败: #{e.message}")
        return false
    end
end

.add_test_scheme(project_dir: nil, scheme_name: nil) ⇒ Boolean

添加测试scheme到Android工程

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

  • scheme_name (String) (defaults to: nil)

    要添加的scheme名称

Returns:

  • (Boolean)

    是否成功添加



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 619

def self.add_test_scheme(project_dir: nil, scheme_name: nil)
    # 参数验证
    if scheme_name.nil?
        Funlog.instance.fancyinfo_error("需要提供scheme名称")
        return false
    end

    if project_dir.nil? || !File.directory?(project_dir)
        Funlog.instance.fancyinfo_error("项目路径无效: #{project_dir}")
        return false
    end

    scheme_name = scheme_name.to_s.gsub(/[^a-zA-Z0-9]/, '').downcase

    # 查找所有可能的模块(添加 launcher 支持 Unity 项目)
    main_module = Pindo::AndroidProjectHelper.get_main_module(project_dir)
    search_modules = ["launcher", "app", "unityLibrary"]
    search_modules << main_module if main_module
    search_modules = search_modules.compact.uniq

    # 查找所有可能的AndroidManifest.xml文件
    manifest_candidates = find_manifests(project_dir, search_modules)

    if manifest_candidates.empty?
        Funlog.instance.fancyinfo_error("未找到任何AndroidManifest.xml文件")
        return false
    end

    # 寻找最佳的AndroidManifest.xml和Activity
    manifest_path, main_activity, android_prefix = find_best_activity(manifest_candidates)

    if manifest_path.nil? || main_activity.nil?
        Funlog.instance.fancyinfo_error("无法找到合适的Activity")
        return false
    end

    # 检查scheme是否已存在
    scheme_exists, existing_scheme = check_scheme_exists(main_activity, android_prefix, scheme_name)

    # 如果scheme已存在,检查是否需要更新格式
    if scheme_exists
        activity_name = main_activity["#{android_prefix}:name"] || "主Activity"
        if existing_scheme != scheme_name
            # 格式不同,需要更新
            if update_existing_scheme(manifest_path, main_activity, android_prefix, existing_scheme, scheme_name)
                Funlog.instance.fancyinfo_update("已将scheme从'#{existing_scheme}'更新为'#{scheme_name}'")
            else
                Funlog.instance.fancyinfo_update("scheme已存在: #{existing_scheme}")
            end
        end
        return true
    end

    # 尝试使用DOM添加
    success = add_scheme_with_dom(manifest_path, main_activity, android_prefix, scheme_name)

    unless success
        # DOM方法失败,尝试文本替换
        success = add_scheme_with_text_replace(manifest_path, scheme_name)
    end

    unless success
        Funlog.instance.fancyinfo_error("无法添加scheme: #{scheme_name}")
    end

    return success
end

.apply_config_from_repo(config_repo_dir: nil, project_dir: nil) ⇒ Boolean

从配置仓库应用配置(拷贝 config.json)

Parameters:

  • config_repo_dir (String) (defaults to: nil)

    配置仓库的路径

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

Returns:

  • (Boolean)

    是否成功应用



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 511

def self.apply_config_from_repo(config_repo_dir: nil, project_dir: nil)
    # 参数验证
    if config_repo_dir.nil? || !File.directory?(config_repo_dir)
        Funlog.instance.fancyinfo_error("配置仓库路径无效: #{config_repo_dir}")
        return false
    end

    if project_dir.nil? || !File.directory?(project_dir)
        Funlog.instance.fancyinfo_error("项目路径无效: #{project_dir}")
        return false
    end

    # 拷贝 config.json 到项目目录
    config_source = File.join(config_repo_dir, 'config.json')
    if File.exist?(config_source)
        config_target = File.join(project_dir, 'config.json')
        FileUtils.cp(config_source, config_target)
        puts "  ✓ config.json 已拷贝到项目目录"
        return true
    else
        puts "  ⚠ 配置仓库中未找到 config.json"
        return false
    end
end

.copy_google_services_from_config_repo(config_repo_dir: nil, project_dir: nil) ⇒ Boolean

从配置仓库拷贝 google-services.json 到项目的 launcher 目录

Parameters:

  • config_repo_dir (String) (defaults to: nil)

    配置仓库的路径

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

Returns:

  • (Boolean)

    是否成功拷贝



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 446

def self.copy_google_services_from_config_repo(config_repo_dir: nil, project_dir: nil)
    # 参数验证
    if config_repo_dir.nil? || !File.directory?(config_repo_dir)
        Funlog.instance.fancyinfo_error("配置仓库路径无效: #{config_repo_dir}")
        return false
    end

    if project_dir.nil? || !File.directory?(project_dir)
        Funlog.instance.fancyinfo_error("项目路径无效: #{project_dir}")
        return false
    end

    # 查找配置仓库中的 google-services.json
    source_file = File.join(config_repo_dir, 'google-services.json')
    unless File.exist?(source_file)
        Funlog.instance.fancyinfo_warning("配置仓库中未找到 google-services.json")
        return false
    end

    puts "正在拷贝 google-services.json..."

    # 优先拷贝到 launcher 目录
    launcher_dirs = [
        File.join(project_dir, 'unityLibrary', 'launcher'),  # Unity 项目的 launcher
        File.join(project_dir, 'launcher'),                  # 独立的 launcher
        File.join(project_dir, 'app')                        # 原生 Android 项目
    ]

    copied = false
    launcher_dirs.each do |launcher_dir|
        if File.exist?(launcher_dir) && File.directory?(launcher_dir)
            target_file = File.join(launcher_dir, 'google-services.json')
            FileUtils.cp(source_file, target_file)
            puts "  ✓ google-services.json 已拷贝到: #{launcher_dir}"
            copied = true

            # Unity 项目也拷贝到 unityLibrary 根目录
            if launcher_dir.include?('unityLibrary/launcher')
                unity_root = File.join(project_dir, 'unityLibrary')
                if File.exist?(unity_root) && File.directory?(unity_root)
                    unity_target = File.join(unity_root, 'google-services.json')
                    FileUtils.cp(source_file, unity_target)
                    puts "  ✓ google-services.json 也已拷贝到: unityLibrary/"
                end
            end

            break # 找到第一个有效目录后停止
        end
    end

    unless copied
        # 如果没找到合适的目录,至少拷贝到项目根目录
        target_file = File.join(project_dir, 'google-services.json')
        FileUtils.cp(source_file, target_file)
        puts "  ✓ google-services.json 已拷贝到项目根目录"
        puts "  ⚠ 请确认文件位置是否正确"
    end

    return true
end

.update_android_project_version(project_dir: nil, version_name: nil, version_code: nil) ⇒ Boolean

更新Android工程版本号

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

  • version_name (String) (defaults to: nil)

    版本名

  • version_code (Integer) (defaults to: nil)

    版本号

Returns:

  • (Boolean)

    是否成功更新

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
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
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
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 60

def self.update_android_project_version(project_dir: nil, version_name: nil, version_code: nil)
    raise ArgumentError, "项目目录不能为空" if project_dir.nil?
    raise ArgumentError, "版本名不能为空" if version_name.nil?
    raise ArgumentError, "版本号不能为空" if version_code.nil?

    # 验证version_code的有效性
    unless valid_build_number?(version_code)
        Funlog.instance.fancyinfo_error("Android versionCode必须在1到#{2**31-1}之间,当前值:#{version_code}")
        return false
    end

    Funlog.instance.fancyinfo_start("正在更新Android工程版本信息...")

    begin
        # 获取主模块路径
        main_module = Pindo::AndroidProjectHelper.get_main_module(project_dir)

        if main_module.nil?
            # 如果无法获取主模块,尝试常见的模块名(优先 launcher,支持 Unity 项目)
            ["launcher", "app", "application", "main"].each do |module_name|
                module_path = File.join(project_dir, module_name)
                if File.exist?(File.join(module_path, "build.gradle")) ||
                   File.exist?(File.join(module_path, "build.gradle.kts"))
                    main_module = module_path
                    break
                end
            end
        end

        if main_module.nil?
            Funlog.instance.fancyinfo_error("未找到Android主模块")
            return false
        end

        # 查找build.gradle或build.gradle.kts文件
        gradle_file = nil
        if File.exist?(File.join(main_module, "build.gradle"))
            gradle_file = File.join(main_module, "build.gradle")
        elsif File.exist?(File.join(main_module, "build.gradle.kts"))
            gradle_file = File.join(main_module, "build.gradle.kts")
        end

        if gradle_file.nil?
            Funlog.instance.fancyinfo_error("未找到build.gradle文件")
            return false
        end

        # 读取并更新gradle文件
        content = File.read(gradle_file)

        # 第一步:清理所有现有的版本信息(无论在什么位置)
        # 直接赋值格式
        content.gsub!(/^\s*versionCode\s+\d+\s*$/, '')
        content.gsub!(/^\s*versionCode\s*=\s*\d+\s*$/, '')
        content.gsub!(/^\s*versionName\s+["'][^"']*["']\s*$/, '')
        content.gsub!(/^\s*versionName\s*=\s*["'][^"']*["']\s*$/, '')

        # 外部变量引用格式
        content.gsub!(/^\s*versionCode\s+rootProject\.ext\.\w+\s*$/, '')
        content.gsub!(/^\s*versionCode\s+project\.ext\.\w+\s*$/, '')
        content.gsub!(/^\s*versionCode\s+ext\.\w+\s*$/, '')
        content.gsub!(/^\s*versionName\s+["']?\$\{rootProject\.ext\.\w+\}["']?\s*$/, '')
        content.gsub!(/^\s*versionName\s+["']?\$\{project\.ext\.\w+\}["']?\s*$/, '')
        content.gsub!(/^\s*versionName\s+["']?\$\{ext\.\w+\}["']?\s*$/, '')
        content.gsub!(/^\s*versionName\s+rootProject\.ext\.\w+\s*$/, '')
        content.gsub!(/^\s*versionName\s+project\.ext\.\w+\s*$/, '')
        content.gsub!(/^\s*versionName\s+ext\.\w+\s*$/, '')

        # 清理多余的空行
        content.gsub!(/\n\n+/, "\n\n")

        # 第二步:在 defaultConfig 块的正确位置添加版本信息
        if content =~ /defaultConfig\s*\{/
            # 找到 defaultConfig 的起始位置
            start_pos = $~.end(0)

            # 从起始位置开始,找到匹配的 }
            brace_count = 1
            end_pos = start_pos

            while end_pos < content.length && brace_count > 0
                if content[end_pos] == '{'
                    brace_count += 1
                elsif content[end_pos] == '}'
                    brace_count -= 1
                end
                end_pos += 1
            end

            if brace_count == 0
                # 成功找到匹配的 }
                # 在 defaultConfig 块的末尾(但在最后的 } 之前)添加版本信息
                insert_pos = end_pos - 1

                version_lines = [
                    "        versionCode #{version_code}",
                    "        versionName \"#{version_name}\""
                ]

                content.insert(insert_pos, "\n" + version_lines.join("\n") + "\n    ")
                File.write(gradle_file, content)

                Funlog.instance.fancyinfo_success("Android版本更新完成!")
                puts "  ✓ 版本号已更新: #{version_name}"
                puts "  ✓ Build号已更新: #{version_code}"
                return true
            end
        end

        Funlog.instance.fancyinfo_error("无法在gradle文件中找到或添加版本信息")
        return false

    rescue StandardError => e
        Funlog.instance.fancyinfo_error("更新Android版本失败: #{e.message}")
        return false
    end
end

.update_app_name_with_packagename(project_dir: nil, package_name: nil) ⇒ Boolean

使用package_name更新Android应用名称(strings.xml中的app_name)

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

  • package_name (String) (defaults to: nil)

    工作流的package_name(如:“Test Demo”)

Returns:

  • (Boolean)

    是否成功更新

Raises:

  • (ArgumentError)


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
232
233
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 192

def self.update_app_name_with_packagename(project_dir: nil, package_name: nil)
    raise ArgumentError, "项目目录不能为空" if project_dir.nil?
    raise ArgumentError, "Package Name不能为空" if package_name.nil?

    # 生成应用名称(去除特殊字符和空格)
    app_name = package_name.gsub(/[^a-zA-Z0-9\s]/, '').gsub(/\s+/, '')

    begin
        main_module = Pindo::AndroidProjectHelper.get_main_module(project_dir)

        if main_module.nil?
            # 尝试常见的模块名(优先 launcher,支持 Unity 项目)
            ["launcher", "app", "application", "main"].each do |module_name|
                module_path = File.join(project_dir, module_name)
                if File.directory?(module_path)
                    main_module = module_path
                    break
                end
            end
        end

        return false if main_module.nil?

        # 1. 先删除 build.gradle 中的 resValue("string", "app_name", ...)
        build_gradle_path = File.join(main_module, "build.gradle")
        build_gradle_kts_path = File.join(main_module, "build.gradle.kts")

        gradle_file = nil
        if File.exist?(build_gradle_kts_path)
            gradle_file = build_gradle_kts_path
        elsif File.exist?(build_gradle_path)
            gradle_file = build_gradle_path
        end

        if gradle_file
            gradle_content = File.read(gradle_file)
            original_content = gradle_content.dup

            # 删除 resValue("string", "app_name", ...) 这一行
            # 支持多种格式:
            # resValue("string", "app_name", "xxx")
            # resValue("string", "app_name", "${rootProject.ext.app_name}")
            # resValue "string", "app_name", "xxx"
            gradle_content.gsub!(/^\s*resValue\s*\(?\s*["']string["']\s*,\s*["']app_name["'].*?\)?\s*$\n?/, '')

            if gradle_content != original_content
                File.write(gradle_file, gradle_content)
                puts "  ✓ 已删除 build.gradle 中的 resValue(\"string\", \"app_name\", ...)"
            end
        end

        # 2. 更新 strings.xml
        # 查找strings.xml文件
        strings_xml_path = File.join(main_module, "src", "main", "res", "values", "strings.xml")

        unless File.exist?(strings_xml_path)
            Funlog.instance.fancyinfo_error("未找到strings.xml文件")
            return false
        end

        # 读取并更新strings.xml
        content = File.read(strings_xml_path)

        # 检查是否存在 app_name
        if content =~ /<string\s+name="app_name">.*?<\/string>/
            # 存在,直接更新
            content.gsub!(/<string\s+name="app_name">.*?<\/string>/, "<string name=\"app_name\">#{app_name}</string>")
            File.write(strings_xml_path, content)
            puts "  ✓ App Name 已更新为: #{app_name} (来自: #{package_name})"
            return true
        else
            # 不存在,自动添加
            if content =~ /<resources>(.*?)<\/resources>/m
                # 在 <resources> 标签内添加 app_name
                new_string_entry = "\n  <string name=\"app_name\">#{app_name}</string>"
                content.sub!(/<resources>/, "<resources>#{new_string_entry}")
                File.write(strings_xml_path, content)
                puts "  ✓ App Name 已添加: #{app_name} (来自: #{package_name})"
                return true
            else
                Funlog.instance.fancyinfo_error("strings.xml格式异常,无法添加app_name")
                return false
            end
        end

    rescue StandardError => e
        Funlog.instance.fancyinfo_error("更新App Name失败: #{e.message}")
        return false
    end
end

.update_application_id(project_dir: nil, application_id: nil) ⇒ Boolean

直接更新Application ID(不依赖package_name生成)

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

  • application_id (String) (defaults to: nil)

    要设置的Application ID(如 com.example.app)

Returns:

  • (Boolean)

    是否成功更新



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 369

def self.update_application_id(project_dir: nil, application_id: nil)
    # 参数验证
    if application_id.nil? || application_id.empty?
        Funlog.instance.fancyinfo_error("Application ID不能为空")
        return false
    end

    if project_dir.nil? || !File.directory?(project_dir)
        Funlog.instance.fancyinfo_error("项目路径无效: #{project_dir}")
        return false
    end

    begin
        puts "正在设置 Application ID: #{application_id}"

        # 获取主模块
        main_module = Pindo::AndroidProjectHelper.get_main_module(project_dir)

        # 支持多种项目结构
        module_paths = []
        module_paths << File.join(project_dir, main_module) if main_module
        module_paths << File.join(project_dir, "app")
        module_paths << File.join(project_dir, "unityLibrary/launcher")
        module_paths << File.join(project_dir, "launcher")

        gradle_file = nil
        module_paths.each do |module_path|
            next unless File.exist?(module_path)

            if File.exist?(File.join(module_path, "build.gradle"))
                gradle_file = File.join(module_path, "build.gradle")
                break
            elsif File.exist?(File.join(module_path, "build.gradle.kts"))
                gradle_file = File.join(module_path, "build.gradle.kts")
                break
            end
        end

        if gradle_file.nil?
            Funlog.instance.fancyinfo_error("未找到build.gradle文件")
            return false
        end

        # 读取并更新gradle文件
        content = File.read(gradle_file)
        updated = false

        # 更新applicationId(支持多种格式)
        # 格式1: applicationId "com.example.app"
        if content =~ /applicationId\s+["'][^"']*["']/
            content.gsub!(/applicationId\s+["'][^"']*["']/, "applicationId \"#{application_id}\"")
            updated = true
        # 格式2: applicationId = "com.example.app"
        elsif content =~ /applicationId\s*=\s*["'][^"']*["']/
            content.gsub!(/applicationId\s*=\s*["'][^"']*["']/, "applicationId = \"#{application_id}\"")
            updated = true
        end

        unless updated
            Funlog.instance.fancyinfo_error("gradle文件中未找到applicationId配置")
            return false
        end

        File.write(gradle_file, content)
        puts "  ✓ 更新 #{File.basename(gradle_file)} 中的 applicationId"
        return true

    rescue StandardError => e
        Funlog.instance.fancyinfo_error("更新Application ID失败: #{e.message}")
        return false
    end
end

.update_applicationid_with_workflowname(project_dir: nil, package_name: nil) ⇒ Boolean

使用工作流名称生成并更新Android Application ID(build.gradle中的applicationId)

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

  • package_name (String) (defaults to: nil)

    工作流的名称(如:“Test Demo”),将被转换为application ID格式

Returns:

  • (Boolean)

    是否成功更新

Raises:

  • (ArgumentError)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
356
357
358
359
360
361
362
363
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 287

def self.update_applicationid_with_workflowname(project_dir: nil, package_name: nil)
    raise ArgumentError, "项目目录不能为空" if project_dir.nil?
    raise ArgumentError, "Package Name不能为空" if package_name.nil?

    # 生成 Application ID
    app_id_suffix = package_name.gsub(/[^a-zA-Z0-9]/, '').downcase
    application_id = "com.heroneverdie101.#{app_id_suffix}"

    begin
        main_module = Pindo::AndroidProjectHelper.get_main_module(project_dir)

        if main_module.nil?
            # 尝试常见的模块名(优先 launcher,支持 Unity 项目)
            ["launcher", "app", "application", "main"].each do |module_name|
                module_path = File.join(project_dir, module_name)
                if File.exist?(File.join(module_path, "build.gradle")) ||
                   File.exist?(File.join(module_path, "build.gradle.kts"))
                    main_module = module_path
                    break
                end
            end
        end

        return false if main_module.nil?

        # 查找build.gradle或build.gradle.kts文件
        gradle_file = nil
        if File.exist?(File.join(main_module, "build.gradle"))
            gradle_file = File.join(main_module, "build.gradle")
        elsif File.exist?(File.join(main_module, "build.gradle.kts"))
            gradle_file = File.join(main_module, "build.gradle.kts")
        end

        return false if gradle_file.nil?

        # 读取并更新gradle文件
        content = File.read(gradle_file)

        # 更新applicationId(支持多种格式)
        updated = false

        # 格式1: applicationId "com.example.app"
        if content =~ /applicationId\s+["'][^"']*["']/
            content.gsub!(/applicationId\s+["'][^"']*["']/, "applicationId \"#{application_id}\"")
            updated = true
        # 格式2: applicationId = "com.example.app"
        elsif content =~ /applicationId\s*=\s*["'][^"']*["']/
            content.gsub!(/applicationId\s*=\s*["'][^"']*["']/, "applicationId = \"#{application_id}\"")
            updated = true
        # 格式3: applicationId rootProject.ext.application_id (外部变量引用)
        elsif content =~ /applicationId\s+rootProject\.ext\.\w+/
            content.gsub!(/applicationId\s+rootProject\.ext\.\w+/, "applicationId \"#{application_id}\"")
            updated = true
        # 格式4: applicationId project.ext.application_id
        elsif content =~ /applicationId\s+project\.ext\.\w+/
            content.gsub!(/applicationId\s+project\.ext\.\w+/, "applicationId \"#{application_id}\"")
            updated = true
        # 格式5: applicationId ext.application_id
        elsif content =~ /applicationId\s+ext\.\w+/
            content.gsub!(/applicationId\s+ext\.\w+/, "applicationId \"#{application_id}\"")
            updated = true
        end

        unless updated
            Funlog.instance.fancyinfo_error("gradle文件中未找到applicationId字段")
            return false
        end

        File.write(gradle_file, content)
        puts "  ✓ Application ID 已更新为: #{application_id} (来自: #{package_name})"
        return true

    rescue StandardError => e
        Funlog.instance.fancyinfo_error("更新Application ID失败: #{e.message}")
        return false
    end
end

.update_project_with_workflow(project_dir: nil, workflow_packname: nil) ⇒ Boolean

使用工作流配置一次性更新App Name、Application ID和URL Schemes 优化版本:提高性能,减少代码重复

Parameters:

  • project_dir (String) (defaults to: nil)

    Android项目目录路径

  • workflow_packname (String) (defaults to: nil)

    工作流的名称(如:“Test Demo”)

Returns:

  • (Boolean)

    是否成功更新

Raises:

  • (ArgumentError)


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
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 17

def self.update_project_with_workflow(project_dir: nil, workflow_packname: nil)
    raise ArgumentError, "项目目录不能为空" if project_dir.nil?
    raise ArgumentError, "Workflow Package Name不能为空" if workflow_packname.nil?

    # 生成各种值
    app_name = workflow_packname.gsub(/[^a-zA-Z0-9\s]/, '').gsub(/\s+/, '')
    app_id_suffix = workflow_packname.gsub(/[^a-zA-Z0-9]/, '').downcase
    application_id = "com.heroneverdie101.#{app_id_suffix}"
    package_scheme = workflow_packname.gsub(/[^a-zA-Z0-9]/, '').downcase
    # bundle_scheme 现在在 update_application_id 中处理

    # 1. 更新 App Name
    success_app_name = update_app_name_with_packagename(
        project_dir: project_dir,
        package_name: workflow_packname
    )

    # 2. 更新 Application ID
    success_app_id = update_applicationid_with_workflowname(
        project_dir: project_dir,
        package_name: workflow_packname
    )

    # 3. 添加基于 package_name 的 Scheme
    success_package_scheme = add_test_scheme(
        project_dir: project_dir,
        scheme_name: package_scheme
    )
    if success_package_scheme
        puts "  ✓ 已添加Scheme: #{package_scheme} (基于 Package Name)"
    end

    # 注意:基于 Application ID 的 Scheme 将在 update_application_id 方法中添加
    # 因为 Application ID 可能在后续流程中被覆盖

    return success_app_name && success_app_id && success_package_scheme
end

.valid_build_number?(build_number) ⇒ Boolean

验证Build号是否在有效范围内

Parameters:

  • build_number (Integer)

    Build号

Returns:

  • (Boolean)

    是否有效



181
182
183
184
185
186
# File 'lib/pindo/module/android/android_build_config_helper.rb', line 181

def self.valid_build_number?(build_number)
    return false if build_number.nil?

    # Android versionCode的有效范围是1到2^31-1
    build_number >= 1 && build_number <= 2**31 - 1
end