Closed sbmatch closed 3 months ago
必须基于AIDL实现一个 IInterface,比如IUserService.aidl
随后在通过java或kotlin实现 UserService.java
因为Dhizuku的所有底层实现都是基于AIDL IBinder机制实现的,如果还是不行,请附上整个工程项目文件到此。
private void bindServiceWithRetry() {
threadPool.execute(() -> {
final int MAX_RETRY_COUNT = 100000;
int retryCount = 0;
while (retryCount < MAX_RETRY_COUNT && remoteServiceBinder == null) {
try {
Dhizuku.bindUserService(dhizukuUserServiceArgs, connection);
retryCount++;
MMKV.defaultMMKV().putInt("rebindCount", retryCount);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1029)
at java.util.ArrayList$Itr.next(ArrayList.java:982)
at com.rosan.dhizuku.api.DhizukuServiceConnection.lambda$onServiceConnected$0$com-rosan-dhizuku-api-DhizukuServiceConnection(DhizukuServiceConnection.java:19)
at com.rosan.dhizuku.api.DhizukuServiceConnection$$ExternalSyntheticLambda0.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:224)
at android.os.Looper.loop(Looper.java:318)
at android.app.ActivityThread.main(ActivityThread.java:8772)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
FATAL EXCEPTION: DefaultDispatcher-worker-4
Process: com.rosan.dhizuku, PID: 18973
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: app.uid (code 2067 SQLITE_CONSTRAINT_UNIQUE)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:961)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:89)
at F1.j.a(SourceFile:3)
at U1.b.d(SourceFile:68)
at U1.d.call(SourceFile:266)
at B1.f.o(SourceFile:6)
at o2.a.q(SourceFile:9)
at E2.F.run(SourceFile:113)
at A1.k.run(SourceFile:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1251)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:668)
at java.lang.Thread.run(Thread.java:1012)
Suppressed: J2.g: [n0{Cancelling}@6db9bd1, Dispatchers.IO]
问题已经解决, 我在我的dpm类中增加了新的构造方法用于从已激活的设备所有者应用程序中创建上下文并获取devicepolicymanager, 就像这样
public class DevicePolicyManager { private IInterface manager; private static DevicePolicyManager devicePolicyManager; private android.app.admin.DevicePolicyManager dpm; private static ComponentName adminComponent;
public DevicePolicyManager(IInterface manager){
this.manager = manager;
}
private DevicePolicyManager(String targetPackage){
android.app.admin.DevicePolicyManager dpm2 = (android.app.admin.DevicePolicyManager) ContextUtil.createPackageContext(targetPackage).getSystemService(Context.DEVICE_POLICY_SERVICE);
IInterface anInterface = (IInterface) ReflectUtil.getObjectField(dpm2, "mService");
switch (targetPackage){
case "com.rosan.dhizuku":
if (anInterface instanceof DhizukuBinderWrapper) dpm = dpm2;
ReflectUtil.setObjectField(dpm2, "mService", IDevicePolicyManager.Stub.asInterface(Dhizuku.binderWrapper(anInterface.asBinder())));
dpm = dpm2;
break;
}
}
public static DevicePolicyManager getByDeviceOwner(){
if (devicePolicyManager == null) {
devicePolicyManager = new DevicePolicyManager(getDeviceOwnerComponent().getPackageName());
adminComponent = getDeviceOwnerComponent();
}
return devicePolicyManager;
}
} ..... 省略若干代码
DhizukuUserService工厂类
异常日志