Skip to content

Commit

Permalink
Add startup agents check for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Sep 6, 2023
1 parent d46667e commit 6487d65
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 2 additions & 21 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
-keepattributes SourceFile,LineNumberTable
-dontobfuscate
8 changes: 8 additions & 0 deletions lib/consumer-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions lib/src/main/java/be/mygod/librootkotlinx/AppProcess.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package be.mygod.librootkotlinx

import android.content.Context
import android.os.Build
import android.os.Debug
import androidx.annotation.RequiresApi
Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion lib/src/main/java/be/mygod/librootkotlinx/RootServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 6487d65

Please sign in to comment.