Skip to content

Commit

Permalink
Merge pull request #1243 from keymapperorg/fix/1234-granting-permissi…
Browse files Browse the repository at this point in the history
…ons-with-shizuku-crashes-on-android-14

Fix/1234 granting permissions with shizuku crashes on android 14
  • Loading branch information
sds100 authored Jul 7, 2024
2 parents 4621b52 + 7089dfc commit 2070f62
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 22 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ dependencies {
def epoxy_version = "4.6.2"
def splitties_version = "3.0.0-beta01"
def multidex_version = "2.0.1"
def shizuku_version = '12.1.0'
def shizuku_version = '13.1.5'

implementation fileTree(include: ['*.jar'], dir: 'libs')

Expand Down Expand Up @@ -160,6 +160,7 @@ dependencies {
// androidx
implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
implementation 'androidx.core:core-ktx:1.13.1'
compileOnly project(':systemstubs')

implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/aidl/android/permission/IPermissionManager.aidl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class AndroidPermissionAdapter(
const val REQUEST_CODE_SHIZUKU_PERMISSION = 1
}

private val iPackageManager: IPackageManager by lazy {
private val shizukuPackageManager: IPackageManager by lazy {
val binder = ShizukuBinderWrapper(SystemServiceHelper.getSystemService("package"))
IPackageManager.Stub.asInterface(binder)
}

private val iPermissionManager: IPermissionManager by lazy {
private val shizukuPermissionManager: IPermissionManager by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
HiddenApiBypass.addHiddenApiExemptions(
"Landroid/permission",
Expand Down Expand Up @@ -151,19 +151,50 @@ class AndroidPermissionAdapter(
private fun grantPermissionWithShizuku(permissionName: String) {
val userId = Process.myUserHandle()!!.getIdentifier()

// In Android 12 this method was moved from IPackageManager to IPermissionManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
iPermissionManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
userId,
)
} else {
iPackageManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
userId,
)
try {
// In revisions of Android 14 the method to grant permissions changed
// so try them all.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
try {
shizukuPermissionManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
ctx.deviceId,
userId,
)
} catch (_: NoSuchMethodError) {
try {
shizukuPermissionManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
"0",
userId,
)
} catch (_: NoSuchMethodError) {
shizukuPermissionManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
userId,
)
}
}
// In Android 11 this method was moved from IPackageManager to IPermissionManager.
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
shizukuPermissionManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
userId,
)
} else {
shizukuPackageManager.grantRuntimePermission(
Constants.PACKAGE_NAME,
permissionName,
userId,
)
}
// The API may change in future Android versions so don't crash the whole app
// just for this shizuku permission feature.
} catch (_: NoSuchMethodError) {
}
}

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include ':app'
include ':systemstubs'
1 change: 1 addition & 0 deletions systemstubs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions systemstubs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "io.github.sds100.keymapper.systemstubs"
compileSdk = 34

defaultConfig {
minSdk = 21

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
}
Empty file added systemstubs/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions systemstubs/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package android.permission;

import android.os.IBinder;

public interface IPermissionManager extends android.os.IInterface {
void grantRuntimePermission(String packageName, String permissionName, int userId);

void grantRuntimePermission(String packageName, String permissionName, int deviceId, int userId);

void grantRuntimePermission(String packageName, String permissionName, String persistentDeviceId, int userId);

abstract class Stub extends android.os.Binder implements IPermissionManager {
public static IPermissionManager asInterface(IBinder obj) {
throw new RuntimeException("Stub!");
}
}
}

0 comments on commit 2070f62

Please sign in to comment.