欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

NanoPi K2 (Amlogic S905) Ubuntu 16.04 编译Android 5.1系统源码

程序员文章站 2022-04-07 10:01:25
NanoPi K2 (Amlogic S905) Ubuntu 16.04 编译Android 5.1系统源码 1. 下载源码 在这里下载https://www.media...
NanoPi K2 (Amlogic S905) Ubuntu 16.04 编译Android 5.1系统源码 1. 下载源码

在这里下载https://www.mediafire.com/folder/8xn2iirciznqh/S905压缩包,或者使用repo进行下载。按照https://wiki.friendlyarm.com/wiki/index.php/NanoPi_K2/zh这里下载。

2. 编译

编译的方法https://wiki.friendlyarm.com/wiki/index.php/NanoPi_K2/zh,为防止链接失效,我这里备份一下:

// #################################引用分隔线###############################################

编译Android 搭建编译环境

搭建编译Android的环境建议使用64位的Ubuntu 16.04,安装需要的包即可。

sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zip
sudo apt-get install flex libncurses5-dev zlib1g-dev gawk minicom

更多说明可查看 https://source.android.com/source/initializing.html 。

下载Android5.1源代码

Android源代码的下载需要使用repo,其安装和使用请查看 https://source.android.com/source/downloading.html 。

mkdir android && cd android
repo init -u https://github.com/friendlyarm/android_manifest.git -b nanopi-k2-lollipop
repo sync

其中“android”是指工作目录。

编译系统
source build/envsetup.sh
lunch nanopi_k2-userdebug
make -j8

编译成功完成后,目录 out/target/product/nanopi-k2 下包含可用于烧写的image文件。

filename    partition   Description
u-boot.bin  bootloader  -
boot.img    boot    -
cache.img   cache   -
userdata.img    userdata    -
system.img  system  -
partmap.txt -   分区描述文件
烧写到SD卡

如果是采用SD卡启动Android,可复制编译生成的image文件到sd-fuse_amlogic/android/ 下,使用脚本即可烧到到SD卡,具体请查看#在Linux Desktop下通过脚本制作。

使用fastboot更新

板子启动后通过串口快速按任意键进入uboot命令行模式,输入命令fastboot usb即可更新Android。
连接USB线,然后PC端输入以下命令:

cd out/target/product/nanopi-k2
sudo fastboot flash boot boot.img
sudo fastboot flash cache cache.img
sudo fastboot flash userdata userdata.img
sudo fastboot flash system system.img
sudo fastboot reboot

// #################################引用分隔线###############################################

3. 遇到的问题 1.JDK版本问题

源码里自带了自己需要使用的1.7版本,你的系统中装的不一定是1.7版本的。直接加上一个全局变量,让编译的时候使用自带的解决JDK版本的问题。

解决方案
export ANDROID_SET_JAVA_HOME=true
2. JniInvocation.cpp:165: error: unsupported reloc 43
...
libnativehelper/JniInvocation.cpp:165: error: unsupported reloc 43
libnativehelper/JniInvocation.cpp:165: error: unsupported reloc 43
libnativehelper/JniInvocation.cpp:165: error: unsupported reloc 43
libnativehelper/JniInvocation.cpp:165: error: unsupported reloc 43
...
解决方案

具体的原因是使用clang编译art时遇到点问题,使用clang而不使用gcc是想log内容好看一些,这里不使用即可。来源:https://github.com/gmacario/easy-jenkins/issues/88

export WITHOUT_HOST_CLANG=false

注:其它博客上写的修改源码的方式也是禁用clang,这里能用export解决就不修改源码。:)

3. public_api.txt:20: error 5: Added public field android.Manifest.permission.BACKUP

新源码,根本没有修改任何代码,怎么上来就需要make update-api呢?应该是别的错误引起的,最终找到了解决方案。

Checking API: checkpublicapi-current
out/target/common/obj/PACKAGING/public_api.txt:20: error 5: Added public field android.Manifest.permission.BACKUP
out/target/common/obj/PACKAGING/public_api.txt:82: error 5: Added public field android.Manifest.permission.INVOKE_CARRIER_SETUP
out/target/common/obj/PACKAGING/public_api.txt:106: error 5: Added public field android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE
out/target/common/obj/PACKAGING/public_api.txt:116: error 5: Added public field android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST

******************************
You have tried to change the API from what has been previously approved.

To make these errors go away, you have two choices:
   1) You can add "@hide" javadoc comments to the methods, etc. listed in the
      errors above.

   2) You can update current.txt by executing the following command:
         make update-api

      To submit the revised current.txt to the main Android repository,
      you will need approval.
******************************
解决方案:

根据:https://*.com/a/42654807/2193455 和 https://android.googlesource.com/platform/system/core/+/dd060f01f68ee0e633e9cae24c4e565cda2032bd%5E%21 需要修改 system/core/libutils/String8.cpp

diff --git a/libutils/String8.cpp b/libutils/String8.cpp
index 9092cbc..3323b82 100644
--- a/libutils/String8.cpp
+++ b/libutils/String8.cpp
@@ -424,7 +424,7 @@
             next = len;
         }

-        memcpy(buf + tail, buf + index + skip, next - index - skip);
+        memmove(buf + tail, buf + index + skip, next - index - skip);
         tail += next - index - skip;
         index = next;
     }
4. 我的完整编译脚本,需要的请拿去
#!/bin/bash
# author: kangear
# e-mail: kangear@163.com
# wechat: kangear
# only for android 5.1 (NanoPi-K2 Amologic S905)

# export http_proxy=127.0.0.1:
# install from wiki
sudo apt-get -y install bison g++-multilib git gperf libxml2-utils make python-networkx zip flex libncurses5-dev zlib1g-dev gawk minicom git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip

# For Jdk 1.7
# sudo add-apt-repository -y ppa:openjdk-r/ppa  
# sudo apt-get -y update   
# sudo apt-get -y install openjdk-7-jdk
export ANDROID_SET_JAVA_HOME=true

# cache
export USE_CCACHE=1

# fix: libnativehelper/JniInvocation.cpp:165: error: unsupported reloc 43
export WITHOUT_HOST_CLANG=false

# fix: public_api.txt:20: error 5: Added public field android.Manifest.permission.BACKUP
sed -i '427s/memcpy/memmove/' ./system/core/libutils/String8.cpp

# ccache size
prebuilts/misc/linux-x86/ccache/ccache -M 200G

# for nanopi_k2
source build/envsetup.sh
lunch nanopi_k2-userdebug

# make
make -j32
5. 截图

NanoPi K2 (Amlogic S905) Ubuntu 16.04 编译Android 5.1系统源码