1. 如何新建一个系统 App 项目
使用 Android Studio 新建一个空项目 FirstSystemApp,包名设置为 com.yuandaima.firstsystemapp
,语言选择 Java。后面为叙述方便称该项目为 as 项目。
接着在 jelly/rice14
目录下创建如下的目录和文件:
接着将 as 项目中的 res 文件下的资源文件拷贝到 Jelly/Rice14/FirstSystemApp/res
中,把 as 项目中的 MainActivity.java 拷贝到 Jelly/Rice14/FirstSystemApp/src/com/yuandaima/firstsystemapp
中。
接着修改已添加的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yuandaima.firstsystemapp">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.FirstSystemApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
接着修改已添加的 Android.bp 文件:
android_app {
name: "FirstSystemApp",
srcs: ["src/**/*.java"],
resource_dirs: ["res"],
manifest: "AndroidManifest.xml",
platform_apis: true,
sdk_version: "",
certificate: "platform",
product_specific: true,
//依赖
static_libs: ["androidx.appcompat_appcompat",
"com.google.android.material_material",
"androidx-constraintlayout_constraintlayout"],
}
至此我们的系统 App 就创建好了。
接着在我们的 Product 中添加这个App,修改 device/Jelly/Rice14/Rice14.mk
:
# 添加以下内容
PRODUCT_PACKAGES += FirstSystemApp
接着编译系统,启动虚拟机,打开 app:
source build/envsetup.sh
lunch Rice14-eng
make -j16
emulator
2. 系统 App 与 普通 App 的差异
2.1 系统 App 可以使用更多的 api
当我们在 Android.bp 中配置了:
platform_apis: true,
sdk_version: "",
当 platform_apis 为 true 时,sdk_version 必须为空。这种情况下我们的 app 会使用平台 API 进行编译而不是 SDK,这样我们的 App 就能访问到非 SDK API 了。关于 SDK API 和非 SDK API 的内容可以参考官方文档
2.2 系统 App 的签名
AOSP 内置了 apk 签名文件,我们可以在 Android.bp 中通过 certificate 配置系统 app 的签名文件,certificate 的值主要有一下几个选项: