在 android-8.1.0_r1/frameworks/base/core/java/android/os 路径下新增IGodvService.aidl
package android.os;
/**
* @hide
*/
interface IGodvService {
int gov_ioctl(int which, int status);
}
在 android-8.1.0_r1/frameworks/base 路径下修改mk文件将新建的aidl文件添加进去
接下来mma mma ANDROID_COMPILE_WITH_JACK=false
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += \
core/java/android/os/IGodvService.aidl \
...
在out目录下find一下 拷贝
godv@godv-OptiPlex-7070:~/godv/AOSP/android-8.1.0_r1/out/target$ find -name IGodvService.java
./common/obj/JAVA_LIBRARIES/framework_intermediates/core/java/android/os/IGodvService.java
cp common/obj/JAVA_LIBRARIES/framework_intermediates/core/java/android/os/IGodvService.java ~
在该文件的末尾有一个生成好了的接口
public int gov_ioctl(int which, int status) throws android.os.RemoteException;
编写服务
在 android-8.1.0_r1/frameworks/base/services/core/java/com/android/server 下编译
GodvService.java
package com.android.server;
import android.os.IGodvService;
import android.os.IBinder;
import android.os.Binder;
import android.os.ServiceManager;
import android.util.Slog;
public class GodvService extends IGodvService.Stub {
private static final String TAG = "godv";
public GodvService(){
godv_open();
Slog.d(TAG, "GodvService::godv_open");
}
public int gov_ioctl(int which, int status) throws android.os.RemoteException {
Slog.d(TAG, "GodvService::gov_ioctl");
godv_ioctl(which, status);
}
public native int godv_open();
public native int godv_ioctl(int which, int status);
}
添加服务
在frameworks/base/services/java/com/android/server/SystemServer.java里面添加服务 编译
private void startOtherServices() {
...
traceBeginAndSlog("StartGodvService");
GodvService godv = new GodvService(context);
ServiceManager.addService("godv", godv);
traceEnd();
...
}
报错信息
SELinux权限问题可以暂时使用一个命令关闭 setenforce 0
关于 SELinux权限问题解决参考https://blog.csdn.net/we1less/article/details/119282763
E ServiceManager: add_service('godv',33) uid=1000 - PERMISSION DENIED
I zygote64: option[47]=Xfingerprint:Android/aosp_x86_64/generic_x86_64:8.1.0/OPM1.171019.011/godv06271014:eng/test-keys
D godv : GodvService::godv_open
E SELinux : avc: denied { add } for service=godv pid=6508 uid=1000 scontext=u:r:system_server:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=0
服务权限解决完成后相应的服务进行就会在系统中运行起来了
generic_x86_64:/ # service list | grep "godv"
71 godv: [android.os.IGodvService]
在路径下out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
有编译好的jar包导入到as中可以进行ndk开发
public class MainActivity extends AppCompatActivity {
IGodvService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IBinder godv = ServiceManager.getService("godv");
service = IGodvService.Stub.asInterface(godv);
}
public void run(View view) {
try {
service.gov_ioctl(0,1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
运行一遍打个log
LIBRARIES/framework_intermediates$ adb logcat -s "godv"
--------- beginning of main
--------- beginning of system
--------- beginning of crash
09-06 22:03:59.721 1540 1540 D godv : GodvService::godv_open
09-06 22:14:04.435 1540 1706 D godv : GodvService::gov_ioctl