1.添加自定义的aidl文件:
/frameworks/base/core/java/android/os/ISelfManager.aidl
package android.os;
interface ISelfManager {
int selfAddNumber(int numberFirst, int numberSecond);
String selfAddString(String originalStr);
}
2.更新Android.mk文件和Android.bp文件
这两个文件在frameworks/base/目录下。如果该目录下没有Android.bp只更新Android.mk就可以。 /frameworks/base/Android.mk里的LOCAL_SRC_FILES中系统添加了很多aidl文件,我们需要添加自己定义的ISelfManager.aidl文件。
在LOCAL_SRC_FILES += 后面添加 core/java/android/os/ISelfManager.aidl
LOCAL_SRC_FILES += \
core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \
core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl \
.....
core/java/android/os/ISelfManager.aidl \
在Android.bp文件里,添加”core/java/android/os/ISelfManager.aidl”,
java_library {
name: "framework",
srcs: [ ......
"core/java/android/os/ISelfManager.aidl",
......
]
}
3.编译/frameworks/base模块,转到android目录下,然后输入命令:
./mk MSM_19021 remake_user frameworks/base allnet
编译成功后会生成对应的java文件,其中包含抽象类stub接口。
4.添加自定义服务文件SelfManagerService.java
/frameworks/base/services/core/java/com/android/server/SelfManagerService.java
package com.android.server;
import android.util.Log;
import android.os.ISelfManager;
public class SelfManagerService extends ISelfManager.Stub {
private static final String TAG = "SelfManagerService";
public int selfAddNumber(int numberFirst, int numberSecond) {
Log.d(TAG,"I WILL CALCULATE IT numberFirst is:"+numberFirst+" numberSecond is :"+numberSecond);
return numberFirst + numberSecond;
}
public String selfAddString(String originalStr) {
Log.d(TAG,"I will add a string to originalStr previous....");
return "previous lalala "+originalStr;
}
}
5.创建对应的管理类SelfManager.java
/frameworks/base/core/java/android/app/SelfManager.java
package android.app;
import android.util.Log;
import android.os.ISelfManager;
import android.content.Context;
import android.os.RemoteException;
public class SelfManager {
private static String TAG = "SelfManager";
ISelfManager mSelfManager;
public SelfManager(Context ctx,ISelfManager selfManager) {
mSelfManager = selfManager;
}
public int selfAddNumber(int numberFirst, int numberSecond) throws RemoteException {
Log.d(TAG,"SelfManager selfAddNumber ......... ");
return mSelfManager.selfAddNumber(numberFirst,numberSecond);
}
public String selfAddString(String originalStr) throws RemoteException {
Log.d(TAG,"SelfManager selfAddString .........");
return mSelfManager.selfAddString(originalStr);
}
}
6.注册SelfManagerService
系统Service的注册流程,需要分别在SystemServer.java和SystemServiceRegistry.java中修改
/frameworks/base/services/java/com/android/server/SystemServer.java里添加服务
private void startOtherServices() {
....
try{
ServiceManager.addService("selfservice", new SelfManagerService());
}catch(Throwable e){
Slog.e(TAG, “ServiceManager.addService failed...”);
}
....
}
/frameworks/base/core/java/android/app/SystemServiceRegistry.java
import android.os.ISelfManager;
static { ...
registerService("selfservice", SelfManager.class, new CachedServiceFetcher()
{ @Override
public SelfManager createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService("selfservice");
ISelfManager service = ISelfManager.Stub.asInterface(b);
return new SelfManager(ctx,service);
}});
...
}
这里的”selfservice”,一般来说需要在context.java中声明,然后在这里引用,比如系统中的其他服务调用时候的应用,都是在Context.java中定义
7.在/frameworks/base/core/java/android/content/Context.java中添加service定义:
public static final String SELF_SERVICE = “selfservice” ;
8.更新权限
这里需要更新的权限文件有7个(不同厂商不同),全部在/vendor/oppo_sepolicy/private/目录下,添加如下代码:
Service.te
type self_service, service_manager_type;
System_server.te
allow system_server self_service:service_manager {find add};
Service_contexts
selfservice u:object_r:self_service:s0
Untrusted_app.te
allow untrusted_app self_service:service_manager find;
Untrusted_app_25.te
allow untrusted_app_25 self_service:service_manager find;
Untrusted_app_27.te
allow untrusted_app_27 self_service:service_manager { find};
Platform_app.te
allow platform_app self_service:service_manager find;
9.全编译
进入Android源码目录下,执行命令全编译。不同厂商全编译命令不同,这里我的全编译命令如下
bash -x compile.sh MSM_XXXXX new_user allnet
10.刷机
全编成功后刷机
11.刷机后查看自定义服务是否添加
可以通过adb shell 查看系统服务列表,若找到selfservice则添加成功。或捕获开机log,查看日志文件,查找自定义服务。
adb shell service list
然后在APP里面调用添加的服务。可以在源码中编译APK,然后adb install到手机里,也可以在Androdi Studio里面编译,需要引用jar包,添加服务管理类来调用服务。这里我选择在源码里编译,adb install安装apk
12.把AndroidStudio工程项目移植到Android源码
1 .把自己写的apk整个包复制到vendor/oppo_app/下,或者packages/apps/下。项目文件里有好多AS自动生成的文件,这里需要删除掉一些文件,只保留部分文件,否则可能会编译不通过报错,目录结构如下
APK_Demo1
java
com
example
apk_demo1
A_Activity.java
...
...
res
layout
...
...
Android.mk
AndroidManifest.xml
其余文件全部删除掉。Android.mk文件代码如下:
LOCAL_PATH := $(call my-dir)Include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := APK_Demo1
LOCAL_SDK_VERSION := current
LOCAL_CERTIFICATE := platform
LOCAL_STATIC_ANDROID_LIBRARIES := \
androidx.legacy_legacy-support-v4
LOCAL_DEX_PREOPT := falseinclude
$(BUILD_PACKAGE)
2.局部编译,生成apk
./mk MSM_19021 remake_user vendor/oppo_app/APK_Demo1 allnet
3.把APK adb install到手机
确认手机连上后,在cmd命令行窗口输入:
adb install -r [apk路径]
回车执行,提示success,安装成功,打开手机上的APK,查看服务是否成功调用。这里我是在主活动里添加了一个Button和一个Textview来测试服务是否成功调用,点击Button后,会调用系统服务获取一个整型数字,然后把这个数字显示到Textview。这里我进去app后,点击button后,Textview成功显示数字,说明服务调用成功。