Android ADB无需root即可访问应用程序数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2078710/
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 04:26:13  来源:igfitidea点击:

Android ADB access to application databases without root

androidadbroot

提问by Scoobler

Can anyone tell me, is it possible to use the ADB to pull and push a database from an app, without root privileges on the phone?

谁能告诉我,是否可以使用 ADB 从应用程序中拉取和推送数据库,而无需手机上的 root 权限?

For example, I know the location on my rooted magic and dream is:

例如,我知道我扎根魔法和梦想的位置是:

/data/data/com.xxxx.xxxx/databases/xxxx

I know that you can use ADB without root, but when trying to use the shell - you can't view that location without root privaliges. But I have been told you can use push and pull if you know the file you want?

我知道您可以在没有 root 的情况下使用 ADB,但是在尝试使用 shell 时 - 您无法在没有 root 权限的情况下查看该位置。但有人告诉我,如果你知道你想要的文件,你可以使用 push 和 pull 吗?

Basically I want to pull a database from MY app on a non rooted phone modify it and push it back on.

基本上我想从我的应用程序中提取一个非 root 手机上的数据库修改它并将其推回。

Only trouble I have is, the two phones I have are both root and I don't have access to a non root one to try it out.

我唯一遇到的麻烦是,我拥有的两部手机都是 root 用户,我无法访问非 root 用户进行试用。

回答by Pilot_51

While Nilhcem's answer didn't work for me, it lead me in the right direction (for that I upvoted) and I now have a working solution.

虽然 Nilhcem 的回答对我不起作用,但它引导我走向正确的方向(为此我投了赞成票),我现在有了一个可行的解决方案。

Old answer that may not work with newer versions of Android:

可能不适用于较新版本的 Android 的旧答案:

#Transfer file from app databases directory to PC
adb shell
$ run-as package.name
$ cd ./databases/
$ ls -l #Find the current permissions - r=4, w=2, x=1
$ chmod 666 ./dbname.db
$ exit
$ exit
adb pull /data/data/package.name/databases/dbname.db ~/Desktop/

#Transfer file from PC to app databases directory (requires the above permission change)
adb push ~/Desktop/dbname.db /data/data/package.name/databases/dbname.db
adb shell
$ run-as package.name
$ chmod 660 ./databases/dbname.db #Restore original permissions
$ exit
$ exit

Alternate method using external storage (confirmed to work with 6.0.1):

使用外部存储的替代方法(已确认适用于 6.0.1):

#Transfer file from app databases directory to external storage
adb shell
$ run-as package.name
$ cp ./databases/dbname.db /sdcard/
$ exit
$ exit

#Transfer file from external storage to app databases directory
adb shell
$ run-as package.name
$ cp /sdcard/dbname.db ./databases/
$ exit
$ exit

回答by Nilhcem

A quick workaround is to use the run-ascommand to copy the database in a folder where you can have access, such as /sdcardand then, do a normal adb pull

一个快速的解决方法是使用该run-as命令将数据库复制到您可以访问的文件夹中,例如/sdcard,然后执行正常操作adb pull

adb shell
$ run-as package.name cp /data/data/package.name/dbname.db /sdcard/
$ exit
adb pull /sdcard/dbname.db

More information on the run-as command here

有关 run-as 命令的更多信息,请点击此处

Note that the run-ascommand is available since API level 8 (Android 2.2) and can only be used if the application is debbugable.

请注意,该run-as命令自 API 级别 8 (Android 2.2) 起可用,并且只能在应用程序可调试时使用

回答by FredMaggiowski

On OxygenOS(based on Android 5.2) I've mixed up the two solutions provided by Pilot_51.

OxygenOS(基于 Android 5.2)上,我将Pilot_51.

First I've used run-asto gain access on /data/data/package.name/databasesfrom here I wasn't able to copy directly on /sdcard/hence I changed the permission of the file. After that I've exited from run-asmode and used cpto copy the file in /sdcard/?storage. Eventually I was able to use adb pull

首先,我曾经从这里run-as获得访问权限,/data/data/package.name/databases但无法直接复制,/sdcard/因此我更改了文件的权限。之后,我退出了run-as模式并用于cp将文件复制到/sdcard/?storage 中。最终我能够使用adb pull

$ adb -s <DEVICE_ID> shell
$ run-as package.name
$ chmod 666 databases/dbname.db
$ exit
$ cp /data/data/package.name/databases/dbname.db /sdcard/dbname.db
$ exit
$ adb pull /sdcard/dbname.db ./dbname.db

回答by AhuraMazda

if you want to push db file into the application

如果要将 db 文件推送到应用程序中

first of all, place "file.db" under "/storage/emulated/0/" because of permission issue. then you should pretend as application to access data folder.

首先,由于权限问题,将“file.db”放在“/storage/emulated/0/”下。那么你应该假装成应用程序来访问数据文件夹。

  adb shell
  $ run-as com.package.name
  :/data/data/com.package.name $ cp /storage/emulated/0/file.db /data/data/com.package.name/databases/

it copies the file.db that in main folder to databases.

它将主文件夹中的 file.db 复制到数据库。

回答by delrocco

We set the file permissions to readable for all users from within the app.

我们将文件权限设置为应用内所有用户都可以读取。

if (BuildConfig.DEBUG)
{
    new File(mDB.getPath()).setReadable(true, false);
}

Then just pull the .db off with adb normally.

然后只需正常使用 adb 将 .db 拉下来。

adb -d pull //data/data/xxxxx/databases/xxxxx.db .

NOTE:I've discovered that this needs to be done each time the database file is opened, for example in onCreate as well as the constructor of your SQLiteOpenHelper wrapper (when your database is not null) or perhaps onOpen. If only in onCreate, then the next time you run your app and the .db already exists, for some reason the permissions have been changed back. This might have something to do with how Android manages its data.

注意:我发现每次打开数据库文件时都需要这样做,例如在 onCreate 以及 SQLiteOpenHelper 包装器的构造函数(当您的数据库不为空时)或 onOpen。如果仅在 onCreate 中,那么下次运行您的应用程序并且 .db 已经存在时,出于某种原因,权限已被改回。这可能与 Android 管理其数据的方式有关。