Android Jelly Bean (api 16) 的 READ_LOGS 权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11461650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:44:17  来源:igfitidea点击:

READ_LOGS permission on Jelly Bean (api 16)

androidloggingrootandroid-4.2-jelly-bean

提问by android developer

Since Android Jelly Bean doesn't support the logs reading permission(according to this google io 2012 videoand this one too) , i would like to know if it's possible for rooted devices (or non-rooted devices) to be able to bypass this restriction and be able to read the logs.

由于 Android Jelly Bean 不支持日志读取权限(根据这个 google io 2012 视频这个也是),我想知道是否有可能让有根设备(或非根设备)能够绕过这个限制并能够读取日志。

How do i do that? Do i really need to make the app a system app, or is rooting enough?

我怎么做?我真的需要使应用程序成为系统应用程序,还是足够扎根?

回答by ge0rg

You can obtain the permission on a rooted device by executing the pm grant command from your app. Probably you will have to restart the app after that for the change to take effect, though:

您可以通过从您的应用程序执行 pm grant 命令来获取对 root 设备的权限。不过,您可能必须在此之后重新启动应用程序才能使更改生效:

String pname = getPackageName();
String[] CMDLINE_GRANTPERMS = { "su", "-c", null };
if (getPackageManager().checkPermission(android.Manifest.permission.READ_LOGS, pname) != 0) {
    Log.d(TAG, "we do not have the READ_LOGS permission!");
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        Log.d(TAG, "Working around JellyBeans 'feature'...");
        try {
            // format the commandline parameter
            CMDLINE_GRANTPERMS[2] = String.format("pm grant %s android.permission.READ_LOGS", pname);
            java.lang.Process p = Runtime.getRuntime().exec(CMDLINE_GRANTPERMS);
            int res = p.waitFor();
            Log.d(TAG, "exec returned: " + res);
            if (res != 0)
                throw new Exception("failed to become root");
        } catch (Exception e) {
            Log.d(TAG, "exec(): " + e);
            Toast.makeText(context, "Failed to obtain READ_LOGS permission", Toast.LENGTH_LONG).show();
        }
    }
} else
    Log.d(TAG, "we have the READ_LOGS permission already!");

This code should be called from your onCreate(). Once the permission is granted, no more root powers are required.

应该从您的 onCreate() 调用此代码。授予权限后,不再需要根权限。

P.S: The p.waitFor() blocks on the Superuser app, delaying your app start and potentially cause an ANR.

PS:超级用户应用程序上的 p.waitFor() 阻塞,延迟您的应用程序启动并可能导致 ANR。

回答by inazaruk



EDIT: It turns out that adb shell pm grantonly works on emulators. Production devices do not allow shellto grant and revoke optional permissions.

编辑:事实证明,adb shell pm grant它只适用于模拟器。生产设备不允许shell授予和撤销可选权限。



You don't need to have root. The only thing you need to enable READ_LOGS to non-system applications is to have Android SDK installed.

你不需要root。为非系统应用程序启用 READ_LOGS 唯一需要的是安装 Android SDK。

Basically, you need to call:

基本上,您需要调用:

adb shell pm grant <pkg.name> android.permission.READ_LOGS

Where <pkg.name>is your application's package name. This is possible because READ_LOGS is not only a "system" permission, but also a "development" permission (this term is introduced in JB). Which actually allows users to grant this permission to any application. No root required.

<pkg.name>您的应用程序的包名称在哪里。这是可能的,因为 READ_LOGS 不仅是“系统”权限,还是“开发”权限(这个术语在 JB 中引入)。这实际上允许用户将此权限授予任何应用程序。无需root。

More information is available in this google groups threadwith comments from Dianne Hackborn.

更多信息可在此google 群组线程中获得,并附有 Dianne Hackborn 的评论。

回答by bara

I've got around this on a rooted device by calling logcat with suand reading from stdout

我通过调用 logcatsu并从 stdout 读取,在有根设备上解决了这个问题

... .getRuntime().exec("su -c logcat -d");

回答by Anton Tananaev

There is another way to access all logs without the need for root permissions. You can use remote debugging. Take a look at the open source rootless Logcat app.

还有另一种无需root权限即可访问所有日志的方法。您可以使用远程调试。看看开源的无根 Logcat 应用程序