Unable to read /data/data/com.packagename in android rooted device

70 views Asked by At

I am working on an app and i have to read a package name from /data/data folder in rooted device. I have tried following code but not able to read that file. Please help me. I am using google pixel 4 with android 13

           val arr = arrayOf(
            "su",
            "chmod 777 /",
            "chmod 777 " + Environment.getDataDirectory(),
            "chmod 777 " +Environment.getDataDirectory()+ "/data/",
            "chmod 777 " +Environment.getDataDirectory()+ "/data/com.packagename"
        )
        try {
            val process = Runtime.getRuntime().exec("su")
            val out = DataOutputStream(process.outputStream)
            for (str in arr) out.writeBytes(
                """
$str

""".trimIndent()
            )
            out.writeBytes("exit\n")
            out.flush()
            val proc = process.waitFor()
            Log.d("MYAPPINFOTA", proc.toString())

           // Log.d("MYAPPINFOTA", file.listFiles().size.toString())
        } catch (e: Exception) {
            e.printStackTrace()
        }

Process return 1 and Say file not exist

Thanks in advance

1

There are 1 answers

1
Hezy Ziv On

if you only need to read files avoid using cdmod 777 use cdmod 755 for read only

try the following

 val targetFolder = Environment.getDataDirectory().absolutePath + "/data/com.packagename"

try {
    val process = Runtime.getRuntime().exec("su")
    val out = DataOutputStream(process.outputStream)
    out.writeBytes("chmod -R 777 $targetFolder\n")
    out.writeBytes("exit\n")
    out.flush()
    val exitCode = process.waitFor()
    Log.d("MYAPPINFOTA", "Exit code: $exitCode")
} catch (e: Exception) {
    e.printStackTrace()
}