When I install my app from Android Studio it works great but when I install it with adb install <pc path to apk> or adb shell pm install -r <device path to apk> the app fails to write a file to the device filesystem...
Does anyone know why? what's the difference? does installing with adb not give the app the permissions it wants?
Thanks!
EDIT:
try {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = doc.createElement("map");
doc.appendChild(root);
buildCamCapabilitiesXML(cm.getCameraCharacteristics(cameraID), doc, root);
writeXMLToFile(doc);
renameFileAndDeleteOldOne(storagePath + fileName, new File(storagePath + fileName + FILE_TEMP_EXTENSION));
} catch (CameraAccessException e) {
e.printStackTrace();
Log.e(TAG, "Failed to access camera to get camera abilities");
} catch (ParserConfigurationException e) {
e.printStackTrace();
Log.e(TAG, "Failed to get a DocumentBuilder");
} catch (TransformerException e) {
e.printStackTrace();
Log.e(TAG, "Failed to write capabilities xml file to filesystem");
}
private void writeXMLToFile(Document doc) throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(new File(storagePath + fileName + FILE_TEMP_EXTENSION)));
}
Solved it.
Apparently the app had to be started once from the device after being installed by adb (which is done automatically in Android Studio).
After that it works perfectly, have no idea why though...
Related
I can’t figure out how to flush the Logcat on my Application. Code below
Saving Logcat into File: (this works)
File File_logcat = new File(Environment.getExternalStorageDirectory(),
"log.txt");
try {
Runtime.getRuntime().exec(
"logcat -f " + File_logcat.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
After that save I flush the Logcat with: (this doesn't work)
try {
Runtime.getRuntime().exec(
"logcat --clear"); //Also tested -c or -b all –c
} catch (IOException e) {
e.printStackTrace();
}
I’ve searched for a solution on developer.android.com which says “logcat -b all –c” (or even –c) would flush it but none of them succeed.
Did I miss something?
Thanks #CommonsWare.
I just discovered that both code snippets work on a rooted mobile device. So it seems like you can't flush it without permission.
My device has been rooted and now i want to run an .sh file from my android application. I tried with following code but it did't provide the intended output:
Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
If i run .sh file from adb it is working fine for me.
Try following code.
try{
Process root = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(root.getOutputStream());
os.writeBytes("sh /system/bin/xyz.sh \n");
os.flush();
}
catch (IOException e) {
e.printStackTrace();
}
catch (SecurityException se){
se.printStackTrace();
}
This snippet worked for me,I hope this may help you.
Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
Scanner stdout = new Scanner(process.getInputStream());
while (stdout.hasNextLine()) {
Log.i("stdout", stdout.nextLine());
}
stdout.close();
Scanner stderr = new Scanner(process.getErrorStream());
while (stderr.hasNextLine()) {
Log.e("stderr", stderr.nextLine());
}
stderr.close();
In my application I'm doing:
try {
String[] cmd = {"su", "-c", "\"ls /data/\""}; //to debug, will be cp /src /dest
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
Log.e("copy", is.toString());
Log.e("copy", convertStreamToString(is));
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
The app is installed in /system/app/ and running with root permissions.
I see SuperSu's overlay that it's granted permissions for the operation.
With the cp /src /dest in place of ls command above, it doesn't copy, so debugging with ls, I get:
tmp-mksh: ls /data: not found
Why is this, and how can I fix it?
NB: This is the same issue as this question, except that was resolved by adding external write permissions - I should note that both paths in my command are in /data/...
Please help me to find out solution for this, I Know there are so many questions and duplicates about this same but here i describe whole things which i tried.
I have one android device where its installed 4.0 version of android.
I want to shutdown this device using my one demo application.
1) Demo application is signed by platform keys which are used in built in file system.
2) Device is already rooted as its development board and i have all permissions on this.
Application contains Following things
1) Application is system application
2) Application signed by platform keys which are used in built in file system.
For make automation easier, I did import the key/cert pair into my java keystore file, with the this keytool-importkeypair and use eclipse for signing.
Used command is mentioned below.
Commad : keytool-importkeypair -k ~/Desktop/myown.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform
I used following code for reboot but i never make success in this .I read So many questions and answers on stackoverflow but they all said you require
1) root access of device
2) signed apk with any one keys which are available on `build/target/product/security/`
3) Given Proper permission in AndroidManifest.xml file.
Am i right in alomg points?
Application code :
First Try
public static void shutdown2() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
String command = "/system/bin/reboot -p";
try { // Run Script
proc = runtime.exec("/system/xbin/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();
}
if (proc.exitValue() != 0) {
}
}
Second Try :
private void shutdown3() {
try {
String[] cmd = { "/system/bin/sh","su","reboot -p"};
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
} catch (Exception ex) {
Log.i("TAG", "Could not reboot 3 ", ex);
}
}
3rd Try :
private void shutdown() {
try {
Process proc = Runtime.getRuntime().exec(
new String[] { "/system/bin/su", "-c",
"/system/bin/reboot -p" });
proc.waitFor();
} catch (Exception ex) {
Log.i("TAG", "Could not reboot 1 ", ex);
}
}
In 3rd method I also tried with "/system/bin/su"
The below code worked for me
Process process = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "busybox poweroff -f"});
process.waitFor();
A much better solution is to run:
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
You can use a Process for this.
I want to make my app become system app programmatically. I managed to do it in my phone with root and busybox. any idea how achieve this without busybox?
Runtime.getRuntime().exec(new String[] { "su", "-c", "mount -o rw,remount -t yaffs2 /system; " +
"cp `ls /data/app/xxx*` /system/app; " +
"rm /data/app/xxx*; " +
"mount -o ro,remount -t yaffs2 /system; " +
"reboot" });
Beside this, I also faced another issue. If i switch back my app from system app > user app and reboot. Android system still recognize my app as system app even though the app already reside in /data/app.
I use code below to check whether my app is system app:
android.content.pm.ApplicationInfo.FLAG_SYSTEM
Refer the below code to move user app apk into system app apk in rooting device with the help of RootTools method .
PackageInfo paramPackageInfo = null;
try {
paramPackageInfo = this.getPackageManager().getPackageInfo(
this.getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
ApplicationInfo localApplicationInfo = paramPackageInfo.applicationInfo;
String str1 = "/system/app/" + localApplicationInfo.packageName
+ ".apk";
String str2 = "busybox mv " + localApplicationInfo.sourceDir + " "
+ str1;
RootTools.remount("/system", "rw");
RootTools.remount("/mnt", "rw");
CommandCapture command = new CommandCapture(0, str2,
"busybox chmod 644 " + str1);
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
RootTools.remount("/system", "ro");
RootTools.remount("/mnt", "ro");
Necessary of Busybox and superuser app while use the above code in your application.