Automotive 添加一个特权APP
platform: android-13.0.0_r32
一. 添加一个自定义空调的app为例
路径:packages/apps/Car/MyHvac
app内容可以自己定义,目录结构如下:
1.1 Android.bp
package {
default_applicable_licenses: ["Android-Apache-2.0"],
}
android_app {
# app的名字
name: "MyHvac",
# 源码
srcs: ["src/**/*.java"],
# 资源
resource_dirs: ["res"],
platform_apis: true,
# platform 签名
certificate: "platform",
# 是否是特权app
privileged: true,
# 依赖 car-lib
libs: ["android.car-system-stubs"],
# 这是app的常用依赖
static_libs: [
"jsr305",
"androidx.legacy_legacy-support-v4",
"androidx.appcompat_appcompat",
"androidx-constraintlayout_constraintlayout",
"androidx.lifecycle_lifecycle-extensions",
"car-apps-common",
],
optimize: {
enabled: false,
},
dex_preopt: {
enabled: false,
},
product_variables: {
pdk: {
enabled: false,
},
},
# 这里就是请求特权的模块信息了
required: ["allowed_privapp_com.jiangc.myhvac"]
}
1.2 权限申请
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jiangc.myhvac">
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="31"/>
<uses-permission android:name="android.car.permission.CONTROL_CAR_CLIMATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Required to use the TYPE_DISPLAY_OVERLAY layout param for the overlay hvac ui-->
<uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />
<!-- Allow Hvac to go across all users-->
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<protected-broadcast android:name="android.car.intent.action.TOGGLE_HVAC_CONTROLS" />
<uses-feature
android:name="android.hardware.type.automotive"
android:required="true" />
<application
android:allowBackup="true"
android:appCategory="audio"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyHvac">
<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>
</activity>
</application>
</manifest>
其中INTERACT_ACROSS_USERS、CONTROL_CAR_CLIMATE为特殊权限
二. 添加特权配置
路径:packages/services/Car/data/etc/
以包名创建一个xml文件,比如本例中的com.jiangc.myhvac.xml
内容如下:将特殊权限放到这里
<permissions>
<privapp-permissions package="com.jiangc.myhvac">
<permission name="android.permission.INTERACT_ACROSS_USERS"/>
<permission name="android.car.permission.CONTROL_CAR_CLIMATE"/>
</privapp-permissions>
</permissions>
然后在Android.bp中添加一个模块
prebuilt_etc {
# 模块名,和前面Android.bp中匹配
name: "allowed_privapp_com.jiangc.myhvac",
sub_dir: "permissions",
# 这里是我们上面添加的特权文件名
src: "com.jiangc.myhvac.xml",
filename_from_src: true,
}
接下来修改packages/services/Car/car_product/overlay/frameworks/base/core/res/res/values/policy_exempt_apps.xml
添加应用的包名
<resources>
<!--
A collection of apps that are critical for the device and hence will never be disabled by
device policies or APIs.
-->
<string-array translatable="false" name="policy_exempt_apps">
<item>com.android.car.cluster.home</item>
<item>com.android.car.hvac</item>
<item>com.android.car.media</item>
<item>com.android.car.radio</item>
<item>com.jiangc.myhvac</item>
</string-array>
</resources>
然后make