I have successfully build Gingerbread 2.3.4 for beagleboard xM rev C. Now i want to check Linux kernel version name inside a script which runs after the init.rc. In linux we can find the same using the command uname -r. But it is not found in android kernel. can somebody help me with some sample script to do the same.
There is a version file in the /proc directory. Try cat /proc/version in a shell and it should display informations about your kernel.
If your phone is rooted and BusyBox is installed then uname -r should work.
You can get kernel version using
adb shell cat /proc/version
or
with the help of
System.getProperty("os.version");
This worked for me
public static String getKernelVersion() {
try {
Process p = Runtime.getRuntime().exec("uname -a");
InputStream is = null;
if (p.waitFor() == 0) {
is = p.getInputStream();
} else {
is = p.getErrorStream();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
Log.i("Kernel Version", line);
br.close();
return line;
} catch (Exception ex) {
return "ERROR: " + ex.getMessage();
}
}
Take a look at the AOSP DeviceInfoSettings:
https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/DeviceInfoSettings.java#L378
'uname -r' command also should work
Related
I am developing one android app where i need to use ping command with interface name .Ping command without interface is working fine but if i specify the interface name ping does not work.Ping with interface name works in linux terminal without root permission.
I am trying below command
pingCommand = "ping -I wlan0 192.168.230.2"; //Does not work
proc = runtime.exec(pingCommand);
pingCommand = "ping 192.168.230.2"; //This does work
proc = runtime.exec(pingCommand);
Please share some solutions for this problem.
I did some experiment regarding ping using interface in Android and i have below conclusions.
1) ROOT permission is required to do Ping using specific interface (Ex: wlan0).
But one specified phone "HUAWEI NXT-AL 10(Android 6.0)" ,where i am able to use interface (wlan0) for ping without ROOT permission.
2) For other phones (Tried with Moto G, Micoromax android one) if root is enabled then ping is allowed with interface otherwise not.
Can you post the error message that you get, Following is the sample code which works on ubuntu:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RuntimeDemo {
public static void main(String args[]) {
String s;
Process p;
try {
p = Runtime.getRuntime().exec("ping -I wlan0 192.168.1.119");
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println ("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {}
}
}
The problem is that in Android the command is
"/system/bin/ping my_url_or_ip" this is why you can't find it.
I've built a system app running on a rooted/customized version of AOSP Android.
It could happen that I need to download new version of my app from my personal website and replace it on Android system with the new one.
This must be done automatically by the app itself and not manually with adb command.
WHAT I TRIED
Let's say i already got my apk downloaded in fpath.
With the following snippet i'm trying to remount /system/ folder with read/write permission and then move my updated apk on that folder.
try {
String line;
Process p = Runtime.getRuntime().exec("su");
// open input/output facilities
OutputStreamWriter osw = new OutputStreamWriter(p.getOutputStream());
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
// output the command
osw.write("mount -o rw,remount /system");
osw.flush();
// read the response
while ((line = in.readLine()) != null) {
Log.i("UPDATE", "output mount rw " + line);
}
// output the command
osw.write("cp " + fpath + " /system/app/myapp.apk");
osw.flush();
// read the response
while ((line = in.readLine()) != null) {
Log.i("UPDATE", "output cp " + line);
}
// output the command
osw.write("mount -o ro,remount system");
osw.flush();
// read the response
while ((line = in.readLine()) != null) {
Log.i("UPDATE", "output mount ro " + line);
}
} catch (Exception e) {
Log.e("UPDATE", "error in executing shell commands: " + e.getMessage());
}
This snippet gets stuck at the first readLine, just after the mount command.
Questions:
Why does it stuck there? Shouldn't i expect something to read from the input stream?
It just doesn't work even if i remove the readLines. Filesystem is not remount'd and file is obviously not copied. Why?
There's a way to avoid the superuser prompt asking for permissions? My app got to run on a screenless system. I cannot get a confirm by an user.
There's a better way to do what i need?
Thank you
There's a better way to do what i need?
Copying the APK will not install it. Use pm instead of cp.
There's a way to avoid the superuser prompt asking for permissions?
This is up to the su app, so if you need one that automatically grants permissions without asking or remembers the permissions.
I'm doing a simple app that show the currently process in android, like a shell.
My app execute ls, cd, makedir and other commands, but top or htop command doesnt. (htop doesn't recognize, and top, the app freeze). I need root to this? I've downloaded the terminal app in unrooted android and top command works.
my app has 2 class. a principal and a shell
principal class
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
shell class
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
Why some commands the app works and top for example doesn't??
If you have your Device connected, go to the shell to see what commands you have available using the following command in your computer's command line:
adb shell
At first glance you will be able to tell that "top" is a job that "displays and update sorted information about processes" and it blocks the shell commands line, hence, in your application is locking the thread that executed that command.
Hope it Helps!
Regards!
thanks. adb shell with top command show the process
but
p = Runtime.getRuntime().exec("top");
doesn't maybe works??
I don't understante cause my app freeze
or a top -n 1 not fixed?
I want to install .apk silently in background by BusyBox command. I`ve seen some similar questions like THIS, but I still cant get working my code properly...
I have:
My .apk I need to install on /sdcard/download/app.apk
Root
BusyBox installed
Code (not working):
String sss = Environment.getExternalStorageDirectory() + "/download/" + "app.apk";
Process install;
install = Runtime.getRuntime().exec("/system/xbin/busybox pm install " + sss);
int success = install.waitFor();
If I use "install" instead of "pm install" it copies file well.
P.S. Code above is executing in AsyncTask. No errors, but also nothing happens...
Please help!
Also I tried this, but I`m getting exit value 139 and no result:
Process process;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("pm install /mnt/sdcard/app.apk\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
int i = process.waitFor();
maybe this code will help you
Process p = null;
try
{
p = Runtime.getRuntime().exec("su");
DataOutputStream outs=new DataOutputStream(p.getOutputStream());
String cmd="pm install /mnt/sdcard/app.apk";
outs.writeBytes(cmd+"\n");
}
catch (IOException e)
{
e.printStackTrace();
}
After a lot of investigations on many android devices I realized that this code is correct and works!
There was just some problem with one device (Samsung Galaxy Tab 2 7.0 - 4.0.3 ICS). Maybe that is some strange feature of ICS. After updating firmware to 4.1.2 (Jelly Bean) problem has been resolved.
You can simply use adb install command to install/update APK silently. Sample code is below
public static void InstallAPK(String filename){
File file = new File(filename);
if(file.exists()){
try {
String command;
filename = StringUtil.insertEscape(filename);
command = "adb install -r " + filename;
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
For the purposes of monitoring Battery usage etc. I have code that executes a few dumpsys calls, reads and parses the output to extract data that I am interested in.
dumpsys battery, dumpsys statusbar, and dumpsys power all give me an error message for output like "Permission Denial: can't dump Battery service from pid..."
Also, when the application is launched there is an item in the log tagged with "PackageManager" statingNot granting permission android.permissionDUMP to package.... (protectionLevel = 3 ...)"
However, dumpsys cpuinfo and dumpsys netstat work and give me the correct output, which seems to be inconsistent.
I am able to generate dumpsys battery and the like from the adb shell, but when I try to call it programmatically it does not work.
I have tried running this on a HTC Nexus One phone as well as the emulator and get the same results for each. The weird thing is that this code worked on my Nexus One a day ago (before I upgraded from 2.2 to 2.3), and now it does not. Is this because of the upgrade?
An example of the code I am trying to run is as follows:
String command = "dumpsys battery";
try {
String s = null;
Process process = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
How do I get the dumpsys to give me the correct output programmatically and how to I get the dump permission to be granted?
*The Nexus One is not rooted and I would like to get this working without having to root it for the purposes of my project
Thank you for your help
Regular applications cannot get the DUMP permission. It is reserved to system applications.
android.permission.Dump is protected by system, signature, and development permission protection levels. Line 1993 of the source shows you this. If your APK is signed with the framework cert, is in the priv-app directory, or debuggable (see below) you can use the pm service to grant the permission, but otherwise the code specifically prevents what you're asking for (line 2624 of source).
Debuggable APKs can be created through setting the debuggable attribute on a buildType via build.gradle. Sample Android DSL:
android {
...
buildTypes {
debug {
debuggable true
...
}
quality_assurance {
debuggable true
}
...
}
If your handset had been rooted, 'dumpsys activity' will work on Android2.3:
private static void dumpIT0(String sCmd) {
try {
String s = null;
String command = "su -c " + sCmd;
Process process = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
Log.e(TAG, "IOException: " + e);
}
}
sCmd = "dumpsys activity";