Android11.0Frameworks中添加一个自定义系统服务,并提供jar包供应用开发调用

编程入门 行业动态 更新时间:2024-10-26 12:23:18

Android11.0Frameworks中添加一个<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义系统服务,并提供jar包供应用开发调用"/>

Android11.0Frameworks中添加一个自定义系统服务,并提供jar包供应用开发调用

Android11.0Frameworks中添加一个自定义系统服务,并提供jar包供应用开发调用

  • Android11.0Frameworks中添加一个系统服务
    • 添加自定义服务
    • 封装自定义服务,提供jar供第三方应用开发者调用

Android11.0Frameworks中添加一个系统服务

本文描述Android11中在frameworks中添加一个新的服务, 并封装后提供jar包,供应用开发调用。

添加自定义服务

注意事项:
a.Android11中frameworks编译使用 make framework-minus-apex
b.在framework/base/core目录下添加文件java和aidl文件后,编译时需要先make update-api去更新current.txt文件,然后才能完整编译android,但Android 11 以后谷歌强制开启lint检查,lint检查不过编译会报错,可以让lint检查忽略掉自己的模块在framework/base下的Android.bp忽略掉代码检查,或者可以添加@hide 避免报错:

metalava_framework_docs_args = "
"--api-lint-ignore-prefix com.yjz.test. " //其中 com.yjz.test是包名的前缀。

c.Android11如果编译报如下错误,可以在/build/make/core/tasks/check_boot_jars/package_allowed_list.txt中添加白名单

Error: out/target/common/obj/JAVA_LIBRARIES/framework-minus-apex_intermediates/classes.jar contains class file com/yjz/test/api/ITestManager.class, whose package name com.yjz.test.api is empty or not in the allow list build/make/core/tasks/check_boot_jars/package_allowed_list.txt of packages allowed on the bootclasspath.
###################################################
# framework.jar
javax\.microedition\.khronos\.opengles
javax\.microedition\.khronos\.egl
com\.yjz\.test            //添加
com\.yjz\.test\..*        //添加
  1. 定义服务名称,修改路径:/frameworks/base/core/java/android/content/Context.java;
//这里添加YJZ_TEST_SERVICE@StringDef(suffix = { "_SERVICE" }, value = {YJZ_TEST_SERVICE, }/*** Use with {@link #getSystemService(String)} to retrieve a* {@link com.yjz.test.TestManager} for yjz_testcontrol.** @hide* @see #getSystemService(String)* @see com.yjz.test.TestManager*/public static final String YJZ_TEST_SERVICE = "yjz_test";
  1. 添加ITestManager.aidl及TestManager.java

frameworks/base/core/com/yjz/test/api/ITestManager.aidl


package com.yjz.test.api;/**
* @hide
*/
interface ITestManager {boolean test();
}

frameworks/base/core/com/yjz/test/TestManager.java

/*** @hide*/
@SystemService(Context.YJZ_TEST_SERVICE)
public class TestManager{private final Context mContext;private final ITestManager mService;public TestManager(Context context, ITestManager manager) {this.mContext = context;this.mService = manager;}public boolean test() {try {return mService.test();} catch (RemoteException e) {e.printStackTrace();}return false;}
}
  1. 添加TestManagerService /frameworks/base/services/core/java/com/android/server/yjz/TestManagerService.java;

package com.android.server.yjz;import android.annotation.NonNull;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.StaticIpConfiguration;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import com.android.server.SystemService;
import com.yjz.test.api.ITestManager;public class TestManagerService extends ITestManager.Stub {private static final String TAG = "TestManagerService";private final Context mContext;public TestManagerService(Context context) {mContext = context;}@Overridepublic boolean test() throws RemoteException {return true;}public void onStart() {}public void systemReady() {Log.d(TAG, "systemReady--->" + Thread.currentThread().getName());}public static final class Lifecycle extends SystemService {private TestManagerService mServer;public Lifecycle(Context context) {super(context);}@Overridepublic void onStart() {mServer = new TestManagerService(getContext());publishBinderService(Context.YJZ_TEST_SERVICE, mServer);mServer.onStart();}@Overridepublic void onBootPhase(int phase) {if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {mServer.systemReady();}}}
}
  1. 添加TestManagerService服务到ServiceManager /frameworks/base/services/java/com/android/server/SystemServer.java;

//********省略代码******import com.android.server.yjz.TestManagerService;private void startOtherServices(@NonNull TimingsTraceAndSlog t){//********省略代码******t.traceBegin("StartTestManagerService ");mSystemServiceManager.startService(TestManagerService.Lifecycle.class);t.traceEnd();t.traceBegin("StartTwilightService");//********省略代码******
}//********省略代码******
  1. 注册服务 /frameworks/base/core/java/android/app/SystemServiceRegistry.java;
//********省略代码******
import com.yjz.test.TestManager;
import com.yjz.test.api.ITestManager;static {
//********省略代码******registerService(Context.YJZ_TEST_SERVICE, TestManager.class,new CachedServiceFetcher<TestManager>() {@Overridepublic TestManager createService(ContextImpl ctx)throws ServiceNotFoundException {IBinder b = ServiceManager.getServiceOrThrow(Context.YJZ_TEST_SERVICE);return new TestManager(ctx, ITestManager.Stub.asInterface(b));}}); //********省略代码******
}
  1. 添加SELinux权限: system/sepolicy/private/service_contexts
    system/sepolicy/prebuilts/api/30.0/private/service_contexts
    在上面两个文件中添加以下
yjz_test                                  u:object_r:yjz_test_service:s0

/system/sepolicy/public/service.te
/system/sepolicy/prebuilts/api/30.0/public/service.te
在上面两个文件中添加以下

type yjz_test_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;

system/sepolicy/private/compat/29.0/29.0.ignore.cil
system/sepolicy/private/compat/27.0/27.0.ignore.cil
system/sepolicy/private/compat/28.0/28.0.ignore.cil
system/sepolicy/private/compat/26.0/26.0.ignore.cil
system/sepolicy/prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil
system/sepolicy/prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil
system/sepolicy/prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil
system/sepolicy/prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil
在上面的文件中添加以下

(typeattributeset new_objects( new_objects//********省略代码******hypervisor_propyjz_test_service //添加服务名称))
  1. 添加白名单:
    //build/make/core/tasks/check_boot_jars/package_allowed_list.txt
###################################################
# framework.jar
javax\.microedition\.khronos\.opengles
javax\.microedition\.khronos\.egl
com\.yjz\.test          //新添加
com\.yjz\.test\..*       //新添加
  1. 添加隐藏方法允许应用层调用名单
    /frameworks/base/config/hiddenapi-greylist-packages.txt
com.yjz.test
  1. 编译刷机验证,adb shell service list

封装自定义服务,提供jar供第三方应用开发者调用

  1. 在vendor目录下添加yjz目录:

    /vendor/yjz/Android.bp
java_library_static {name: "yjz-sdk-api",srcs: ["com/yjz/**/*.java"],
}

/vendor/yjz/com/yjz/sdk/SDKManager.java

package com.yjz.sdk;import android.content.Context;
import com.yjz.test.TestManager;public class SDKManager {private static final String TAG = "SDKManager";private final Context mContext;private TestManager mTestManager;public SDKManager(Context context) {mContext = context.getApplicationContext();mTestManager = (TestManager) context.getSystemService(Context.YJZ_TEST_SERVICE);}public boolean test() {return mTestManager.test();}}
  1. 编译 mmm vendor/yjz/ 生成yjz-sdk-api.jar
  2. 应用开发时导入yjz-sdk-api.jar,并调用方法
   SDKManager manger = new SDKManager(this);manger.test();

更多推荐

Android11.0Frameworks中添加一个自定义系统服务,并提供jar包供应用开发调用

本文发布于:2024-02-11 14:13:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1681412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   系统   Frameworks   jar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!