|
| 1 | +# AndroidRemoteBuildWithDocker |
| 2 | + |
| 3 | +当您在笔记本上编译一个大型的Android project时,可能觉得性能和电池都不够。而且,你还需要付出很多时间等待编译。 |
| 4 | +所以你一定很想要一个既高性能又便携的工作机器。那是不可能的。 |
| 5 | +我有过同样的困扰,不过我解决了。我过去常常建立一个远程编译的服务在我的台式机上。即可以用命令行控制远程编译服务,也可以使用Android Studio。 |
| 6 | +但是,有个问题是每次配置一个远程编译服务需要花很多时间。一堆步骤摆在我面前。这使我心累。所以,我做了这个repo,它同时集成了 两种技术:虚拟化技术 和 远程编译工具. |
| 7 | + |
| 8 | + |
| 9 | +[Mainframer](https://github.com/buildfoundation/mainframer)正式我说的远程编译工具。它用于同步文件和执行编译指令. 它的官方说法: |
| 10 | +> A tool that executes a command on a remote machine while syncing files back and forth. The process is known as remote execution (in general) and remote build (in particular cases). |
| 11 | +> It works via pushing files to the remote machine, executing the command there and pulling results to the local machine. |
| 12 | +
|
| 13 | +我需要重复: 它不但可以用命令行控制,还可以用Android studio。 |
| 14 | + |
| 15 | +在开头,我说配置远程编译的 Android环境需要**花很多时间**,但是现在我们有虚拟化技术, 比如 `Docker` and `Kubernetes`, 这项技术我经常使用,它使用简单且易于备份。 |
| 16 | + |
| 17 | +***在这个repo里, 我创建了一个docker镜像。它同时包含Android开发环境,和[Mainframer](https://github.com/buildfoundation/mainframer)***. |
| 18 | +如果你有高性能台式机/其他,它就是作为服务器。你可以运行这个repo在你的服务器。你的这台服务器,不要求一定与你的开发机器在 **同一局域网下**。***关于这点,我需要补充的是:在中国,你可能没有外网IP,就没办法在广域网下面连接到你的这台服务器,你可以尝试内网穿透技术。*** |
| 19 | + |
| 20 | + |
| 21 | +另外,你可以把这个docker image运行在云服务上。比如 google, amazon。 为了您的远程的且高性能的编译. Cloud services在性能和价格上面往往是弹性的。另外,你可能觉得cloud server高延迟.我需要阐明 : **延迟不会影响您的编译体验**因为 mainframer不会与您的笔记本沟通得那么的频繁。仅仅第一次编译,会传输大量的文件。除第一次之外的编译,不需要同步那么多的文件。 |
| 22 | + |
| 23 | +如上所述,我介绍了该repo的远程编译解决的一些问题。它可以解决笔记本性能不够,而台式机写代码又不够便携的问题,还有电池问题。 |
| 24 | +所以,当你建立好这个远程编译服务,你可以去咖啡馆,草地上,阳光下用笔记本写代码,且不需要充电。 |
| 25 | + |
| 26 | +接下来介绍如何使用。 |
| 27 | + |
| 28 | +## Server configuration |
| 29 | +<details><summary>click to expand</summary> |
| 30 | +第一步是编译docker镜像。在您的terminal中, run `docker build -t mainframer-docker .` |
| 31 | + |
| 32 | +最后一步是启动镜像为一个容器: run `docker run --restart always -d -p 23:22 mainframer-docker`. |
| 33 | + |
| 34 | +现在,如果没有errors, run `docker container ls | grep mainframer-docker` 去观察容器是否启动。我相信一切都很正常。 |
| 35 | + |
| 36 | +</details> |
| 37 | + |
| 38 | +## Client configuration |
| 39 | +<details><summary>click to expand</summary> |
| 40 | + |
| 41 | + 客户端这边的步骤,我们需要做两件事,一个SSH key和一个ssh配置文件用于你的笔记本与服务器沟通。 |
| 42 | + |
| 43 | + |
| 44 | + ```bash |
| 45 | + ssh-keygen -t rsa -f ~/.ssh/remote-builder -q -N "" |
| 46 | + #brew install ssh-copy-id |
| 47 | + ssh-copy-id -i ~/.ssh/remote-builder -p 23 root@127.0.0.1 |
| 48 | + |
| 49 | + echo -e "Host remote_builder |
| 50 | + User root |
| 51 | + HostName 127.0.0.1 |
| 52 | + Port 23 |
| 53 | + IdentityFile ~/.ssh/remote-builder |
| 54 | + PreferredAuthentications publickey |
| 55 | + ControlMaster auto |
| 56 | + ControlPath /tmp/%r@%h:%p |
| 57 | + ControlPersist 1h" >> ~/.ssh/config |
| 58 | + ``` |
| 59 | + **REPLACE IP ADDRESS** |
| 60 | + |
| 61 | +用ssh连接服务器: `ssh remote_builder` |
| 62 | + |
| 63 | +为了Android,你拷贝我已经建立了的`.mainframer` 目录和一个文件`./mainframer.sh`到您的项目下。然后,你可以运行` bash ./mainframer.sh ./gradlew assembleDebug`去尝试启动命令行编译. |
| 64 | + |
| 65 | +**And now enjoy faster builds** |
| 66 | + |
| 67 | +### DEFAULT USER ROOT:ROOT IS USED IN THIS SETUP. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +### 使用Android Studio远程编译 |
| 72 | + |
| 73 | +1. 用Android Studio打开 your project. |
| 74 | + |
| 75 | +3. 点击 **Run → Edit Configuration → +**. |
| 76 | + |
| 77 | +4. 选择 your **Android App**. |
| 78 | + |
| 79 | +5. 使用一个新的名字, e.g. remote-build. |
| 80 | + |
| 81 | +6. 在 **Module**, 选择submodule name, 可能是 `app`。 |
| 82 | + |
| 83 | +7. 在 **Before Launch**, 点击 **-** 删除 `Gradle-aware Make` |
| 84 | + |
| 85 | +8. 在 **Before Launch**, 点击 **+**, 添加 **Run External Tool**, 输入新的名字,比如: `remote assembleDebug`. |
| 86 | + |
| 87 | +9. 在 **Program**, 输入 `bash`. |
| 88 | + |
| 89 | +10. 在 **Parameters** 输入 `mainframer.sh ./gradlew :app:assembleDebug -Pandroid.enableBuildCache=true` |
| 90 | + |
| 91 | +11. 最后一步, 在 **Working directory** , 输入 `$ProjectFileDir$`. |
| 92 | + |
| 93 | + |
| 94 | +</details> |
| 95 | + |
| 96 | +## 补充 |
| 97 | + |
| 98 | +**How to install other sdk?** |
| 99 | + |
| 100 | +> 大多数情况下这是不需要关心的事情。sdk都是会自动下载的,androidsdk都已经很智能了。 |
| 101 | +修改 Dockerfile 去重编docker镜像.或者使用如下步骤: |
| 102 | + |
| 103 | +1. 启动 你的 docker. |
| 104 | +2. 运行 `ssh remote_builder` 进入该 docker container. 这步可以用docker命令代替. |
| 105 | +3. 此时你已经进入bash在你的docker容器中, run: |
| 106 | + |
| 107 | + `/sdk/tools/bin/sdkmanager--install "ndk;21.0.6113669" --channel=3`. |
| 108 | + |
| 109 | +**Mainframer also can build other kind of project** |
| 110 | +You can fork this project to edit `Dockerfile` to make it work for your project. |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +**其他Mainframer的功能:** |
| 115 | +[mainframer/v2.1.0 doc](https://github.com/buildfoundation/mainframer/tree/v2.1.0/samples/gradle-android) |
| 116 | +简体 |
0 commit comments