环境
基于rk3566 android 11 系统
步骤
添加aidl文件
frameworks/base/core/java/android/os/IMyTestService.aidl
package android.os;
/**
@hide
*/
interface IMyTestService {
void test();
}
添加service
frameworks/base/services/core/java/com/android/server/MyTestService.java
package com.android.server;
import android.util.Log;
import android.os.IMyTestService;
public class MyTestService extends IMyTestService.Stub {
private static final String TAG = "MyTestService";
public MyTestService() {}
@Override
public void test(){
Log.i(TAG,"MyTestService test ...");
}
}
将自定义的service添加到系统中
首先在Context中添加常量
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
[url=home.php?mod=space&uid=1999721]@@[/url] -3494,6 +3494,7 @@ public abstract class Context {
//@hide: TIME_ZONE_DETECTOR_SERVICE,
PERMISSION_SERVICE,
LIGHTS_SERVICE,
MY_TEST_SERVICE,
})
@Retention(RetentionPolicy.SOURCE)
public [url=home.php?mod=space&uid=2697273]@interface[/url] ServiceName {}
@@ -5016,6 +5017,13 @@ public abstract class Context {
*/
public static final String TIME_ZONE_RULES_MANAGER_SERVICE = "timezone";
在 frameworks/base/services/java/com/android/server/SystemServer.java文件中的startOtherServices方法中加上我们自己的服务
//zhuw add code start
-
try {
-
ServiceManager.addService(Context.MY_TEST_SERVICE, new MyTestService());
-
} catch (Throwable e) {
-
Slog.e(TAG, "Failure starting MyTest Service", e);
-
}
-
//zhuw add code end
添加manager
frameworks/base/core/java/android/app/MyTestManager.java
package android.app;
import android.annotation.NonNull;
import android.content.Context;
import android.os.IMyTestService;
import android.os.RemoteException;
public class MyTestManager {
private IMyTestService mService;
public MyTestManager(@NonNull Context mContext, @NonNull IMyTestService service) {
mService = service;
}
public void test() {
try {
mService.test();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
将manager注册到系统
frameworks/base/core/java/android/app/SystemServiceRegistry.java
diff --git a/frameworks/base/core/java/android/app/SystemServiceRegistry.java b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
index e599a5c..243790d 100644
--- a/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -43,6 +43,8 @@ import android.app.usage.IUsageStatsManager;
import android.app.usage.NetworkStatsManager;
import android.app.usage.StorageStatsManager;
import android.app.usage.UsageStatsManager;
+import android.os.IMyTestService;//add code
+import android.app.MyTestManager;//add code
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothManager;
import android.companion.CompanionDeviceManager;
@@ -1334,6 +1336,18 @@ public final class SystemServiceRegistry {
return new DreamManager(ctx);
}});
-
//custom service zhuw add begin
-
registerService(Context.MY_TEST_SERVICE, MyTestManager.class,
-
new CachedServiceFetcher<MyTestManager>() {
-
@Override
-
public MyTestManager createService(ContextImpl ctx)
-
throws ServiceNotFoundException {
-
IBinder b = MyTestManager.getServiceOrThrow(Context.MY_TEST_SERVICE);
-
IMyTestService service = IMyTestService.Stub.asInterface(b);
-
return new MyTestManager(ctx.getOuterContext(),service);
-
}});
-
//custom service zhuw add end
-
sInitializing = true;
try {
添加到系统中编译
在Android 9 之前 是在frameworks/base/Android.mk中添加,后续已经移到了frameworks/base/Android.bp中
像Android 10的时候只需要添加aidl文件即可
java_defaults {
name: "framework-defaults",
installable: true,
srcs: [
"core/java/**/*.java",
...省略代码...
"core/java/android/app/IMyTestService.aidl",
到了Android 11 的时候又变了,我们添加的文件直接不用管,已经被包含进去了
filegroup {
name: "framework-core-sources",
srcs: [
"core/java//*.java",
"core/java//*.aidl",
],
path: "core/java",
}
编译
build/envsetup.sh
lunch rk3566_r-userdebug //看自己平台配置
好像不需要执行 make update-api 直接使用./build.sh -KUAuo 就能编译出来
导出我们需要的jar包
在out/target/common/obj/JAVA_LIBRARIES目录下找到framework-minus-apex_intermediates目录,11之前的好像是framework_intermediates目录 现在改成了这个framework-minus-apex_intermediates
使用下面的命令生成jar
cd rk3566-11.0/out/target/common/obj/JAVA_LIBRARIES/framework-minus-apex_intermediates
jar -xvf classes.jar android/app/MyTestManager.class
jar -cvf 3566_smatek.jar android
最后导入jar包使用即可
如果报下面这个错误,则需要导入平台的系统签名
java.lang.NoSuchMethodError: No virtual method test()V in class Landroid/app/MyTestManager; or its super classes (declaration of 'android.app.MyTestManager' appears in /system/framework/framework.jar)
补充说明
framework_intermediates目录下其实有生成jar包,但方法实现为空,上面framework-minus-apex_intermediates中提取的jar 必须要系统签名才能使用.因此 可以尝试在修改下面的文件编译系统之后再从framework_intermediates中提取jar包
diff --git a/art/runtime/class_linker.cc b/art/runtime/class_linker.cc
index c39c5be..13227b8 100644
--- a/art/runtime/class_linker.cc
+++ b/art/runtime/class_linker.cc
@@ -8704,7 +8704,7 @@ ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr mirror::Class klass,
hiddenapi::ShouldDenyAccessToMember(resolved,
hiddenapi::AccessContext(class_loader, dex_cache),
hiddenapi::AccessMethod::kLinking)) {
- //resolved = nullptr;
}
if (resolved != nullptr) {
// In case of jmvti, the dex file gets verified before being registered, so first
@@ -8840,7 +8840,14 @@ ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx,
// If we had a method, or if we can find one with another lookup type,
// it's an incompatible-class-change error.
if (resolved == nullptr) {
-
//resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx)
-
if (klass->IsInterface()) {
-
resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, pointer_size)
-
} else {
-
-
// we would have found it also in the "copied" methods.
-
DCHECK(klass->FindInterfaceMethod(dex_cache.Get(), method_idx, pointer_size) == nullptr)
-
}
}
if (resolved != nullptr) {
ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer);
@@ -8882,7 +8889,7 @@ ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx,
resolved,
hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()),
hiddenapi::AccessMethod::kLinking)) {
- //resolved = nullptr;
}
return resolved;
}
@@ -8983,7 +8990,7 @@ ArtField* ClassLinker::FindResolvedField(ObjPtrmirror::Classklass,
hiddenapi::ShouldDenyAccessToMember(resolved,
hiddenapi::AccessContext(class_loader, dex_cache),
hiddenapi::AccessMethod::kLinking)) {
if (resolved != nullptr) {
@@ -9010,7 +9017,7 @@ ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr mirror::Class klass,
hiddenapi::ShouldDenyAccessToMember(resolved,
hiddenapi::AccessContext(class_loader, dex_cache),
hiddenapi::AccessMethod::kLinking)) {
原作者:时光一去不在