[AOSP笔记]编译Android Studio

209 阅读3分钟

下载

官方文档:android.googlesource.com/platform/to…

经过反复测试Issue 394726980studio-2024.3.2-alpha03是最接近当时studio-main的tag

$ mkdir studio-2024.3.2-alpha03 && cd studio-2024.3.2-alpha03
$ repo init -u https://android.googlesource.com/platform/manifest -b studio-2024.3.2-alpha03
$ repo sync -c -j$(sysctl -n hw.ncpu)
# 中国大陆推荐清华源
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b studio-2024.3.2-alpha03

环境配置

官方文档:android.googlesource.com/platform/to…

  • 确保prebuilts/tools可用:repo sync -cj8 prebuilts/tools

  • 确认Bazel可用:tools/base/bazel/bazel version

$ cd studio-main/
$ tools/base/bazel/bazel version
  Build label: …
  Build target: …
  …
  • 移除vender/google
diff --git a/android/BUILD b/android/BUILD
index 77896ad44dc..e2899eb5617 100644
--- a/android/BUILD
+++ b/android/BUILD
@@ -362,7 +362,6 @@ filegroup(
         "//tools/base/third_party/kotlin:kotlin-m2repository",
         "//tools/base/threading-agent:threading_agent.jar",
         "//tools/base/tracer:trace_agent",
-        "//tools/vendor/google/ml/aiplugin/ij-platform/atlas:atlas_artifacts",
     ] + select({
         "@platforms//os:windows": [],
         "//conditions:default": [
@@ -433,19 +432,6 @@ filegroup(
         "//tools/base/sdklib:schema-generated",
         "//tools/data-binding:compilerCommon.antlr.shaded",
         "//tools/data-binding:compilerCommon.antlr_runtime.shaded",
-        "//tools/vendor/google/asfp/base:studio-intellijext",
-        "//tools/vendor/google/directaccess-client:directaccess_client_proto",
-        "//tools/vendor/google/firebase/integration:libtitan_java_proto",
-        "//tools/vendor/google/firebase:libfirebase_java_proto",
-        "//tools/vendor/google/firebase:libgemini_java_proto",
-        "//tools/vendor/google/firebase:libtitan_java_proto",
-        "//tools/vendor/google/game-tools/main:game-tools-protos",
-        "//tools/vendor/google/game-tools/packaging:game-tools-protos",
-        "//tools/vendor/google/ml/aiplugin/core:aida_grpc",
-        "//tools/vendor/google/ml/aiplugin/core:aida_proto",
-        "//tools/vendor/google/ml/aiplugin/core:aida_protobuf",
-        "//tools/vendor/google/ml/aiplugin/core:gemini_plugin_proto",
-        "//tools/vendor/google/url-assistant:urlassistant_proto",
     ],
     visibility = ["//visibility:public"],
 )
  • 链接Android SDK,保证里面有SDK API 34和NDK r27(Linux/macOS)
# Linux
$ ln -s ~/Android/Sdk prebuilts/studio/sdk/linux
# macOS
$ ln -s ~/Library/Android/sdk prebuilts/studio/sdk/darwin 

因为改用studio-2024.3.2-alpha03,基于IdeaIC-2024.3,下载链接改为ideaIC-243.22562.218.zip

        remote_platform(
            name = "studio-sdk",
            url = "https://www.jetbrains.com/intellij-repository/releases/com/jetbrains/intellij/idea/ideaIC/243.22562.218/ideaIC-243.22562.218.zip",
            sha256 = "9887033a62c0b53f39a53cb14db71fc54f3e3b1f96ea7f2526a315e2d7371471",
            plugins = [
                ...
            ],
        },
  • 确保vm-options解析正确
  1. tools/adt/idea/studio/studio.bzl
diff --git a/studio/studio.bzl b/studio/studio.bzl
index d413af31ef1..aaee8a8ecd6 100644
--- a/studio/studio.bzl
+++ b/studio/studio.bzl
@@ -1335,6 +1335,16 @@ def intellij_platform_import(name, spec):
         visibility = ["//visibility:public"],
     )
 
+    native.filegroup(
+        name = name + "-vm-options",
+        srcs = select({
+            "@platforms//os:windows": ["bin/win/idea64.exe.vmoptions"],
+            "@platforms//os:macos": ["bin/mac/idea64.vmoptions"],
+            "//conditions:default": ["bin/linux/idea64.vmoptions"],
+        }),
+        visibility = ["//visibility:public"],
+    )
+
     native.filegroup(
         name = name + "-build-txt",
         srcs = ["build.txt"],
  1. tools/adt/idea/studio/jvm_args_extractor.py
diff --git a/studio/jvm_args_extractor.py b/studio/jvm_args_extractor.py
index dee492253e5..9e5ede929c6 100644
--- a/studio/jvm_args_extractor.py
+++ b/studio/jvm_args_extractor.py
@@ -17,9 +17,7 @@ def main():
     out: Path = args.out
 
     # Read JVM args from product-info.json in the IDE distribution.
-    product_info = json.loads(product_info_file.read_text("ascii"))
-    (launch_config,) = product_info["launch"]
-    required_jvm_args = launch_config["additionalJvmArguments"]
+    required_jvm_args = get_required_jvm_args(product_info_file)
 
     # Get ready to substitute $IDE_HOME path vars.
     ide_home = product_info_file.parent
@@ -43,5 +41,52 @@ def get_ide_home_macro() -> str:
     else: sys.exit(f"Unrecognized system: {system}")
 
 
+def get_required_jvm_args(product_info_file):
+    # 加载 product-info.json 文件内容
+    product_info = json.loads(product_info_file.read_text("ascii"))
+
+    # 获取当前系统信息
+    current_os = platform.system().lower()  # 获取当前操作系统,例如 "Windows", "Linux", "Darwin(macOS)"
+    current_arch = platform.machine().lower()  # 获取当前计算机架构,例如 "x86_64", "aarch64"
+    print("current_os " + current_os + ", current_arch " + current_arch)
+
+    # 映射操作系统名称以便匹配 JSON 数据中的 `os` 字段
+    os_mappings = {
+        "windows": "Windows",
+        "linux": "Linux",
+        "darwin": "macOS"
+    }
+
+    # 根据当前操作系统筛选 launch 配置
+    current_os_json = os_mappings.get(current_os)
+    if not current_os_json:
+        raise ValueError(f"不支持的操作系统名称: {current_os}")
+
+    # 映射操作系统架构以便匹配 JSON 数据中的 `arch` 字段
+    arch_mappings = {
+        "arm64": "aarch64",
+        "x86_64": "amd64",
+    }
+
+    # 根据当前系统架构筛选 launch 配置
+    current_arch_json = arch_mappings.get(current_arch)
+    if not current_arch_json:
+        raise ValueError(f"不支持的系统架构: {current_arch}")
+
+    matching_launch_configs = [
+        config for config in product_info["launch"]
+        if config["os"] == current_os_json and config["arch"] == current_arch_json
+    ]
+
+    if not matching_launch_configs:
+        raise ValueError(f"没有找到与操作系统 {current_os_json} 和架构 {current_arch} 匹配的配置")
+
+    # 只取第一个匹配项作为目标配置
+    launch_config = matching_launch_configs[0]
+
+    # 提取所需的 JVM 参数
+    return launch_config["additionalJvmArguments"]
+
+
 if __name__ == "__main__":
     main()

编译Android IDEA插件

$ ./tools/base/bazel/bazel build //tools/adt/idea/android:artifacts

参考