How to make my app become system app? - android

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.

Related

Caused by: java.io.IOException: Cannot run program "g++": error=13, Permission denied

Cannot solve this
I have tried everything I could find
try {
process = Runtime.getRuntime().exec("chmod -777 YourAndroidStudioFolder"+
"chmod +x /User/Library/Android/sdk/build-tools/23.0.1/aapt\\n\" +\n" +
" \"chmod +x /User/Library/Android/sdk/build-tools/23.0.1/dx\\n\" +\n" +
" \"chmod +x /User/Library/Android/sdk/build-tools/23.0.1/zipalign"+"cat config.cpp");
dataOutputStream = new DataOutputStream(process.getOutputStream());
//dataOutputStream.writeBytes("g++ config.cpp -o a.out");
//dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
Log.v("SUccess", "works");
} catch (Exception e) {
Log.v("Error:",e.toString());
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
process.destroy();
} catch (Exception e) {
}
}
This code works but
process = Runtime.getRuntime().exec("g++ config.cpp -o a.out");
This does not work keeps getting error13.
Try to check which g++ you are actually trying to use and check permissions.
ls -l `which g++`

What's the difference between Android Studio install and "adb install"

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...

How to Shutdown Android 4.0 rooted Device programatically

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.

RE :How can we execute a shell script file from my Android Application

I am trying to write an android application that runs a shell commands, or a shell script if that is preferable, and displays the output... can anyone give me in the right direction?
My code is as follows:
void execCommandLine()
{
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
try
{
String[] str={"/system/bin/sh","/data/shTest.sh"};
System.out.println("EXEC STRING");
proc = runtime.exec(str);
osw = new OutputStreamWriter(proc.getOutputStream());
//osw.write(command);
osw.flush();
osw.close();
}
catch (IOException ex)
{
Log.e("erre","ioexception");
//Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
return;
}
finally
{
if (osw != null)
{
try
{
osw.close();
}
catch (IOException e){}
}
}
try
{
proc.waitFor();
}
catch (InterruptedException e){}
if (proc.exitValue() != 0)
{
Log.e("erre","interruotexception");
//Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue());
}
}
// **************************************
Code is running successfully but I am not getting any output in adb shell logcat
would anyone tell me if this script is executed successfully how to get this output
in Adb shell.
Have you looked into GScript. It is quite flexible.

Android: App not working on some rooted devices... Why?

I have an app on the market that is for rooted devices only. I have tested the app extensively on a rooted and unrooted G1, MT3G and Cliq with no errors. I am receiving a number of low ratings from people with supposedly rooted devices, saying that the app tells them that they are not rooted (of course, they usually don't leave important info like what phone and what rom)
Here is the code that generates the error... can anyone see out what the problem might be?
final Button button = (Button) findViewById(R.id.******);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String command1 = "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system";
String command2 = "cp -f /sdcard/******* /etc/";
String command3 = "dos2unix -u /etc/*****";
String command4 = "mount -o ro,remount -t yaffs2 /dev/block/mtdblock3 /system";
execCommandLine1(command1);
execCommandLine1(command2);
execCommandLine1(command3);
execCommandLine1(command4);
}
void execCommandLine1(String command)
{
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
try
{
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
}
catch (IOException ex)
{
Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
return;
}
finally
{
if (osw != null)
{
try
{
osw.close();
}
catch (IOException e){}
}
}
try
{
proc.waitFor();
}
catch (InterruptedException e){}
if (proc.exitValue() != 0)
{
**// Error Dialog that is being erroneously displayed**
}
else {
// Success Dialog
}
}
I agree with Christopher's comment: you appear to be making some assumptions:
/system is at /dev/block/mtdblock3
/dev/block/mtdblock3 is yaffs2
/etc/ is a hardlink or symlink to something on /system
mount exists
dos2unix exists
cp exists
su exists
Most of those should be testable at runtime, though the /etc/ check might be a bit tricky. Test that stuff out on the first run of your app, then do whatever makes sense:
an "sorry, this app won't work" if you detect a failure
disable the menu/button/whatever that leads to whatever is executing your code

Categories

Resources