如何编译Android AOSP内核并使用Android模拟器进行测试?

发布于 2024-08-12 21:47:25 字数 109 浏览 11 评论 0原文

有没有人成功编译了android内核并在Android模拟器中测试了它,如果是的话,有什么特别需要做的吗?

SDK 的文档非常好,但是我很难找到编译内核和在模拟器中设置自定义机器的文档。

Has anyone successfully compiled the android kernel and tested it in the Android emulator, and if so is there anything that special that needs to be done?

Documentation for the SDK is excellent, however documentation for compiling the kernel and setting up a custom machine in the emulator has been hard for me to find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

想你的星星会说话 2024-08-19 21:47:25

自 2009 年 8 月以来,内核不再是按照说明下载 Android 开源项目源代码时获得的标准存储库清单的一部分。在模拟器上成功下载、构建和运行特定内核所需的步骤如下:

  • 通过将 Android 内核添加到您的存储库清单或通过手动运行来获取 Android 内核:
    git clone https://android.googlesource.com/kernel/goldfish.git
  • 查看使用模拟器的正确分支,即 goldfish:
    git checkout -t origin/android-goldfish-2.6.29 -b goldfish
  • 生成模拟器配置(qemu模拟器运行arm代码,即arm配置):
    make ARCH=arm goldfish_defconfig

    • 如果这不起作用,请尝试 make ARCH=arm goldfish_armv7_defconfig
  • 现在使用随开源项目分发的交叉编译工具构建内核:
    make ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-
  • 这种方式构建的内核应该最终出现在 arch/arm 中内核树的 /boot 文件夹(放置来自 git clone 的代码的位置)
  • 要使用内核运行模拟器,有两种选择,要么将其复制到开源项目的预构建内核文件夹中以替换标准内核。另一个选项是使用内核选项集启动模拟器:
    emulator -kernel mydroid/kernel/common/arch/arm/boot/zImage

请注意,我在上面的描述中使用了默认路径,您需要将它们更改为适用于您的设置的路径。自从我上次测试这个以来已经有一段时间了,但我认为它应该有效。

一些额外信息:
在标准 Android 开源发行版中,内核作为预构建的二进制文件发布在 mydroid/prebuilt/android-arm/kernel 文件夹中,并且不包含源代码。据我所知,内核源代码被从默认清单中删除有两个原因。一是大多数人不会经常使用的平台组件需要大量带宽和磁盘空间。另一个原因是,由于内核是使用内核构建系统构建的,而不是作为 aosp 构建系统的一部分,因此将其分开是有意义的。内核的公共分支是模拟器使用的分支。还有实验、msm(高通平台)和 Omap(TI 平台)等分支。如果您想将 Android 内核与硬件一起使用,这些对您来说可能更有趣。

Since August 2009 the kernel is no longer part of the standard repo manifest that you get when you follow the instructions to download the source code for the android open source project. The steps that are needed to successfully download, build and run a specific kernel on the emulator are as follows:

  • Get the Android kernel either by adding it to your repo manifest or manually by running:
    git clone https://android.googlesource.com/kernel/goldfish.git
  • Check out the correct branch for working with the emulator, i.e. goldfish:
    git checkout -t origin/android-goldfish-2.6.29 -b goldfish
  • Generate the emulator configuration (qemu emulator runs arm code, i.e. an arm config):
    make ARCH=arm goldfish_defconfig

    • if that doesn't work, try make ARCH=arm goldfish_armv7_defconfig
  • Now build the kernel using the cross compilation tools distributed with the open source project:
    make ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-
  • The kernel built this way should end up in the arch/arm/boot folder of your kernel tree (where you put the code from git clone)
  • To run the emulator with your kernel there are two alternatives, either copy it to the prebuilt kernel folder of the open source project to replace the standard kernel. The other option is to start the emulator with the kernel option set:
    emulator -kernel mydroid/kernel/common/arch/arm/boot/zImage

Note that I have used the default paths in the above description, you need to change them to what applies to your setup. It has been a some time since last time I tested this but I think it should work.

Some extra information:
In the standard Android open source distribution the kernel is distributed as a pre-built binary in the mydroid/prebuilt/android-arm/kernel folder and the source code is not included. The kernel source was removed from the default manifest for two reasons as I take it. One is that it takes a lot of bandwith and diskspace for a platform component that most people will not work with much. The other reason is that since the kernel is built with the kernel build system and not as part of the aosp build system it makes sense to keep it separated. The common branch for the kernel is the one used by the emulator. There are also branches for experimental, msm (Qualcomm platforms) and Omap (TI platform) and maybe some more. If you want to use the Android kernel with hardware these may be more interesting to you.

清风挽心 2024-08-19 21:47:25

只是纠正 BMB 帖子中的一些内容(这对我来说非常有用,它拯救了我的项目):

  • git clone git://android.git.kernel.org/kernel/common.git (缺少内核);
  • git checkout -t origin/android-goldfish-2.6.29 -b goldfish (相同);
  • make ARCH=arm goldfish_defconfig (同上) ;
  • 让 ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi- (同上)
  • 模拟器 -avd my_avd -kernel mydroid/kernel/common/arch/arm/boot /zImage (这里我在命令中添加了一个 avd ,没有它对我来说不起作用)。

Just to correct a few things from BMB's post (which was very useful to me, it saved my project) :

  • git clone git://android.git.kernel.org/kernel/common.git (the kernel missed) ;
  • git checkout -t origin/android-goldfish-2.6.29 -b goldfish (the same) ;
  • make ARCH=arm goldfish_defconfig (idem) ;
  • make ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi- (idem)
  • emulator -avd my_avd -kernel mydroid/kernel/common/arch/arm/boot/zImage (here I added an avd in the command, it didn't work without for me).

2024-08-19 21:47:25

Ubuntu 17.10 主机上的全自动 Android 8.1 来宾

# Download the source. Takes several minutes.
curl https://storage.googleapis.com/git-repo-downloads/repo >repo
chmod a+x repo
./repo init -b android-8.1.0_r1 --depth 1 -u https://android.googlesource.com/platform/manifest
./repo sync -c  -j $(($(nproc) - 2)) --no-tags --no-clone-bundle

# Do the actual build. Takes minutes / hours.
. build/envsetup.sh
lunch aosp_x86_64-eng
USE_CCACHE=1 CCACHE_DIR=ccache make -j $(($(nproc) - 2))

# Run the emulator.
emulator -show-kernel

out/ 构建目录占用约 90Gb,树的其余部分约 40Gb(不包括 CCACHE)。

启动模拟器后约 1-2 分钟,主屏幕显示:

如果您在启动 Android 的主机终端上按 Enter 键,您将在主机终端上获得 Android 系统的 shell:

在此处输入图像描述

注意:

  • ./repo init -b 必须指向标签master 分支总是损坏,-release 分支也是如此。

    标签列表位于:https://android.googlesource.com/platform/manifest 或通过克隆该存储库。

    分支总是损坏可能有两个原因:

    • Android 是闭门开发的,代码被丢弃。因此,Google 和 OEM 开发人员已经在公共“master”之上拥有大量补丁,并且已经解决了该问题。

      出于同样的原因,尝试报告 master 上的任何构建错误可能是无用的:它们已经被报告并修复了。另外,我敢说您甚至可以找到正确的官方位置来报告构建失败。

    • 分支上的

    • reposync只是简单地拉取构成给定分支的AOSP的所有650个git存储库的最新版本,而不像子模块一样同步它们。因此,没有什么可以保证它们是兼容的。然而,标签会获取所有存储库的特定标签。

  • --depth 1sync -c --no-tags --no-clone-bundle 试图让极其缓慢的克隆变得更快。不确定它有多成功。另请参阅:AOSP 存储库同步时间太长

  • 我们使用 午餐 aosp_x86_64-eng 而不是 ARM,因为由于主机 x86 虚拟化扩展(包括 KVM.

    要构建 ARM 版本,只需使用 lunch aosp_arm-eng 即可。

    此外,ARM 映像存在错误,可能是由于速度缓慢?当 GUI 启动时(如果幸运的话),它显示“系统 UI 没有响应”。另请参阅:进程系统在 Android 模拟器中没有响应

    理论上,您在 Java / C++ 本机 API 中执行的所有“通常的高级操作”都应该在 x86 和 ARM 上同样工作,因此除非您足够硬核,可以手动触摸某些程序集,否则这应该不重要。

  • -show-kernel 将终端链接到串行,即您看到启动消息,并在最后得到一个 shell,这对于调试非常有用。

  • type emulator 显示它只是 emulator 的别名,没有任何参数。 在 Android 模拟器上运行自定义 ROM 询问如何传递一些参数来显式选择您的ROM。

    模拟器 -help 目标具有惊人的洞察力:

    模拟器-帮助
    模拟器-帮助构建图像
    模拟器-帮助-磁盘-图像
    

    您可以确定给出的确切 QEMU 命令行参数:

    模拟器 -verbose | grep '模拟器:argv'
    

    如所述:启动 android 模拟器时如何显示哪些选项传递给 QEMU?

    这显示了一些自定义选项,例如 -android-hw,因此它们必须分叉 QEMU:QEMU vs Android 模拟器:命令行选项 源显然每 5 分钟移动一次位置:修改Android模拟器源代码

Fully automated Android 8.1 guest on Ubuntu 17.10 host

# Download the source. Takes several minutes.
curl https://storage.googleapis.com/git-repo-downloads/repo >repo
chmod a+x repo
./repo init -b android-8.1.0_r1 --depth 1 -u https://android.googlesource.com/platform/manifest
./repo sync -c  -j $(($(nproc) - 2)) --no-tags --no-clone-bundle

# Do the actual build. Takes minutes / hours.
. build/envsetup.sh
lunch aosp_x86_64-eng
USE_CCACHE=1 CCACHE_DIR=ccache make -j $(($(nproc) - 2))

# Run the emulator.
emulator -show-kernel

The out/ build directory takes up about 90Gb, and the rest of the tree about 40Gb, excluding CCACHE.

About 1-2 minutes after starting the emulator, the home screen shows:

and if you press enter on the host terminal Android was launched from, you get a shell on to the Android system on your host terminal:

enter image description here

Notes:

  • ./repo init -b MUST point to a tag. master branch is always broken, and so were -release branches.

    The list of tags can be found at: https://android.googlesource.com/platform/manifest or by cloning that repo.

    There are likely two reasons why branches are always broken:

    • Android is developed behind closed doors and code dropped. Therefore Google and OEM devs already have a ton of paches on top of the public "master", and have already fixed the problem.

      For the same reason it is likely useless to try and report any build errors on master: they have already been reported and fixed. Also I dare you to even find the right official place to report build failures.

    • repo sync on a branch simply pulls whatever latest version of all 650 git repos makes up AOSP for the given branch, without syncing them like submodules. Therefore nothing guarantees that they are compatible. Tags however fetch a specific tag of all repos.

  • --depth 1 and sync -c --no-tags --no-clone-bundle were an attempt to make the painfully slow clone faster. Not sure how successful it was. See also: AOSP repo sync takes too long

  • We use lunch aosp_x86_64-eng instead of ARM because it runs much faster due to host x86 virtualization extensions including KVM.

    To build an ARM version instead, just use lunch aosp_arm-eng instead.

    Furthermore, the ARM image was buggy, possibly due to the slowness? When the GUI starts (if you are lucky), it shows "System UI isn't responding". See also: Process system isn't responding in android emulator

    All the "usual high level things" that you do in Java / C++ native APIs should just work equally on x86 and ARM in theory, so it should not matter unless you are hardcore enough to manually touch some assembly.

  • -show-kernel links the terminal to a serial, i.e. you see boot messages, and get a shell at the end, which is very useful to debug things.

  • type emulator shows that it is just an alias to the emulator without any arguments. Run custom ROM on Android Emulator asks how to pass some arguments to explicitly select your ROM.

    The emulator -help targets are surprisingly insightful:

    emulator -help
    emulator -help-build-images
    emulator -help-disk-images
    

    You can determine the exact QEMU command line arguments given with:

    emulator -verbose | grep 'emulator: argv'
    

    as mentioned at: How to show which options are passed to QEMU when launching the android emulator?

    This shows some custom options e.g. -android-hw, so they must have forked QEMU: QEMU vs Android emulator: command line options The source moves location every 5 minutes apparently: Modifying Android emulator source code

鸠书 2024-08-19 21:47:25

这是 BMB 和 Arnaud LM 答案的更新。
看来金鱼分支名称已于 2011/03/03 更改。
检查 goldfish 分支时,请使用以下命令:

git checkout -t origin/archive/android-gldfish-2.6.29 -b goldfish 

注意 android-gldfish-2.6.29 中缺少的“o”!

希望这可以为某人节省时间。

This is an update for BMB and Arnaud LM's answers.
It seems the goldfish branchnames were changed as of 2011/03/03.
When checking out the goldfish branch, use this:

git checkout -t origin/archive/android-gldfish-2.6.29 -b goldfish 

Note the missing 'o' in android-gldfish-2.6.29!

Hope this saves time for somebody.

贩梦商人 2024-08-19 21:47:25

截至 2012 年,下载内核在 source.google.com 上有详细记录,但我发现编译它需要几次尝试。以下是我用来为 ARM 模拟器构建内核的命令:

cd /kernel/source/root
make mrproper
adb pull /proc/config.gz # from the emulator
gunzip config
mv config .config  # now you have a (perhaps slightly outdated kernel .config)
make ARCH=arm silentoldconfig # update the .config - take the defaults if prompted
make ARCH=arm menuconfig # make any further changes
time make -j4 ARCH=arm CROSS_COMPILE=/path/to/android/source/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- 2>&1 | tee ../../logs/$(date +%Y%m%d-%H%M)-make-kernel.log
emulator -kernel /kernel/source/root/kernel/goldfish/arch/arm/boot/zImage -avd myAVD &

感谢所有回答这个问题的人 —— 我能够用这个答案中的一些点点滴滴来完成它。 Amaund 的“you need the AVD”是最后一首给我带来麻烦的曲子。

As of 2012, downloading the kernel is well documented on source.google.com, however I found compiling it took a few tries. Here are the commands I used to build a kernel for the ARM emulator:

cd /kernel/source/root
make mrproper
adb pull /proc/config.gz # from the emulator
gunzip config
mv config .config  # now you have a (perhaps slightly outdated kernel .config)
make ARCH=arm silentoldconfig # update the .config - take the defaults if prompted
make ARCH=arm menuconfig # make any further changes
time make -j4 ARCH=arm CROSS_COMPILE=/path/to/android/source/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- 2>&1 | tee ../../logs/$(date +%Y%m%d-%H%M)-make-kernel.log
emulator -kernel /kernel/source/root/kernel/goldfish/arch/arm/boot/zImage -avd myAVD &

Thanks to all who answered this one -- I was able to do it with bits and pieces from this answer. Amaund's 'you need the AVD' was the last piece that gave me trouble.

夜灵血窟げ 2024-08-19 21:47:25

这很容易。按照 http://source.android.com/download 中的说明获取并编译源代码整个安卓。这需要一段时间,但并不复杂。

通过构建它,您将在 /out 目录中输出。除了 ROM 映像之外,还包括一堆工具,包括模拟器。我的模拟器位于 /out/host/linux-x86/bin/emulator。只需将名为 ANDROID_PRODUCT_OUT 的环境变量设置为 /out/target/product/generic,然后在不带任何选项的情况下运行模拟器即可运行编译后的 ROM。

That's easy. Follow the instructions at http://source.android.com/download for getting and compiling the source code for the whole android. That takes a while, but isn't that complicated.

By building that, you'll have you're output in the <android>/out directory. That includes, besides the ROM images, a bunch of tools too, including the emulator. My emulator is at <android>/out/host/linux-x86/bin/emulator. Just set an environment variable named ANDROID_PRODUCT_OUT to <android>/out/target/product/generic, and then running the emulator without any options will run your compiled ROM.

撩起发的微风 2024-08-19 21:47:25

截至 2012 年 5 月,我发现您无法使用“goldfish_defconfig”来编译内核。您需要使用 goldfish_armv7_defconfig'。这可以解释为什么 JonnyLambada 从模拟器(针对预构建内核)提取配置的方法有效,并且是必要的。

goldfish_defconfig 将内核配置为在 ARM 926 处理器上运行,但模拟器配置为作为 Coretex A8(即 ARM V7 CPU)运行。因此,如果您希望它正常工作,您需要使用较新的 defconfig(或从模拟器中提取配置)。

仅供参考。

As of May 2012, I found that you can't use the 'goldfish_defconfig' for compiling the kernel. You need to use goldfish_armv7_defconfig'. This would explain why JonnyLambada's method of extracting the config from the emulator (for the prebuilt kernel) works, and is necessary.

The goldfish_defconfig configures the kernel to run on an ARM 926 processor, but the emulator is configured to run as a Coretex A8 (which is an ARM V7 CPU). So you need to use the newer defconfig if you want it to work (or pull the config from the emulator).

Just FYI.

自由如风 2024-08-19 21:47:25

我能够启动我编译的 AOSP rom 的方法是将编译后的 system.img 复制到 ~/.android/avd/Froyo.avd/
但是,当我提取 system.img 并添加 root 版本的 su 和 busybox,然后重新制作 system.img 时,模拟器无法启动。我仍在努力弄清楚那部分:S

The way I was able to boot the AOSP rom I compiled was to copy the system.img that got compiled to ~/.android/avd/Froyo.avd/
But, when I extract the system.img and add the rooted version of su and busybox, then remake the system.img, the emulator does not boot. I'm still trying to figure that part out :S

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文