在 VirtualBox 中安装 Android 系统
Android VirtualBox
![](/data/attachment/album/201809/18/001443wijtrv00r8sr90b0.png)


如果你正在开发 Android 应用，也许会遇到小麻烦。诚然，ios 移动开发有 macOS 系统平台为其提供友好便利性， Android 开发仅有支持少部分 Android 系统（其中还包括可穿戴设备系统）的 Android Studio 工具。


毋庸置疑，所有的二进制工具、SDK 工具、开发框架工具以及调试器都会产生大量日志和其他各种文件，使得你的文件系统很混乱。一个有效的解决方法就是在 VirtualBox 中安装 Android 系统，这样还解决了 Android 开发中最为棘手问题之一 —— 设备模拟器。你可以在该虚拟机里测试应用程序，也可以使用 Android 的内部功能。因此，让我们迫不及待的开始吧！


### 准备工作


首先，你需要在你的系统上安装 VirtualBox，可从[这里](https://www.virtualbox.org/wiki/Downloads)下载 Windows 版本、macOS 版本或者各种 Linux 版本的 VitualBox。然后，你需要一个能在 x86 平台运行的 Android 镜像，因为 VirtualBox 为虚拟机提供运行 x86 或者 x86\_64（即 AMD64）平台的功能。


虽然大部分 Android 设备都在 ARM 上运行，但我们依然可以从 [Android on x86 项目](http://www.android-x86.org/) 中获得帮助。这些优秀的开发者已经将 Android 移植到 x86 平台上运行（包括实体机和虚拟机），我们可以下载最新版本的 Android 7.1。你也可以用之前更为稳定的版本，本文写作时最新稳定版是 Android 6.0。


### 创建 VM 虚拟机


打开 VirtualBox，单击左上角的 “新建” 按钮，在弹出的窗口中选择 “类型：Linux” ，然后根据下载的 ISO 镜像来确定版本，x86 对应 “32-bit”，x86\_64 对应 “64-bit”，此处选择 “Linux 2.6 / 3.x / 4.x (64-bit)”。


RAM 大小设置为 2 GB 到你系统能提供的最大内存之间。如果你想模拟真实的使用环境你可以设置 6 GB RAM 和 32 GB ROM。


![](/data/attachment/album/201809/18/001450x9gxkkq8wxv8zn0w.png)


![](/data/attachment/album/201809/18/001451iew2wdqwygaraawr.png)


创建完成后，你还需要做一些设置，添加更多的处理器核心，提高开机显示内存。在 VM 上打开设置选项，“设置 -> 系统 -> 处理器”，如果硬件条件允许，可以多分配一些处理器。


![](/data/attachment/album/201809/18/001451cv7grgl1l6go8x3r.png)


在 “设置 -> 显示 -> 显存大小” 中，你可以分配一大块内存并开启 3D 加速功能。


![](/data/attachment/album/201809/18/001452ajgwj8l1wpk9zklp.png)


现在我们可以启动 VM 虚拟机了。


### 安装 Android


首次启动 VM 虚拟机，VirtualBox 会提示你需要提供启动媒介，选择之前下载好的Android 镜像。


![](/data/attachment/album/201809/18/001453iw4wi4bz0ncewinv.png)


下一步，如果想长时间使用 Android，选择 “Installation” 选项，你也可以选择 Live 模式体验 Android 环境。


![](/data/attachment/album/201809/18/001454nurscj3j1jpom1j1.png)


按回车键。


#### 分区


分区是通过文本界面操作，并没有友好的 GUI 界面，所以每个操作都需要小心对待。例如，在第一屏中还没有创建分区并且只检测到原始（虚拟）硬盘时显示如下。


![](/data/attachment/album/201809/18/001454zsvpvmy7y7yvg3a2.png)


红色字母 `C` 和 `D` 表明 `C` 开头选项可以创建或者修改分区，`D` 开头选项可以检测设备。你可以选择 `D` 开头选项，然后它就会检测硬盘，也可不进行这步操作，因为在启动的时候它会自动检测。


我们选择 `C` 开头选项，在虚拟盘中创建分区。官方不推荐使用 GPT 格式，所以我们选择 “No” 并按回车键。


![](/data/attachment/album/201809/18/001454r5brfh9xd9h8upcz.png)


现在你被引导到 fdisk 工具页面。


![](/data/attachment/album/201809/18/001455pzexeffexudox2em.png)


为了简洁起见，我们就只创建一个较大的分区，使用方向键来选择 “New” ，然后选择 “Primary”，按回车键以确认。


![](/data/attachment/album/201809/18/001455t8h2gbl288qnhotb.png)


分区大小系统已经为你计算好了，按回车键确认。


![](/data/attachment/album/201809/18/001456njebitt9lrxxxxij.png)


这个分区就是 Android 系统所在的分区，所以需要它是可启动的。选择 “Bootable”，然后按回车键（上方表格中 “Flags” 标志下面会出现 “boot” 标志），进一步，选择 “Write” 选项，保存刚才的操作记录并写入分区表。


![](/data/attachment/album/201809/18/001456jbad46nw0et4ijoz.png)


现在你可以选择退出分区工具，然后继续安装过程。


![](/data/attachment/album/201809/18/001457n1qfnovnqqma4qyn.png)


#### 文件系统格式化为 EXT4 并安装 Android


在“Choose Partition”分区页面上会出现一个刚刚我们创建的分区，选择它并点击“OK”进入。


![](/data/attachment/album/201809/18/001458f0xhpn6xjt6op8x9.png)


在下一个菜单中选择 Ext4 作为实际的文件系统，在下一页中选择 “Yes” 然后格式化开始。会提示是否安装 GRUB 引导工具以及是否允许在目录 `/system` 进行读写，都选择 “Yes” 。现在，安装进程开始。


安装完成后，当系统提示可以重启的时候你可以安全地重启系统。在重启之前，你可以先关机，然后在 VitualBox 的 “设置 -> 存储” 中检查 Android iso 镜像是否还连接在虚拟机上，如果在，将它移除。


![](/data/attachment/album/201809/18/001459kzhm95fc9w8zm565.png)


移除安装媒介并保存修改，再去启动 VM 虚拟机。


#### 运行 Android


在 GRUB 引导界面，有调试模式和普通模式的选项。我们选择默认选项，如下图所示。


![](/data/attachment/album/201809/18/001459nunzjb27iep92r29.png)


如果一切正常，你将会看到如下界面：


![](/data/attachment/album/201809/18/001500ylqz8x71if3518kq.png)


如今的 Android 系统使用触摸交互而不是鼠标。不过 Android-x86 平台提供了鼠标操作支持，但开始时可能需要方向键来辅助操作。


![](/data/attachment/album/201809/18/001501js4t737j05u55ju7.png)


移动到”let’s go“按钮并按下回车键。选择 “Set up as new” 选项，回车确认。


![](/data/attachment/album/201809/18/001502n2ynnyt47ppnc9t2.png)


在提示用谷歌账户登陆之前，系统检查更新并检测设备信息。你可以跳过这一步，直接去设置日期和时间、用户名等。


还有一些其他的选项，和让你设置一个新的 Android 设备类似。选择 “I Agree” 选项同意有关更新、服务等的相应的选项，当然谷歌的服务条款是不得不同意的。


![](/data/attachment/album/201809/18/001503v3hhz48qnhdq0b0w.png)


在这之后，因为它是个虚拟机，所以可能需要添加额外的 email 账户来设置 “On-body detection”，大部分的选项对我们来说都没有多大作用，因此可以选择 ”All Set“。


接下来，它会提示你选择主屏应用，这个根据个人需求选择。现在我们进入了一个虚拟的 Android 系统。


![](/data/attachment/album/201809/18/001505trff6lmnqldjiook.png)


如果你需要在 VM 做一些交互测试，有个可触摸屏幕会提供很大的方便，因为那样才更接近真实使用环境。


希望这篇教程会给你带来帮助。




---


via: <https://linuxhint.com/install_android_virtualbox/>


作者：[Ranvir Singh](https://linuxhint.com/author/sranvir155/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[jrglinux](https://github.com/jrglinux) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Needless to say, all the binaries, SDKs, frameworks and debuggers are going to pollute your filesystem with lots and lots of files, logs and other miscellaneous objects. An efficient work around for this is installing Android on your VirtualBox which takes away one of the sluggiest aspect of Android development — The device emulator. You can use this VM to run your test application or just fiddle with Android’s internals. So without further ado let’s set on up!

# Getting Started

To get started we will need to have VirtualBox installed on our system, you can get a copy for Windows, macOS or any major distro of Linux [here](https://www.virtualbox.org/wiki/Downloads). Next you would need a copy of Android meant to run on x86 hardware, because that’s what VirtualBox is going to offer to a Virtual Machine an x86 or an x86_64 (a.k.a AMD64) platform to run.

While most Android devices run on ARM, we can take help of the project [Android on x86](http://www.android-x86.org/). These fine folks have ported Android to run on x86 hardware (both real and virtual) and we can get a copy of the latest release candidate (Android 7.1) for our purposes. You may prefer using a more stable release but in that case Android 6.0 is about as latest as you can get, at the time of this writing.

## Creating VM

Open VirtualBox and click on “New” (top-left corner) and in the Create Virtual Machine window select the type to be Linux and version Linux 2.6 / 3.x /4.x (64-bit) or (32-bit) depending upon whether the ISO you downloaded was x86_64 or x86 respectively.

RAM size could be anywhere from 2 GB to as much as your system resources can allow. Although if you want to emulate real world devices you should allocate upto 6GB for memory and 32GB for disk size which are typical in Android devices.

Upon creation, you might want to tweak a few additional settings, add in an additional processor core and improve display memory for starters. To do this, right-click on the VM and open up settings. In the Settings → System → Processor section you can allocate a few more cores if your desktop can pull it off.

And in Settings → Display → Video Memory you can allocate a decent chunk of memory and enable 3D acceleration for a more responsive experience.

Now we are ready to boot the VM.

### Installing Android

Starting the VM for the first time, VirtualBox will insist you to supply it with a bootable media. Select the Android iso that you previously downloaded to boot the machine of with.

Next, select the Installation option if you wish to install Android on the VM for a long term use, otherwise feel free to log into the live media and play around with the environment.

Hit <Enter>.

### Partitioning the Drive

Partitioning is done using a textual interface, which means we don’t get the niceties of a GUI and we will have to use the follow careful at what is being shown on the screen. For example, in the first screen when no partition has been created and just a raw (virtual) disk is detected you will see the following.

The red lettered C and D indicates that if you hit the key *C *you can create or modify partitions and *D *will detect additional devices. You can press *D *and the live media will detect the disks attached, but that is optional since it did a check during the boot.

Let’s hit *C *and create partitions in the virtual disk. The offical page recommends against using GPT so we will not use that scheme. Select No using the arrow keys and hit *<Enter>.*

And now you will be ushered into the fdisk utility.

We will create just a single giant partition so as to keep things simple. Using arrow keys navigate to the *New *option and hit *<Enter>. * Select primary as the type of partition, and hit *<Enter> *to confirm

The maximum size will already be selected for you, hit *<Enter> *to confirm that.

This partition is where Android OS will reside, so of course we want it to be bootable. So select *Bootable *and hit enter (Boot will appear in the flags section in the table above) and then you can navigate to the Write section and hit *<Enter> *to write the changes to the partitioning table.

Then you can *Quit *the partitioning utility and move on with the installation.

### Formatting with Ext4 and installing Android

A new partition will come in the *Choose Partition *menu where we were before we down the partitioning digression. Let’s select this partition and hit *OK*.

Select ext4 as the de facto file system in the next menu. Confirm the changes in the next window by selecting ** Yes** and the formatting will begin. When asked, say

**to the GRUB boot loader installation. Similarly, say**

*Yes***to allowing read-write operations on the**

*Yes**/system*directory. Now the installation will begin.

Once it is installed, you can safely reboot the system when prompted to reboot. You may have to power down the machine before the next reboot happens, go to Settings → Storage and remove the android iso if it is still attached to the VM.

Remove the media and save the changes, before starting up the VM.

### Running Android

In the GRUB menu you will get options for running the OS in debug mode or the normal way. Let’s take a tour of Android in a VM using the default option, as shown below:

And if everything works fine, you will see this:

Now Android uses touch screen as an interface instead of a mouse, as far as its normal use is concerned. While the x86 port does come with a mouse point-and-click support you may have to use arrow keys a lot in the beginning.

Navigate to let’s go, and hit enter, if you are using arrow keys and then select *Setup as New.*

It will check for updates and device info, before asking you to sign in using a Google account. You can skip this if you want and move on to setting up Data and Time and give your username to the device after that.

A few other options would be presented, similar to the options you see when setting up a new Android device. Select appropriate options for privacy, updates, etc and of course Terms of Service, which we might have to Agree to.

After this, it may ask you to add another email account or set up “On-body detection” since it is a VM, neither of the options are of much use to us and we can click on “All Set”

It would ask you to select Home App after that, which is upto you to decide, as it is a matter of Preference and you will finally be in a virtualized Android system.

You may benefit greatly from a touch screen laptop if you desire to do some intensive testing on this VM, since that would emulate a real world use case much closely.

Hope you have found this tutorial useful in case, you have any other similar request for us to write about, please feel free to reach out to us.