近日,在搞一套开机启动的Service,虽然在之前低版本弄过,以为直接照搬过来就可以了,结果还出了一堆问题,比如framework里边@NonNull检测、selinux新规则等等,废了本尊些许时日才完工~
且听我一一道来~~~~
首先,在Framework相关目录添加Service和aidl文件;
在frameworks/base/core/java/android/app/目录创建genie目录,新建如下两个文件:
GeniePadManager.java:
/**
* Pad manager
*/
package android.app.genie;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
import android.annotation.NonNull;
public class GeniePadManager {
Context mContext;
IGeniePadManager mService;
static final String TAG = "GeniePadManager";
public GeniePadManager(@NonNull Context context, @NonNull IGeniePadManager service) {
mContext = context;
mService = service;
if (mService != null)
Slog.d(TAG, "====== mService not null");
else
Slog.d(TAG, "====== mService not null");
}
public @NonNull String getPadBrightness() {
if (mService != null) {
try {
//Slog.d(TAG, " Will return getPadBrightness");
return mService.getPadBrightness();
} catch (RemoteException e) {
Slog.d(TAG, "RemoteException " + e);
return null;
}
}
return null;
}
public @NonNull String getTpVersion() {
if (mService != null) {
try {
Slog.d(TAG, "====== Will return getTpVersion");
return mService.getTpVersion();
} catch (RemoteException e) {
Slog.d(TAG, "RemoteException " + e);
return null;
}
}
return null;
}
}
IGeniePadManager.aidl:
/**
* pad controller
*/
package android.app.genie;
interface IGeniePadManager {
String getPadBrightness();
String getTpVersion();
}
第二步:执行一次make update-api,会遇到类似下边的报错日志:
1 new API lint issues were found. See tools/metalava/API-LINT.md for how to handle these.
/************************************************************
Your API changes are triggering API Lint warnings or errors. To make these errors go away, fix the code according to the error and/or warning messages above. it is not possible to do so, there are workarounds
1.You can suppress the errors with @SuppressLint("")
2.You can update the baseline by executing the following
command:
cp \
out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api_lint_baseline.txt \
frameworks/base/api/lint-baseline.txt
To submit the revised baseline.txt to the main Android\n repository, you will need approval.
************************************************************/
这是Android的lint检查,之前的版本没有明显语法错误是不报错的,android11规范了这种语法,会报错,
假设报错提示为:
Missing nullability on method csdn() return [MissingNullability]
则需要在方法之前添加@NonNull关键字,这是解决这个问题的方案之一;还有一个更简单的方案,就是直接忽略掉这个目录的lint检查:
修改framework/base目录下的Android.bp
diff --git a/Android.bp b/Android.bp
index 14a2bff8a..fd2c2dcb9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1219,7 +1219,8 @@ metalava_framework_docs_args = "--manifest $(location core/res/AndroidManifest.x
"--api-lint-ignore-prefix android.icu. " +
"--api-lint-ignore-prefix java. " +
"--api-lint-ignore-prefix junit. " +
- "--api-lint-ignore-prefix org. "
+ "--api-lint-ignore-prefix org. " +
+ "--api-lint-ignore-prefix android.app.genie "
build = [
"StubLibraries.bp",
第三步:解决掉编译错误,顺利的话可以make update-api成功:
api文件会自动生成如下的改动:
diff --git a/api/current.txt b/api/current.txt
index 0d8a2c56c..fee9c416e 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -7616,6 +7616,37 @@ package android.app.blob {
}
+package android.app.genie {
+
+ public class GeniePadManager { </

最低0.47元/天 解锁文章
2244





