I am developing a root app for modifying build.prop programatically.
I copied build.prop from /system/build.prop to /sdcard/
but after modifying it I was unable to copy back to root partition
here is my code for copying build.prop from /system/build.prop to /sdcard/
protected void SUCommand()
{
String sSUCommand = "cp /system/build.prop /sdcard/";
final String[] sCommand = {"su","-c",sSUCommand};
Thread thSUProcess = new Thread()
{
public void run()
{
try
{
Process p = Runtime.getRuntime().exec(sCommand);
}
catch(Exception e){}
}
};
thSUProcess.start();
}
I changed String sSUCommand = "cp /system/build.prop /sdcard/"; to String sSUCommand = "cp /sdcard/build.prop /system/"; to copy it to system partition
but didn't worked
I already rooted my phone and running lot of root apps successfully
please tell me the right way to do it
You might want to mount system partition as rw instead of rd so as to do it
https://android.stackexchange.com/questions/25250/how-to-mount-system-in-rw-mode-if-no-custom-recovery
Related
I have a rooted Android 7 phone and I would like to dump unix input event files. Using adb I could do it using the following command:
adb shell getevent -t /dev/input/event7 > recorded_touch_events.txt
This will dump the event7 file into recorded_touch_events.txt. But this only works when the phone is connected by usb cable with the PC. Using Android I can dump files with the following code:
th = new Thread(new Runnable(){
private Process exec;
#Override
public void run() {
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c","getevent -t /dev/input/event7"});
InputStreamReader is = new InputStreamReader(
exec.getInputStream());
String s;
BufferedReader br = new BufferedReader(is);
while(((s = br.readLine()) != null) && run){
// write line to text file
}
is.close();
exec.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
In this way, I could store every read line in a text file.
Are there other approaches (probably faster ones) for directly dumping the event file?
getevent is used to print input events out in human readable form. For example during interactive debug session. You do not need to use getevent for just dumping or any other computer processing task. Just open and read the input file. The event record format is very simple.
Certainly No, you are doing it right.
I ve got a problem, executing su with parameters (containing blank space?!). My Command.java looks like this:
public class Command
{
Process process;
public String executeCommand(String command)
{
StringBuffer output = new StringBuffer();
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
}
In my MainActivity it is working for single commands.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView content = (TextView) findViewById(R.id.txt_content);
Command cmd = new Command();
String outp = cmd.executeCommand("su -c 'id'");
Log.d("OOOOOOUT", outp);
content.setText(outp);
}
The result shows, that it is working:
D/OOOOOOUT: uid=0(root) gid=0(root) context=u:r:init:s0
This is also working for the ls command. But when I try to parse an argument with paramters, it will not get executed.
E.g.
String outp = cmd.executeCommand("su -c 'ls /data/data/'");
Log.d("OOOOOOUT", outp);
content.setText(outp);
I also tryed the following:
String outp = cmd.executeCommand("su -c \'ls /data/data/\'");
String outp = cmd.executeCommand("su -c 'ls -l'");
And even more. When I execute this command within the shell directly I get the following output:
shell#hammerhead:/ $ su -c 'ls -l'
drwxr-xr-x root root 1970-06-20 18:01 acct
drwxrwx--- system cache 2016-06-21 22:06 cache
-rwxr-x--- root root 272364 1970-01-01 01:00 charger
dr-x------ root root 1970-06-20 18:01 config
I also tryed using the complete path:
shell#hammerhead:/ $ /system/xbin/su -c 'ls -l'
drwxr-xr-x root root 1970-06-20 18:01 acct
drwxrwx--- system cache 2016-06-21 22:06 cache
Also within the application. I guess its a parsing error. Sometimes I see people adding the "\n" at the end of a command? No clue why. I really appreciate any help within this topic. Thanks!
I think the way to pass arguments to the su command would be to use a string array:
Runtime.getRuntime().exec(new String[]{"su","-c", "ls /data/data/"});
I am intermittently getting Too many open files exceptions when I try to write to a text file in my Xamarin / Android app.
I understand that this is because each Android app has a limit on the number of files it can have open, and obviously my app is exceeding it in some circumstances. Hence I must be leaving some files open / not disposing of them properly somewhere.
I would like to know if I can programatically list the files that my app has open at any given time? Obviously the operating system knows this, is it possible to do this from within my app? This will help me find the problem.
Thanks to fiddler, this is how I solved it in case anyone else needs to do it in the future (in C# / Xamarin):
private static List<string> m_commandData;
private static void GetOpenedFiles()
{
m_commandData = new List<string>();
RunCommand("lsof");
RunCommand("ps | grep TestNoFilesOpen");
}
private static void RunCommand(string command)
{
string data;
using (Java.Lang.Process process = Java.Lang.Runtime.GetRuntime().Exec(command))
{
Stream stream = process.InputStream;
StreamReader bodyReader = new StreamReader(stream);
data = bodyReader.ReadToEnd();
}
m_commandData.Add(data);
}
private static void WriteCommands()
{
for (int i = 0; i < m_commandData.Count; i++)
{
using (TextWriter tw = new StreamWriter(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/NoFilesOpen/adb_" + i.ToString() + ".txt"))
{
tw.Write(m_commandData[i]);
tw.Close();
}
}
m_commandData = new List<string>();
}
You could use the Unix lsof command in the ADB shell.
1) Start the shell:
$ adb shell
2) Find the process ID of your app:
shell#android:/ $ ps | grep <packagename>
3) List files opened by your app:
shell#android:/ $ lsof <pid>
I have a rooted device running MarshMallow. I have integrated Chainfire's library Library in my code. I get the superuser prompt and granted the permission. After that I am trying to get the folders inside /data/misc/ but it returns me null. I have READ_EXTERNAL_STORAGE permission also in manifest and in App->Permissions have granted the same.
What I have tried so far ..
Checked /data/misc/ folder in ES File Explorer. It does have folders.
From cmd, ran
adb shell
su
cd /data/misc/
ls
Was able to see the folders in cmd.
Can anyone please help how to solve this?
Code :
private final static String ROOT_DIR = "/data/misc/";
#Override
protected Void doInBackground(Void... params) {
// Let's do some SU stuff
suAvailable = Shell.SU.available();
if (suAvailable) {
Log.d("TAG", "SU there");
//List<String> output = Shell.SU.run("mount -o rw,remount /");
//Log.d("TAG","Output ="+output); // empty
File file = new File(ROOT_DIR);
if (file.exists()) {
Log.d("TAG", "File exists"+ file.getAbsolutePath()+ file.isDirectory());
File file1[] = file.listFiles(); // RETURNS NULL HERE
}
}
}
I need to remove a particular folder from the /mnt/sdcard/new.
I am looking at the folder with the DDMS in Eclipse.
How do we remove a particular folder.
Thanks in advance.
You can use rm command with -r parameter to delete a nonempty folder.
C:\> adb shell
$ rm -r /mnt/sdcard/Android/data/mydirectory/
NOTE: rmdir can delete only a nonempty folder.
C:\>adb shell
$ rmdir /mnt/sdcard/Android/data/mydirectory/
Please use below method for delete folder from sdcard
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
write below code for call deleteDir() method
// Delete an empty directory
boolean success = (new File("directorypath")).delete();
if (!success) {
// Deletion failed Message
}
boolean success = (
new File("/data/data/yourpackege/New Folder")).delete();
if (!success) {
// Deletion failed Message
Toast.makeText(getApplicationContext(),"not deleted : ", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext()," deleted : ", Toast.LENGTH_LONG).show();
}
If you want to delete any folder from ddms first you have to go to adb shell through cmd
simple go to the path where your sdk\platform-tools\ is located ,there is your adb shell
to run command for deleting folders you must first root your device by simply typing
adb root
then you can delete the folder using
rmdir /mnt/sdcard/folder
to delete folder with files
rm -r /mnt/sdcard/folder
hope my answer will help anyone(beginners)