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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1809774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:39:17  来源:igfitidea点击:

How to compile the Android AOSP kernel and test it with the Android Emulator?

androidandroid-emulatoremulationandroid-source

提问by Zyris Development Team

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?

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

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.

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

回答by BMB

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:

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

  • 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
  • 通过将 Android 内核添加到存储库清单或手动运行以下命令来获取 Android 内核:
    git clone https://android.googlesource.com/kernel/goldfish.git
  • 检查使用模拟器的正确分支,即金鱼:
    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

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/kernelfolder 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.

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

回答by Arnaud LM

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

只是为了更正 BMB 帖子中的一些内容(这对我非常有用,它保存了我的项目):

  • git clone git://android.git.kernel.org/kernel/common.git (the kernelmissed) ;
  • 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 avdin the command, it didn't work without for me).
  • 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 (idem) ;
  • make ARCH=arm CROSS_COMPILE=mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-(同上)
  • emulator -avd my_avd -kernel mydroid/kernel/common/arch/arm/boot/zImage(这里我在命令中添加了一个avd,没有我它就无法工作)。



回答by hopia

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:

这是 BMB 和 Arnaud LM 答案的更新。
从 2011 年 3 月 3 日起,金鱼分支名称似乎已更改。检查金鱼分支时,请使用:

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

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

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

Hope this saves time for somebody.

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

回答by JohnnyLambada

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:

截至 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 &

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.

感谢所有回答这个问题的人——我能够用这个答案的点点滴滴来做到这一点。Amaund 的“你需要 AVD”是最后一个给我带来麻烦的作品。

回答by Mostafa

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

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

By building that, you'll have you're output in the <android>/outdirectory. 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_OUTto <android>/out/target/product/generic, and then running the emulator without any options will run your compiled ROM.

通过构建它,您将在<android>/out目录中输出。除了 ROM 映像之外,这还包括一堆工具,包括模拟器。我的模拟器在<android>/out/host/linux-x86/bin/emulator. 刚刚成立名为环境变量ANDROID_PRODUCT_OUT<android>/out/target/product/generic,然后运行没有任何选项将运行ROM编译模拟器。

回答by Tim Bird

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.

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

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).

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

Just FYI.

仅供参考。

回答by triki

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

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