Class: Pindo::AndroidBuildHelper

Inherits:
Object
  • Object
show all
Includes:
ApkHelper, BaseAndroidHelper, GradleHelper, SoHelper, Singleton
Defined in:
lib/pindo/module/android/build_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApkHelper

#build_aab, #build_apk

Methods included from BaseAndroidHelper

#find_android_subproject, #get_build_tools, #get_ext_values, #get_keystore_config, #get_main_module, #modify_il2cpp_config, #remove_desktop_google_service, #unity_android_project?

Methods included from SoHelper

#build_so_library, #copy_so_files

Methods included from GradleHelper

#check_gradle_files, #update_build_gradle, #update_gradle_version

Class Method Details

.share_instanceObject



19
20
21
# File 'lib/pindo/module/android/build_helper.rb', line 19

def share_instance
  instance
end

Instance Method Details

#add_test_scheme(project_dir: nil, scheme_name: nil) ⇒ Object



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
55
56
57
58
59
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
# File 'lib/pindo/module/android/build_helper.rb', line 24

def add_test_scheme(project_dir:nil, scheme_name:nil)
  puts "正在为Android项目添加自定义scheme: #{scheme_name}"
  puts "项目路径: #{project_dir}, 当前工作目录: #{Dir.pwd}"
  
  # 参数验证
  return puts "错误: 需要提供scheme名称" if scheme_name.nil?
  return puts "错误: 需要提供项目路径" if project_dir.nil?
  return puts "错误: 项目路径不存在: #{project_dir}" unless File.directory?(project_dir)

  scheme_name = scheme_name.to_s.downcase.strip.gsub(/[\s\-_]/, '')
  # 查找所有可能的模块
  main_module = get_main_module(project_dir)
  search_modules = ["app", "unityLibrary"]
  search_modules << main_module if main_module
  search_modules = search_modules.compact.uniq
  
  puts "搜索的模块: #{search_modules.join(', ')}"

  # 查找所有可能的AndroidManifest.xml文件
  manifest_candidates = find_manifests(project_dir, search_modules)
  
  if manifest_candidates.empty?
    return puts "错误: 未找到任何AndroidManifest.xml文件"
  end
  
  puts "找到#{manifest_candidates.size}个AndroidManifest.xml文件"
  
  # 寻找最佳的AndroidManifest.xml和Activity
  manifest_path, main_activity, android_prefix = find_best_activity(manifest_candidates)
  
  if manifest_path.nil? || main_activity.nil?
    return puts "错误: 无法找到合适的Activity"
  end
  
  puts "已选择 #{manifest_path} 文件中的 #{main_activity['android:name'] || '主Activity'}"
  
  # 检查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
      # 格式不同,需要更新
      update_scheme = update_existing_scheme(manifest_path, main_activity, android_prefix, existing_scheme, scheme_name)
      if update_scheme
        puts "已将scheme从'#{existing_scheme}'更新为'#{scheme_name}'"
      else
        puts "尝试更新scheme格式失败,但scheme已存在: #{existing_scheme}"
      end
    else
      puts "scheme: #{scheme_name}已存在于#{activity_name}"
    end
    return
  end
  
  # 尝试添加scheme
  success = add_scheme_with_dom(manifest_path, main_activity, android_prefix, scheme_name)
  
  # 如果DOM操作失败,使用文本替换
  unless success
    add_scheme_with_text_replace(manifest_path, scheme_name)
  end
  
  puts "添加scheme操作完成,项目路径: #{project_dir}"
end

#auto_build_apk(project_dir, debug = false, ignore_sub = false) ⇒ Object



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

def auto_build_apk(project_dir, debug = false, ignore_sub = false)
  # 检查 gradle.properties 中是否设置了 Java Home
  gradle_properties_path = File.join(project_dir, "gradle.properties")
  if File.exist?(gradle_properties_path)
    puts "检查 gradle.properties 中的 Java Home 设置..."
    content = File.read(gradle_properties_path)
    java_home_match = content.match(/org\.gradle\.java\.home\s*=\s*(.+)/)
    if java_home_match && !java_home_match[1].empty?
      java_home_path = java_home_match[1].strip
      puts "找到 Java Home 路径: #{java_home_path}"
      # 设置环境变量
      ENV['JAVA_HOME'] = java_home_path
      ENV['PATH'] = "#{java_home_path}/bin:#{ENV['PATH']}"
      puts "已设置 JAVA_HOME 环境变量为: #{java_home_path}"
      puts "Java 版本信息:"
      system("java -version")
    end
  end

  # 检查
  if !ignore_sub
    sub_android_dir = find_android_subproject(project_dir)
    if sub_android_dir
      prepare_proj(sub_android_dir)
      # 构建 AAB 文件
      unless build_so_library(sub_android_dir)
        raise RuntimeError, "编译SO库失败:"
      end
      copy_so_files(sub_android_dir, project_dir)
    end
  end

  prepare_proj(project_dir)
  build_apk(project_dir, debug)
end

#dsign(project_path, debug) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pindo/module/android/build_helper.rb', line 152

def dsign(project_path, debug)
  keystore_config = get_keystore_config(project_path, debug)

  ks = keystore_config[:store_file]
  puts "读取 keystore path = #{ks}"
  ks_pass = keystore_config[:store_password]
  puts "读取 keystore pass = #{ks_pass}"
  key_alias = keystore_config[:key_alias]
  puts "读取 key alias = #{key_alias}"
  key_pass = keystore_config[:key_password]
  puts "读取 key pass = #{key_pass}"
end

#get_application_id(project_path) ⇒ Object



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

def get_application_id(project_path)
  main_module = get_main_module(project_path)
  return nil unless main_module

  # 尝试从 build.gradle 获取
  gradle_path = File.join(main_module, "build.gradle")
  if File.exist?(gradle_path)
    content = File.read(gradle_path)
    if content =~ /applicationId\s+['"]([^'"]+)['"]/
      return $1
    end
  end

  # 如果 build.gradle 中没有,尝试从 AndroidManifest.xml 获取
  manifest_path = File.join(main_module, "src", "main", "AndroidManifest.xml")
  if File.exist?(manifest_path)
    require 'nokogiri'
    doc = Nokogiri::XML(File.read(manifest_path))
    package = doc.at_xpath('//manifest/@package')&.value
    return package if package
  end

  nil
end