We are using UIAutomator for automation.
To run UIAutomator test scripts we used to compile the scripts from command prompt by using either USB or Wifi adb and it will run on the android device.
UiAutomator scripts will come in jar format after compilation.
We will push the jar in the device and we have trigger the test scripts.
The command to start the test script in “adb shell uiautomator runtest TestPackage.jar”.
It is working with normal adb connection.
But in our case we have to initiate the test scripts without any adb connection.
So we tried by passing the commands programmatically in the device using this java code
try {
StringBuffer output = new StringBuffer();
Process p = Runtime.getRuntime().exec(params[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
p.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
After the execution we are getting the following exception:
java.lang.securityException : longMsg = You do not have android.permission.RETRIEVE_WINDOW_CONTENT required to call registerUITestAutomationService
If we run scripts from command prompt with adb connection it is working. But if we tried the command programmatically we are getting this exception.
We came to know that the above mentioned issue can be overcome in a rooted android device by enabling the RETRIEVE_WINDOW_CONTENT permission. But we want this scripts to run on a non-rooted setup.
Can anyone help me in explaining this security permission issue?
How can I resolve it?
I am working on an Android App and trying to access an executable at /data/data/app_package_name/files/libs/xyz and executing a command formed using this path.
For execution of command I am using the following code
Runtime rt = Runtime.getRuntime();
String command = "/data/data/app_package_name/files/libs/xyz -edir/data/data/app_package_name/files/libs/ "+ var1 +" "+ var2+" -someFlag , -fl -g, -head";
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
line=input.readLine()
I am getting line as nullbut in only Android Lollipop .Its working in Kitkat or below Android versions.
Please comment if any more information is required.
I covered this part of code in try,catch block also but no exceptions is there.
I want to do some work for which I require shell in eclipse but I can't get it anywhere.
I have tried :
Window > Show View > other
but i didn't get any option over there.
Shall I download something for the shell, or do any settings to get the shell ?
Give any link.
Thank you.
You need Eclipse RSE (Remote System Explorer) installed for this support.
Go to Help > Install New Software, choose your Eclipse release to work with. RSE is in the Mobile and Device Development section.
You can also access using java code
Process p;
p = Runtime.getRuntime().exec("//enter Your shell Command here");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
sb.append(line);
while (line != null) {
line = reader.readLine();
sb.append(line);
}
System.out.println(sb.toString());//ur shell command result
The logcat process is launched, I don't get any error or force close. It simply seems to not generate any output.
String commandLine = "logcat -v time -b main *:V";
String line;
try {
process = Runtime.getRuntime().exec(commandLine);
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while(!mStop && (line = bufferedReader.readLine()) != null && newMessages.size() < maxMessages) {
// It does not reach this point
// Debugger shows
// mStop == false
// newMessages.size == 0
// maxMessages == 5000
// line == null
The suspicion is that logcat exits wihtout printing anything out. I tried this in a command line and it does output logcat messages.
I replaced logcat with ls and then bufferedReader does fill line.
The inspection of bufferedReader shows member in(InputStreamReader) which itself has a memeber endOfInput which is false before the readLine() and becomes true right after it.
LAST NEWS:
I opened the Terminal Emulator within the emulator itself, and run it:
$ logcat -v time -b main *:V
Unable to open log device '/dev/log/main': Permission denied
$
After looking for similar post it seems this is quite a duplicate from:
Android unable to run logcat from application
In short, the problem was: a user permission is required to read logs, so I added:
<uses-permission android:name="android.permission.READ_LOGS" />
to the manifest and it works fine now.
I would like to pull the log file from a device to my PC. How can I do that?
Logcollector is a good option but you need to install it first.
When I want to get the logfile to send by mail, I usually do the following:
connect the device to the pc.
Check that I already setup my os for that particular device.
Open a terminal
Run adb shell logcat > log.txt
I hope this code will help someone. It took me 2 days to figure out how to log from device, and then filter it:
public File extractLogToFileAndWeb(){
//set a file
Date datum = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY);
String fullName = df.format(datum)+"appLog.log";
File file = new File (Environment.getExternalStorageDirectory(), fullName);
//clears a file
if(file.exists()){
file.delete();
}
//write log to file
int pid = android.os.Process.myPid();
try {
String command = String.format("logcat -d -v threadtime *:*");
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (currentLine != null && currentLine.contains(String.valueOf(pid))) {
result.append(currentLine);
result.append("\n");
}
}
FileWriter out = new FileWriter(file);
out.write(result.toString());
out.close();
//Runtime.getRuntime().exec("logcat -d -v time -f "+file.getAbsolutePath());
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
//clear the log
try {
Runtime.getRuntime().exec("logcat -c");
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
return file;
}
as pointed by #mehdok
add the permission to the manifest for reading logs
<uses-permission android:name="android.permission.READ_LOGS" />
I would use something of this sort :
$adb logcat -d > logcat.txt
The -d option dumps the entire circular buffer into the text file and if you are looking for a particular action/intent try
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
Hope this helps :)
For those not interested in USB debugging or using adb there is an easier solution. In Android 6 (Not sure about prior version) there is an option under developer tools: Take Bug Report
Clicking this option will prepare a bug report and prompt you to save it to drive or have it sent in email.
I found this to be the easiest way to get logs. I don't like to turn on USB debugging.
EDIT:
The internal log is a circular buffer in memory. There are actually a few such circular buffers for each of: radio, events, main. The default is main.
To obtain a copy of a buffer, one technique involves executing a command on the device and obtaining the output as a string variable.
SendLog is an open source App which does just this: http://www.l6n.org/android/sendlog.shtml
The key is to run logcat on the device in the embedded OS. It's not as hard as it sounds, just check out the open source app in the link.
Often I get the error "logcat read: Invalid argument". I had to clear the log, before reading from the log.
I do like this:
prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
I know it's an old question, but I believe still valid even in 2018.
There is an option to Take a bug report hidden in Developer options in every android device.
NOTE: This would dump whole system log
How to enable developer options? see: https://developer.android.com/studio/debug/dev-options
What works for me:
Restart your device (in order to create minimum garbage logs for developer to analyze)
Reproduce your bug
Go to Settings -> Developer options -> Take a bug report
Wait for Android system to collect the logs (watch the progressbar in notification)
Once it completes, tap the notification to share it (you can use gmail or whetever else)
how to read this?
open bugreport-1960-01-01-hh-mm-ss.txt
you probably want to look for something like this:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of crash
06-13 14:37:36.542 19294 19294 E AndroidRuntime: FATAL EXCEPTION: main
or:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of main
A simple way is to make your own log collector methods or even just an existing log collector app from the market.
For my apps I made a report functionality which sends the logs to my email (or even to another place - once you get the log you can do whether you want with it).
Here is a simple example about how to get the log file from a device:
http://code.google.com/p/android-log-collector/
Simple just run the following command to get the output to your terminal:
adb shell logcat
Two steps:
Generate the log
Load Gmail to send the log
.
Generate the log
File generateLog() {
File logFolder = new File(Environment.getExternalStorageDirectory(), "MyFolder");
if (!logFolder.exists()) {
logFolder.mkdir();
}
String filename = "myapp_log_" + new Date().getTime() + ".log";
File logFile = new File(logFolder, filename);
try {
String[] cmd = new String[] { "logcat", "-f", logFile.getAbsolutePath(), "-v", "time", "ActivityManager:W", "myapp:D" };
Runtime.getRuntime().exec(cmd);
Toaster.shortDebug("Log generated to: " + filename);
return logFile;
}
catch (IOException ioEx) {
ioEx.printStackTrace();
}
return null;
}
Load Gmail to send the log
File logFile = generateLog();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(logFile));
intent.setType("multipart/");
startActivity(intent);
References for #1
https://stackoverflow.com/a/34883741/2162226
https://stackoverflow.com/a/3359857/2162226
~~
For #2 - there are many different answers out there for how to load the log file to view and send. Finally, the solution here actually worked to both:
load Gmail as an option
attaches the file successfully
Big thanks to https://stackoverflow.com/a/22367055/2162226 for the correctly working answer
Thanks to user1354692 I could made it more easy, with only one line! the one he has commented:
try {
File file = new File(Environment.getExternalStorageDirectory(), String.valueOf(System.currentTimeMillis()));
Runtime.getRuntime().exec("logcat -d -v time -f " + file.getAbsolutePath());}catch (IOException e){}
I have created a small library (.aar) to retrieve the logs by email. You can use it with Gmail accounts. It is pretty simple but works. You can get a copy from here
The site is in Spanish, but there is a PDF with an english version of the product description.
I hope it can help.
First make sure adb command is executable by setting PATH to android sdk platform-tools:
export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH
then run:
adb shell logcat > log.txt
OR first move to adb platform-tools:
cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools
then run
./adb shell logcat > log.txt
I would use something like:
$ adb logcat --pid=$(adb shell pidof com.example.yourpackage)
which you can then redirect to a file
$ adb logcat --pid=$(adb shell pidof com.example.yourpackage) > log.txt
or if you also want to see it at stdout as well:
$ adb logcat --pid=$(adb shell pidof com.example.yourpackage) | tee log.txt