Class: Pindo::AndroidConfigHelper
- Inherits:
-
Object
- Object
- Pindo::AndroidConfigHelper
- Defined in:
- lib/pindo/module/android/android_config_helper.rb
Class Method Summary collapse
-
.add_application_id_based_scheme(project_dir: nil) ⇒ Boolean
添加基于 Application ID 的 Scheme 读取当前项目的 Application ID 并添加对应的 Scheme.
-
.add_scheme_with_dom(manifest_path, activity, android_prefix, scheme_name) ⇒ Object
使用DOM操作添加scheme.
-
.add_scheme_with_text_replace(manifest_path, scheme_name) ⇒ Object
使用文本替换添加scheme.
-
.add_test_scheme(project_dir: nil, scheme_name: nil) ⇒ Boolean
添加测试scheme到Android工程.
-
.apply_config_from_repo(config_repo_dir: nil, project_dir: nil) ⇒ Boolean
从配置仓库应用配置(拷贝 config.json).
-
.check_scheme_exists(activity, android_prefix, scheme_name) ⇒ Object
检查scheme是否已存在.
-
.copy_google_services_from_config_repo(config_repo_dir: nil, project_dir: nil) ⇒ Boolean
从配置仓库拷贝 google-services.json 到项目的 launcher 目录.
-
.create_android_icons(icon_path: nil, output_dir: nil) ⇒ Hash
创建Android各种密度的icon(委托给 AndroidResHelper).
-
.create_element(doc, name, attr_name, attr_value) ⇒ Object
创建XML元素并设置属性.
-
.download_and_replace_icon_from_url(project_dir: nil, icon_url: nil) ⇒ Boolean
从URL下载Icon并替换Android工程的Icon.
-
.find_best_activity(manifest_candidates) ⇒ Object
查找最佳的Activity.
-
.find_manifests(project_dir, modules) ⇒ Object
查找所有可能的AndroidManifest.xml文件.
-
.install_android_icon(project_dir: nil, icon_dir: nil) ⇒ Boolean
安装Android icon到项目中(委托给 AndroidResHelper).
-
.update_android_project_version(project_dir: nil, version_name: nil, version_code: nil) ⇒ Boolean
更新Android工程版本号.
-
.update_app_name_with_packagename(project_dir: nil, package_name: nil) ⇒ Boolean
使用package_name更新Android应用名称(strings.xml中的app_name).
-
.update_application_id(project_dir: nil, application_id: nil) ⇒ Boolean
直接更新Application ID(不依赖package_name生成).
-
.update_applicationid_with_workflowname(project_dir: nil, package_name: nil) ⇒ Boolean
使用工作流名称生成并更新Android Application ID(build.gradle中的applicationId).
- .update_existing_scheme(manifest_path, activity, android_prefix, existing_scheme, scheme_name) ⇒ Object
-
.update_project_with_workflow(project_dir: nil, workflow_packname: nil) ⇒ Boolean
使用工作流配置一次性更新App Name、Application ID和URL Schemes 优化版本:提高性能,减少代码重复.
-
.valid_build_number?(build_number) ⇒ Boolean
验证Build号是否在有效范围内.
Class Method Details
.add_application_id_based_scheme(project_dir: nil) ⇒ Boolean
添加基于 Application ID 的 Scheme 读取当前项目的 Application ID 并添加对应的 Scheme
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 614 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 541 def self.add_application_id_based_scheme(project_dir: nil) # 参数验证 if project_dir.nil? || !File.directory?(project_dir) Funlog.instance.("项目路径无效: #{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.("未找到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.("无法从 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.("添加 Scheme 失败: #{bundle_scheme}") return false end rescue StandardError => e Funlog.instance.("添加基于 Application ID 的 Scheme 失败: #{e.message}") return false end end |
.add_scheme_with_dom(manifest_path, activity, android_prefix, scheme_name) ⇒ Object
使用DOM操作添加scheme
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 785 def self.add_scheme_with_dom(manifest_path, activity, android_prefix, scheme_name) begin doc = Nokogiri::XML(File.read(manifest_path)) # 创建intent-filter intent_filter = doc.create_element('intent-filter') # 添加子元素 intent_filter.add_child(create_element(doc, 'action', "#{android_prefix}:name", 'android.intent.action.VIEW')) intent_filter.add_child(create_element(doc, 'category', "#{android_prefix}:name", 'android.intent.category.DEFAULT')) intent_filter.add_child(create_element(doc, 'category', "#{android_prefix}:name", 'android.intent.category.BROWSABLE')) intent_filter.add_child(create_element(doc, 'data', "#{android_prefix}:scheme", scheme_name)) # 添加空白和缩进 activity.add_child(doc.create_text_node("\n ")) activity.add_child(intent_filter) activity.add_child(doc.create_text_node("\n ")) # 保存修改 xml_content = doc.to_xml(indent: 2, encoding: 'UTF-8') # 验证修改是否成功 if xml_content.include?("android:scheme=\"#{scheme_name}\"") File.write(manifest_path, xml_content) return true end return false rescue => e return false end end |
.add_scheme_with_text_replace(manifest_path, scheme_name) ⇒ Object
使用文本替换添加scheme
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 826 def self.add_scheme_with_text_replace(manifest_path, scheme_name) begin # 读取原始内容 xml_content = File.read(manifest_path) # 定义要添加的intent-filter scheme_intent_filter = %Q{ <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="#{scheme_name}"/> </intent-filter>} # 在</activity>前添加intent-filter if xml_content.match(/<\/activity>/) modified_xml = xml_content.gsub(/<\/activity>/) do |match| "#{scheme_intent_filter}\n #{match}" end # 保存修改 File.write(manifest_path, modified_xml) return true end return false rescue => e return false end end |
.add_test_scheme(project_dir: nil, scheme_name: nil) ⇒ Boolean
添加测试scheme到Android工程
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 686 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 620 def self.add_test_scheme(project_dir: nil, scheme_name: nil) # 参数验证 if scheme_name.nil? Funlog.instance.("需要提供scheme名称") return false end if project_dir.nil? || !File.directory?(project_dir) Funlog.instance.("项目路径无效: #{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.("未找到任何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.("无法找到合适的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.("已将scheme从'#{existing_scheme}'更新为'#{scheme_name}'") else Funlog.instance.("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.("无法添加scheme: #{scheme_name}") end return success end |
.apply_config_from_repo(config_repo_dir: nil, project_dir: nil) ⇒ Boolean
从配置仓库应用配置(拷贝 config.json)
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 512 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.("配置仓库路径无效: #{config_repo_dir}") return false end if project_dir.nil? || !File.directory?(project_dir) Funlog.instance.("项目路径无效: #{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 |
.check_scheme_exists(activity, android_prefix, scheme_name) ⇒ Object
检查scheme是否已存在
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 756 def self.check_scheme_exists(activity, android_prefix, scheme_name) scheme_exists = false existing_scheme = nil # 首先检查完全一致的scheme(修复:移除命名空间后使用 @scheme) activity.xpath("intent-filter/data[@scheme='#{scheme_name}']").each do |node| scheme_exists = true existing_scheme = scheme_name break end # 如果没有找到完全一致的,检查可能格式不同的scheme if !scheme_exists activity.xpath("intent-filter/data[@scheme]").each do |node| current_scheme = node["scheme"] normalized_current = current_scheme.to_s.downcase.strip.gsub(/[\s\-_]/, '') if normalized_current == scheme_name scheme_exists = true existing_scheme = current_scheme break end end end [scheme_exists, existing_scheme] end |
.copy_google_services_from_config_repo(config_repo_dir: nil, project_dir: nil) ⇒ Boolean
从配置仓库拷贝 google-services.json 到项目的 launcher 目录
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 506 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 447 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.("配置仓库路径无效: #{config_repo_dir}") return false end if project_dir.nil? || !File.directory?(project_dir) Funlog.instance.("项目路径无效: #{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.("配置仓库中未找到 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 |
.create_android_icons(icon_path: nil, output_dir: nil) ⇒ Hash
创建Android各种密度的icon(委托给 AndroidResHelper)
896 897 898 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 896 def self.create_android_icons(icon_path: nil, output_dir: nil) AndroidResHelper.create_icons(icon_name: icon_path, new_icon_dir: output_dir) end |
.create_element(doc, name, attr_name, attr_value) ⇒ Object
创建XML元素并设置属性
819 820 821 822 823 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 819 def self.create_element(doc, name, attr_name, attr_value) element = doc.create_element(name) element[attr_name] = attr_value element end |
.download_and_replace_icon_from_url(project_dir: nil, icon_url: nil) ⇒ Boolean
从URL下载Icon并替换Android工程的Icon
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 912 def self.download_and_replace_icon_from_url(project_dir: nil, icon_url: nil) # 参数校验 - 不抛出异常,返回 false if project_dir.nil? Funlog.instance.("Icon 替换失败: 项目目录不能为空") return false end if icon_url.nil? || icon_url.empty? Funlog.instance.("Icon 替换失败: Icon URL不能为空") return false end puts "\n检测到项目 Icon URL: #{icon_url}" # 创建临时目录(所有 icon 处理都在此目录进行) temp_icon_dir = File.join(project_dir, ".pindo_temp_android_icon") icon_download_path = nil replace_success = false begin # 先清理旧的临时目录,确保干净的环境 FileUtils.rm_rf(temp_icon_dir) if File.exist?(temp_icon_dir) FileUtils.mkdir_p(temp_icon_dir) icon_download_path = File.join(temp_icon_dir, "android_downloaded_icon.png") # 使用带重试机制的下载 unless IconDownloader.download_icon_with_retry(icon_url: icon_url, save_path: icon_download_path, max_retries: 3) Funlog.instance.("Icon 下载失败: 已重试3次") return false end # 生成 icon 目录(在临时目录内部),先清理确保干净环境 new_icon_dir = File.join(temp_icon_dir, "android_generated_icons") FileUtils.rm_rf(new_icon_dir) if File.exist?(new_icon_dir) FileUtils.mkdir_p(new_icon_dir) # 创建各种密度的 icon Funlog.instance.("正在创建 Android icon...") create_android_icons( icon_path: icon_download_path, output_dir: new_icon_dir ) Funlog.instance.("创建 icon 完成!") # 替换工程中的 icon Funlog.instance.("正在替换 icon...") install_android_icon( project_dir: project_dir, icon_dir: new_icon_dir ) Funlog.instance.("替换 icon 完成!") replace_success = true rescue => e Funlog.instance.("Icon 处理失败: #{e.message}") puts e.backtrace replace_success = false ensure # 清理临时文件(无论成功失败都清理) FileUtils.rm_rf(temp_icon_dir) if temp_icon_dir && File.exist?(temp_icon_dir) end return replace_success end |
.find_best_activity(manifest_candidates) ⇒ Object
查找最佳的Activity
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 714 def self.find_best_activity(manifest_candidates) require 'nokogiri' best_manifest = nil best_activity = nil android_prefix = nil manifest_candidates.each do |manifest_path| begin doc = Nokogiri::XML(File.read(manifest_path)) doc.remove_namespaces! # 查找包含MAIN和LAUNCHER的Activity activities = doc.xpath("//activity") activities.each do |activity| intent_filters = activity.xpath("intent-filter") intent_filters.each do |intent_filter| # 修复:移除命名空间后,应该使用 @name 而不是 @android:name has_main = intent_filter.xpath("action[@name='android.intent.action.MAIN']").any? has_launcher = intent_filter.xpath("category[@name='android.intent.category.LAUNCHER']").any? if has_main && has_launcher best_manifest = manifest_path best_activity = activity android_prefix = "android" break end end break if best_activity end rescue => e # 静默处理解析错误,继续尝试下一个文件 puts "解析 #{manifest_path} 时出错: #{e.message}" if ENV['PINDO_DEBUG'] end break if best_activity end [best_manifest, best_activity, android_prefix] end |
.find_manifests(project_dir, modules) ⇒ Object
查找所有可能的AndroidManifest.xml文件
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 691 def self.find_manifests(project_dir, modules) manifest_candidates = [] modules.each do |mod| mod_path = mod.is_a?(String) && !mod.start_with?(project_dir) ? File.join(project_dir, mod) : mod next unless File.directory?(mod_path) manifest_paths = [ File.join(mod_path, "src", "main", "AndroidManifest.xml"), File.join(mod_path, "AndroidManifest.xml") ] manifest_paths.each do |path| if File.exist?(path) manifest_candidates << path end end end manifest_candidates end |
.install_android_icon(project_dir: nil, icon_dir: nil) ⇒ Boolean
安装Android icon到项目中(委托给 AndroidResHelper)
904 905 906 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 904 def self.install_android_icon(project_dir: nil, icon_dir: nil) AndroidResHelper.install_icon(proj_dir: project_dir, new_icon_dir: icon_dir) end |
.update_android_project_version(project_dir: nil, version_name: nil, version_code: nil) ⇒ Boolean
更新Android工程版本号
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 177 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 61 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.("Android versionCode必须在1到#{2**31-1}之间,当前值:#{version_code}") return false end Funlog.instance.("正在更新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.("未找到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.("未找到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.("Android版本更新完成!") puts " ✓ 版本号已更新: #{version_name}" puts " ✓ Build号已更新: #{version_code}" return true end end Funlog.instance.("无法在gradle文件中找到或添加版本信息") return false rescue StandardError => e Funlog.instance.("更新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)
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 282 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 193 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.("未找到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.("strings.xml格式异常,无法添加app_name") return false end end rescue StandardError => e Funlog.instance.("更新App Name失败: #{e.message}") return false end end |
.update_application_id(project_dir: nil, application_id: nil) ⇒ Boolean
直接更新Application ID(不依赖package_name生成)
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 441 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 370 def self.update_application_id(project_dir: nil, application_id: nil) # 参数验证 if application_id.nil? || application_id.empty? Funlog.instance.("Application ID不能为空") return false end if project_dir.nil? || !File.directory?(project_dir) Funlog.instance.("项目路径无效: #{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.("未找到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.("gradle文件中未找到applicationId配置") return false end File.write(gradle_file, content) puts " ✓ 更新 #{File.basename(gradle_file)} 中的 applicationId" return true rescue StandardError => e Funlog.instance.("更新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)
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 364 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 288 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.("gradle文件中未找到applicationId字段") return false end File.write(gradle_file, content) puts " ✓ Application ID 已更新为: #{application_id} (来自: #{package_name})" return true rescue StandardError => e Funlog.instance.("更新Application ID失败: #{e.message}") return false end end |
.update_existing_scheme(manifest_path, activity, android_prefix, existing_scheme, scheme_name) ⇒ Object
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 857 def self.update_existing_scheme(manifest_path, activity, android_prefix, existing_scheme, scheme_name) begin doc = Nokogiri::XML(File.read(manifest_path)) # 查找所有intent-filter intent_filters = doc.xpath("//intent-filter") intent_filters.each do |intent_filter| # 检查intent-filter中的scheme intent_filter.xpath("data[@#{android_prefix}:scheme]").each do |data| current_scheme = data["#{android_prefix}:scheme"] if current_scheme == existing_scheme # 找到intent-filter,更新scheme data["#{android_prefix}:scheme"] = scheme_name end end end # 保存修改 xml_content = doc.to_xml(indent: 2, encoding: 'UTF-8') # 验证修改是否成功 if xml_content.include?("android:scheme=\"#{scheme_name}\"") File.write(manifest_path, xml_content) return true end return false rescue => e return false end end |
.update_project_with_workflow(project_dir: nil, workflow_packname: nil) ⇒ Boolean
使用工作流配置一次性更新App Name、Application ID和URL Schemes 优化版本:提高性能,减少代码重复
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 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 18 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号是否在有效范围内
182 183 184 185 186 187 |
# File 'lib/pindo/module/android/android_config_helper.rb', line 182 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 |