Skip to content

Commit

Permalink
fix: crash during Shizuku setup on Android 15 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Dec 12, 2024
1 parent bbd16f1 commit 5269ce0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

import com.android.internal.app.IAppOpsService;

import java.io.OutputStream;
import java.lang.reflect.Method;

import rikka.shizuku.Shizuku;
import rikka.shizuku.ShizukuBinderWrapper;
import rikka.shizuku.ShizukuRemoteProcess;
import rikka.shizuku.SystemServiceHelper;

public class ShizukuSystemServerApi {
Expand Down Expand Up @@ -45,10 +48,14 @@ protected IAudioPolicyService create() {

public static void PermissionManager_grantRuntimePermission(String packageName, String permissionName, int userId) {
try {
if (Build.VERSION.SDK_INT >= 34) {
if (Build.VERSION.SDK_INT >= 35) {
PERMISSION_MANAGER.getOrThrow().grantRuntimePermission(packageName, permissionName, "default:0", userId);
}
else if (Build.VERSION.SDK_INT == 34) {
try {
PERMISSION_MANAGER.getOrThrow().grantRuntimePermission(packageName, permissionName, 0, userId);
}catch (NoSuchMethodError e) {
}
catch (NoSuchMethodError e) {
PERMISSION_MANAGER.getOrThrow().grantRuntimePermission(packageName, permissionName, userId);
}
} else {
Expand All @@ -57,9 +64,27 @@ public static void PermissionManager_grantRuntimePermission(String packageName,
}
catch(Exception ex) {
Log.e("ShizukuSystemServerApi", "Failed to call app ops service");
exec("pm grant " + packageName + " " + permissionName);
}
}

private static synchronized void exec(String cmd) {
try {
Method newProcess = Shizuku.class.getDeclaredMethod("newProcess", String[].class, String[].class, String.class);
newProcess.setAccessible(true);
ShizukuRemoteProcess process = (ShizukuRemoteProcess) newProcess.invoke(null, new String[]{"sh"}, null, null);
assert process != null;
OutputStream outputStream = process.getOutputStream();
outputStream.write((cmd + "\nexit\n").getBytes());
outputStream.flush();
outputStream.close();
process.waitFor();
} catch (Exception e) {
Log.e("ShizukuSystemServerApi", "Failed to call cmd via exec");
}
}


public static final String APP_OPS_MODE_ALLOW = "allow";
public static final String APP_OPS_MODE_IGNORE = "ignore";
public static final String APP_OPS_MODE_DENY = "deny";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
import android.os.IInterface;
import android.os.RemoteException;

import androidx.annotation.RequiresApi;

public interface IPermissionManager extends IInterface {

void grantRuntimePermission(String packageName, String permissionName, int userId) throws RemoteException;

@RequiresApi(34)
void grantRuntimePermission(String packageName, String permissionName, int deviceId, int userId)
throws RemoteException;

@RequiresApi(35)
void grantRuntimePermission(String packageName, String permissionName, String deviceId, int userId)
throws RemoteException;

void revokeRuntimePermission(String packageName, String permissionName, int userId,
String reason) throws RemoteException;

Expand Down

0 comments on commit 5269ce0

Please sign in to comment.