From 6487d651d1381183b817052758c341f339eeff44 Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 6 Sep 2023 18:57:55 -0400 Subject: [PATCH] Add startup agents check for debugging --- app/build.gradle.kts | 7 ++++++ app/proguard-rules.pro | 23 ++----------------- lib/consumer-rules.pro | 8 +++++++ .../be/mygod/librootkotlinx/AppProcess.kt | 7 ++++++ .../be/mygod/librootkotlinx/RootServer.kt | 6 ++++- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 8d44d12..a916050 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -21,6 +21,13 @@ android { targetCompatibility = javaVersion } kotlinOptions.jvmTarget = javaVersion.toString() + buildTypes { + release { + isShrinkResources = true + isMinifyEnabled = true + proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro") + } + } } dependencies { diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index ff59496..6f3ce64 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,21 +1,2 @@ -# Add project specific ProGuard rules here. -# You can control the set of applied configuration files using the -# proguardFiles setting in build.gradle.kts. -# -# 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 \ No newline at end of file +-keepattributes SourceFile,LineNumberTable +-dontobfuscate diff --git a/lib/consumer-rules.pro b/lib/consumer-rules.pro index a4be112..ed68a3e 100644 --- a/lib/consumer-rules.pro +++ b/lib/consumer-rules.pro @@ -4,3 +4,11 @@ -keep class be.mygod.librootkotlinx.RootServer { public static void main(java.lang.String[]); } + +# Strip out debugging stuffs +-assumenosideeffects class be.mygod.librootkotlinx.AppProcess { + boolean hasStartupAgents*(android.content.Context) return false; +} +-assumenosideeffects class android.os.Debug { + public static boolean isDebuggerConnected() return false; +} diff --git a/lib/src/main/java/be/mygod/librootkotlinx/AppProcess.kt b/lib/src/main/java/be/mygod/librootkotlinx/AppProcess.kt index c26e507..57910c3 100644 --- a/lib/src/main/java/be/mygod/librootkotlinx/AppProcess.kt +++ b/lib/src/main/java/be/mygod/librootkotlinx/AppProcess.kt @@ -1,5 +1,6 @@ package be.mygod.librootkotlinx +import android.content.Context import android.os.Build import android.os.Debug import androidx.annotation.RequiresApi @@ -131,4 +132,10 @@ object AppProcess { val extraParams = if (niceName != null) " --nice-name=$niceName" else "" return "CLASSPATH=$packageCodePath exec $appProcess $debugParams /system/bin$extraParams $clazz" } + + /** + * See also: https://github.com/topjohnwu/libsu/pull/120 + */ + internal fun hasStartupAgents(context: Context) = Build.VERSION.SDK_INT >= 30 && + File(context.codeCacheDir, "startup_agents").isDirectory } diff --git a/lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt b/lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt index bdf8b8d..b230090 100644 --- a/lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt +++ b/lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt @@ -133,6 +133,8 @@ class RootServer { } private fun doInit(context: Context, shouldRelocate: Boolean, niceName: String) { try { + if (AppProcess.hasStartupAgents(context)) Logger.me.w("JVMTI agent is enabled. Please enable the " + + "'Always install with package manager' option in Android Studio.") val (reader, writer) = try { process = ProcessBuilder("su").start() val token1 = UUID.randomUUID().toString() @@ -149,7 +151,9 @@ class RootServer { try { val token2 = UUID.randomUUID().toString() writer.writeBytes(if (shouldRelocate) { - val persistence = File(context.codeCacheDir, ".librootkotlinx-uuid") + val persistence = File((if (Build.VERSION.SDK_INT >= 24) { + context.createDeviceProtectedStorageContext() + } else context).codeCacheDir, ".librootkotlinx-uuid") val uuid = context.packageName + '@' + try { persistence.readText() } catch (_: FileNotFoundException) {