Android Automotive Application Development and Analysis (1) - Overview and Compilation of Android Automotive

1. A new track for Android developers

When the smartphone industry first emerged, many traditional Internet companies, including BAT, had deployed the mobile phone industry, but with the basic shape of the mobile phone market, 造车it seems that it has become the next direction for major capital to pursue. Baidu and Xiaomi successively announced 造车that Alibaba and SAIC Motor Co., Ltd. jointly invested and founded to provide intelligent vehicle operating systems and overall solutions for intelligent connected vehicles for the entire automotive industry. 斑马网络It 造车has become the next outlet in the capital market for a while.

As the overlord of the mobile operating system - Android , it has also invaded the field of car manufacturing with a new attitude. This is the Android car infotainment system - Android Automotive.

image.png

2. What is Android Automotive?

Android Automotive is a basic Android platform vehicle infotainment system, referred to as IVI(In-Vehicle Infotainment).

The Android Automotive system gives car manufacturers IVIthe ability to pre-install Android applications in the system, and a large number of Android development practitioners also reduce IVIsystem and application development costs.

2.1 Android Automotive 和 Android

  • Android Automotive is the Android platform . Android Automotive is not a fork or side-by-side development version of Android. It uses the same codebase and resides in the same repository as Android on devices such as phones and tablets. Built on a robust platform and feature set developed over 10 years, it leverages existing security models, compatibility programs, developer tools, and infrastructure while continuing to maintain high levels of customizability and portability , completely free and open source.

  • Android Automotive extends the Android platform . In making Android a full-fledged infotainment platform, we've added support for car-specific requirements, features, and technologies. Android Automotive will be a one-stop full-stack in-vehicle infotainment platform, just like Android is for mobile devices today.

2.2 Android Automotive 和 Android Auto

  • Android Auto is a platform that runs on the user's mobile phone and projects the Android Auto user experience to a compatible in-vehicle infotainment system via a USB connection. Android Auto supports apps designed for in-vehicle use. For more information, visit developer.android.com/auto .

  • Android Automotive is an operating system and platform that runs directly on vehicle hardware. It is a highly customizable full-stack open source platform that provides powerful technical support for the infotainment experience. Android Automotive supports apps built for Android, as well as apps built for Android Auto.

2.3 Overview of Architecture Design of Android Automotive

Android Automotive作为车载信息娱乐系统必须具备查看、控制整车其它子系统(如 空调)的能力,但是不同的制造商提供的总线类型和协议之间有很大差异,例如控制器局域网 (CAN) 总线、区域互连网路 (LIN) 总线、面向媒体的系统传输 (MOST) 总线以及汽车级以太网和 TCP/IP 网络(如 BroadR-Reach)。

Android Automotive 的硬件抽象层 (HAL) 为 Android 框架提供了一致的接口(无需考虑物理传输层),系统集成商可以将特定功能的平台 HAL 接口(如 空调)与特定于技术的网络接口(如 CAN 总线)连接,以实现车载 HAL 模块。

image.png

  • Car API:内有包含 CarSensorManager 在内的 API。如需详细了解受支持的 API,请参阅/platform/packages/services/Car/car-lib

  • CarService:位于 /platform/packages/services/Car/

  • 车载 HAL:用于定义 OEM 可以实现的车辆属性的接口。包含属性元数据(例如,车辆属性是否为 int 以及允许使用哪些更改模式)。位于 hardware/libhardware/include/hardware/vehicle.h。如需了解基本参考实现,请参阅 hardware/libhardware/modules/vehicle/

作为车载应用开发者,对于Android Automotive 的架构,有个基础认知即可并不影响我们后续对车载应用开发的学习。

3. 创建Android Automtive模拟器

为了让便于我们对Android Automotive有一个直观上的认知,我们可以先在Android Studio上创建一个模拟器。下面的Android Automtive模拟器创建步骤基于MAC OS版Android Studio Arctic Fox

  • 1.在Preferences(Windows下是Settings) -> Appearance&Behavior -> System Settings ->Updates 中将检查更新的channel改为Canary Channel

image.png

  • 2.在创建模拟器的时候选择一个你需要的 Android Automotive 镜像

image.png

  • 3.最后,我们就可以使用Android Automotive的模拟器了

image.png

模拟器到此为止就创建完毕了,可以随便把玩一波,看看google是如何理解车载娱乐系统的。

不得不说的是,在国内实际的车载应用开发中,我们很少会把应用直接跑在模拟器上,其中一个原因就是AS创建的Android Automotive模拟器是production版本,我们并不能获取root、remount权限,这非常不利于我们的调试。

这里额外提一句,通过Android Studio创建的手机模拟器,无需任何操作就可以获取root权限。然后还可以通过控制台在Android/sdk/emulator目录下,运行下面的指令来开放remount权限

emulator -writable-system -netdelay none -netspeed full -avd 模拟器的名字
复制代码

为了在模拟器中获取root、remount权限,以及方便我们之后研究Android Automotive上原生应用的原理,这里我们接着来介绍一下如何下载 Android Automotive 源码,以及如何编译源码。

4. 下载&编译 Android Automotive

由于众所周知的原因国内下载AOSP速度非常缓慢,所以以下步骤使用清华大学的AOSP镜像。下载以及编译环境推荐使用Ubuntu系统,编译Android 9及以上的AOSP,硬盘需要预留500GB以上的空间,内存也至少需要8GB以上。以下内容基于如下环境编写。

image.png

1. 下载repo工具

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
复制代码

2. 下载初始化包

从清华大学开源镜像站下载初始化包。由于首次同步需要下载约 130GB 数据,过程中任何网络故障都可能造成同步失败,强烈建议直接使用初始化包进行初始化。使用方法如下:

wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包,可以用下载工具代替
tar xf aosp-latest.tar #解压初始化包
cd aosp   # 解压得到的 AOSP 工程目录
# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
复制代码

此后,每次只需运行 repo sync 即可保持与主分支同步。当然我们也可以选择我们指定的Android版本,继续如下的操作

cd .repo/manifests
git branch -a # 查看Android分支
复制代码

image.png

repo init -b android-11.0.0.0_r40 # 切换到Android 11
repo sync # 再同步一遍即可得到基于Android 11的完整目录
复制代码

3. 准备编译环境

在Ubuntu的控制台中执行下列指令来安装编译AOSP所必需各类型工具

sudo apt-get update
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk
sudo apt-get install 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 libgl1-mesa-dev libxml2-utils xsltproc unzip
sudo apt-get install -y lib32stdc++6 
sudo apt-get install git
sudo apt-get install libssl-dev
sudo apt-get install libncurses5
复制代码

4. 开始编译

  • 1.在aosp根目录的控制台中执行下列指令,初始化脚本
source build/envsetup.sh
复制代码
  • 2.使用lunch选择编译的目标类型。因为是在电脑上调试编译出的版本,所以这里我们选择 aosp_car_x86_64-userdebug或aosp_car_x86-userdebug。
lunch # 打开选择菜单
11 # 选择 aosp_car_x86_64-userdebug
复制代码

image.png

  • 3.使用make -jX编译源码。电脑的CPU核心数越多,X可以设定的值越大,编译速度也就越快,一般可以直接设为cpu核心数,如果你的CPU支持超线程还可以再乘以2。
make -j8 # 开始编译
复制代码

image.png

编译时间取决于你电脑的性能,在机械硬盘下首次编译约耗时5-7个小时。控制台中提示Successful,即表示编译成功。

  • 4.启动模拟器
emulator -partition-size 1500 
复制代码

漫长的开机动画之后,模拟器顺利启动。可以看出我们自行编译的模拟器,launcher 界面以及预装的APP与Android Studio中提供的 Android Automotive 还是有很大区别的。在之后的时间里面,我们就来一一解析的这些系统应用的运行原理。

image.png

image.png

5.常见错误

1.各类编译环境报错

一般环境报错,百度一下基本上都解决。在这里强烈建议在 Ubuntu 16 或以上的Linux环境下编译Android的源码!我个人尝试过在 Mac OS 和Windows OS下编译Android源码,各种错误层出不穷,而换到 Ubuntu 环境下这些错误几乎就都没有了。

2. This user doesn't have permissions to use KVM

解决方案,在控制台执行以下指令

sudo chown 用户名 -R /dev/kvm 
复制代码
3. warning: repo is not tracking a remote branch, so it will not receive updates. repo reset: error: Entry 'xxxxx.py' not uptodate. Cannot merge.fatal: Could not reset index file to revision 'v2.15.4^0'

solution:

cd .repo
cd repo
ls 
复制代码

Confirm on the console whether the xxx.py that reported the error is in this file or not. If it is not, you need to go to another file to see it. Generally, the xxx.py that reports the error is in the directory.

git log # 找到倒数第二个conmmit-id 
复制代码

image.png

image.png

git reset --hard 5637afcc60fdbd38fc0790ea84d5dcb901ec5959
git pull ## 重新拉取
复制代码

After the synchronization is complete, execute repo sync. That's it

5. References

Automotive | Android Open Source Project | Android Open Source Project (google.cn)

AOSP | Mirror Site Use Help | Tsinghua University Open Source Software Mirror Site | Tsinghua Open Source Mirror

Guess you like

Origin juejin.im/post/7083135984518299656