my app have root right ,i want to using code to build a dir or file at /system or other dir of device.
i know common java build file as:
File file = new File(destFileName);
if (file.exists()) {
return false;
}
if i can build dir at system like the above code?if i need use su command before build file dir as
Process p = null;
p = Runtime.getRuntime().exec("su");
edit: i have read Create a file in /system directory but i cannot using code write :
" hint: use the "adb shell" and check if the upper steps can lead to success (su && remount -o remount,rw /system && touch /system/test)"
You need to get /system be mounted for write. By default it is read-only. You can use command like mount -o remount,rw /system
You can create dir using mkdir command
You can change the owner user by chown command
After that you can use java code as ususal.
Related
so im having problem with accessing root app data directory
my example dir:
/data/app/com.android.chrome-jAB96abq4RXcFrKebL0BUQ==
i want to get into /data/app/com.android.chrome without extension name to get list folder
tried /data/app/* com.android.chrome */ but got null
and here's my code
String path = "/data/app/*com.android.chrome*";
File file = new File(path);
String[] dir = file.list();
for(int i=0;i<dir.length;i++) {
Toast.makeText(this, "File: "+dir[i], Toast.LENGTH_LONG).show();}
and before that i've done changing the permission to 777 and using * com.android.chrome * goes well but my code above return null when try to listing directory
try {
Runtime.getRuntime().exec("su -c chmod 777 /data");
Runtime.getRuntime().exec("su -c chmod 777 /data/app");
Runtime.getRuntime().exec("su -c chmod 777 /data/app/*com.android.chrome*")
} catch (IOException e) { e.printStackTrace(); }
tried without * package * and still got null
sry the stars doesnt have whitespace...got to whitespace because the texteditor recognize as text format
thanks before
Your code does not work, because java.io.File can not handle wildcards.
You have to specify the exact path.
As /data is not readable for regular users Java code will never be able to list a directory in this directory (a wildcard requires directory listing).
Get the app info via PackageManager and the dataDir from the ApplicationInfo for the app you are interested in:
context.getPackageManager().getPackageInfo("com.android.chrome", 0).applicationInfo.dataDir;
source
Alternatively you can run a shell ls command with su/root that lists the /data/data directory and parse the result for the chrome app data directory.
Story
I take photos and record videos with my phone camera and keep all of them on my internal storage/sdcard. I periodically back them up on my PC, so I keep these camera photos on PC storage in sync with phone storage.
For years, I've been backing up my phone camera photos to my PC in the following way:
Plug in phone into PC and allow access to phone data
Browse phone storage → DCIM → Camera
Wait several minutes for the system to load a list of ALL photos
Copy only several latest photos which haven't been backed up yet
I figured that waiting several minutes for all photos to load is an unnecessary drag so I downloaded adb platform tools. I've added the folder bin to my Path environment variable (i.e. %USERPROFILE%\Tools\adb-platform-tools_r28.0.3) so that I can seamlessly use adb and not write its full path each time.
The script
I wrote the following script for Git Bash for Windows. It is also compatible with Unix if you change the $userprofile variable. Essentially, the script pulls camera photos between two dates from phone storage to PC.
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
The problem
When I run the script (bash adb-pull-camera-photos.sh), everything runs smoothly except for the adb pull command in the while-loop. It gives the following error:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
I am not sure why the output is broken. Sometimes when I resize the Git Bash window some of the text goes haywire. This is the actual error text:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
I am sure that these files exist in the specified directory on the phone. When I manually execute the failing command in bash, it succeeds with the following output:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
The question
I can't figure out what's wrong with the script. I thought the Windows system might be causing a commotion, because I don't see the reason why the same code works when entered manually, but doesn't work when run in a script. How do I fix this error?
Additional info
Note that I had to use // in the beginning of an absolute path on Windows because Git Bash would interpret / as its own root directory (C:\Program Files\Git).
I've echoed all variables inside the script and got all the correct paths that otherwise work via manual method.
camera-ls.txt file contents
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
Additional questions
Is it possible to navigate to external sdcard without using its name? I had to use /storage/06CB-C9CE/ because /sdcard/ navigates to internal storage.
Why does tr "\\" "/" give me this error: tr: warning: an unescaped backslash at end of string is not portable?
Windows batch script
Here's a .bat script that can be run by Windows Command Prompt or Windows PowerShell. No Git Bash required.
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
Just edit the srcFolder to point to your phone camera folder,
plug a pattern into the dateRegex for matching the date interval and
save it as a file with .bat extension, i.e: adb-pull-camera-photos.bat.
Double-click the file and it will pull filtered photos into CameraPhotos folder on Desktop.
Keep in mind that you still need have adb for Windows on your PC.
The problem was with Windows line delimiters.
Easy fix
Just add the IFS=$'\r\n' above the loop so that the read command knows the actual line delimiter.
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Explanation
I tried plugging the whole while-loop into the console and it failed with the same error:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
This time I started investigating why the output was broken. I remembered that windows uses \r\n as newline, which means Carriage Return + Line Feed, (CR+LF), so some text must have been overwritten.
It was because of broken values stored inside the $filename variable.
This is the loop from the script:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Since each iteration of the while-loop reads a line from $lsFile in the following form:
exampleFilename.jpg\r\n
It misinterprets the newline symbols as part of the file name, so adb pull tries to read files with these whitespaces in their names, but fails and it additionally writes a broken output.
Adb Photo Sync
This might not be the answer but might be useful for others looking for android photo/files backup solution.
I use this script on my Windows with git bash. This can be easily used for Linux. A common issue with a long backup process is that it might get interrupted and you might have to restart the entire copy process from start.
This script saves you from this trouble. You can restart the script or interrupt in between but it will resume copy operation from the point it left.
Just change the rfolder => android folder, lfolder => local folder
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files
There is a file named bootanimation.zip in /system/media:
1853 -rw-r--r-- 1 root root 2.1M 2018-07-10 17:29 bootanimation.zip
I want to make it writeable.
So I do this in init.rc:
on fs
+ mount ext4 /dev/block/platform/mstar_mci.0/by-name/system /system wait rw
+ chmod 0777 /system/media/bootanimation.zip
mount_all /fstab.m7221
But it is useless.The bootanimation.zip still is -rw-r--r--.
How can I make /system/media/bootanimation.zip rw in init.rc ?
Can someone tell me how to debug init.rc , I can not see any log!
I have the following problem.
I need to lock my tablet for a specific app. I am using my application in KioskMode, however I need to block some buttons, "Switch_app", "Volume_UP", "Volume_DOWN", etc.
I was able to block these buttons by accessing the ES File Explorer and changing the file manually, saving and restarting the tablet.
However, I would like to change this file progammatically.
I've tried the following:
{
using (StreamReader sr = new StreamReader("/system/usr/keylayout/Generic.kl"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("VOLUME"))
{
line = $"# {line}";
}
text += line + "\n";
System.Console.WriteLine(text);
}
}
CreateFile();
TransferFile();
};
void CreateFile()
{
string sdCard = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
string path = Path.Combine(sdCard, "MyFolder/Generic.kl");
// This text is added only once to the file.
if (!System.IO.File.Exists(path))
{
// Create a file to write to.
System.IO.File.WriteAllText(path, text);
}
}
And to transfer the created file to / system / usr / Keylayout I use this:
Java.Lang.Runtime.GetRuntime().Exec("su -c mount -o rw,remount,rw /system");
Java.Lang.Runtime.GetRuntime().Exec("su -c rm system/usr/keylayout/Generic.kl");
Java.Lang.Runtime.GetRuntime().Exec("su -c mv /storage/emulated/0/MyFolder/Generic.kl system/usr/keylayout/Generic.kl");
When I use these commands, the file is copied, but when I restart the tablet no more physical buttons work. So I believe it's some problem related to deleting the old file and adding the new one.
Any help will be very welcome as well as ideas.
Thank you all.
There is no need to worry about ownership, permissions, etc. if you would use sed's in place editing:
GetRuntime().Exec("su 0 mount -o remount,rw /system");
GetRuntime().Exec("su 0 sed -i 's/^[^#]*VOLUME_DOWN/# &/' /system/usr/keylayout/Generic.kl");
GetRuntime().Exec("su 0 sed -i 's/^[^#]*VOLUME_UP/# &/' /system/usr/keylayout/Generic.kl");
GetRuntime().Exec("su 0 sed -i 's/^[^#]*APP_SWITCH/# &/' /system/usr/keylayout/Generic.kl");
You still need to reboot though.
That's the solution Rooted devices
Java.Lang.Runtime.GetRuntime().Exec("su -c mount -o rw,remount,rw /system");
Java.Lang.Runtime.GetRuntime().Exec("su -c rm system/usr/keylayout/Generic.kl");
Java.Lang.Runtime.GetRuntime().Exec("su -c mv /storage/emulated/0/MyFolder/Generic.kl system/usr/keylayout/");
**Java.Lang.Runtime.GetRuntime().Exec("su -c chmod 644 /system/usr/keylayout/Generic.kl");
Java.Lang.Runtime.GetRuntime().Exec("su -c chown system.system /system/usr/keylayout/Generic.kl");**
I am doing a small application for a private circulation so that I do not use google play to install and update the app.
In my MainActivity, I will check whether app updation found or not via an api. If any update found my app download the updated apk file into download folder on the SDCard and install by:
intent.setDataAndType(Uri.fromFile(new File(destination)), "application/vnd.android.package-archive");
it works fine, but my problem is users can get the apk file in the download folder. So that I decided to download the file into /data/data/com.xxx.aaa. This idea, download the file into the /data... path. But I could not be installed the apk file. I make sure the apk file is present into the download folder by the following code:
if(new File(destination).exists()){
// apk file is present into download folder.
}
my method showing Parsing Error as below:
My Questions is:
How can I get install the updated apk file from the /data.... path?
I guess root privilege is the problem to install apk.
please help me.
Edited question:-
To get root access my application I run the following command:
try {
Runtime.getRuntime().exec("su");
}
after executing this command I can list /data/data/com.xxx.aaa path. and I have verified my downloaded apk file is presented here.
To install the apk, as told in the comment I run mount command in various option but no luck. I get Parsing error as shown in the figure.
I have tried the mount in the following ways:
1) mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
2) mount -o rw,remount -t yaffs2 /data/data.com.xxx.aaa/appinfo.apk
3) mount
4) mount /system
I do not know the right way to mount /data/data/.... path. How to mount the /data/data/.... path.
please help me.
To List Files From /data/app/
[1] su
[2] ls /data/app/
To get apk from "/data/app/" folder
# ls /data/app/
i am getting following apk list
com.android.vending-1.apk
com.google.android.gms-1.apk
com.noshufou.android.su-1.apk
com.corusen.accupedo.te-1.zip
com.microsoft.office.lync-1.apk
com.okythoos.android.tdmpro-1.apk
com.devindia.acr-1.apk
for example i want to get "com.android.vending-1.apk" out to /sdcard
[1] su
[2] cat /data/app/com.android.vending-1.apk > /sdcard/com.android.vending-1.apk
[3] adb pull /sdcard/com.android.vending-1.apk /path-to-your-folder/
To Run application as system app
[1] adb push your-app.apk /sdcard/
[2] adb shell
[3] su
[4] mount -o remount,rw /system
[5] cat /sdcard/your-app.apk > /system/app/your-app.apk
[6] chmod 0644 /system/app/your-app.apk
Run Command As Root User, From Your Android Code :
for example if you want to run command "chmod 0644 /system/app/your-app.apk" form root user
Example Steps :
[1] Execute From Computer
adb push your-app.apk /sdcard/
[2] Execute From Android Code
Exec_SU("mount -o remount,rw /system");
Exec_SU("cat /sdcard/your-app.apk > /system/app/your-app.apk");
Exec_SU("chmod 0644 /system/app/your-app.apk");
Here is implementation of above function "Exec_SU" :
public static void Exec_SU(String str_command)
{
// working well
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
StringBuilder sbstdOut = new StringBuilder();
sbstdErr = new StringBuilder();
String command=str_command;
try {
// Run Script
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (proc != null)
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
i think this will helpful to you. Thanks.