How to redirect logcat output? - android

Is there a way to redirect the logcat's output towards an InputStream or something like that? I'm thinking of something in the lines of how you can redirect the stderr in C.
I want to do it so its redirected when my app starts and everything that happends get dumped to a file.

If you can connect the device for debugging, you can this from the command line
$ adb logcat > textfile.txt

The easiest way I found is to use System.setErr. I allows you to easily redirect the error output to a file.
Example:
System.setErr(new PrintStream(new File("<file_path>"))

The only redirecting possibilites of LogCat output are documentend here and here.
It is not possible to reuse the LogCat output in your app itself. You can however, export it to a file like you ask.

to filter logcat just from your app try this:
int pid = android.os.Process.myPid();
File outputFile = new File(Environment.getExternalStorageDirectory() + "/logcat.txt");
Log.d("zzz","outputFile: " + outputFile);
try {
String command = "logcat | grep " + pid + " > " + outputFile.getAbsolutePath();
Log.d("zzz","command: " + command);
Process p = Runtime.getRuntime().exec("su");
OutputStream os = p.getOutputStream();
os.write((command + "\n").getBytes("ASCII"));
} catch (IOException e) {
}

In Kotlin, this is what I used and it seems to create and log to the logcat.txt file (located in the app directory) whether the app is being debugged or run directly from the tablet. (Sidenote: For some reason the directory doesn't refresh or show up in file explorer until I stop the app and reconnect the device.) (thanks for the help from the previous contributors)
located in utilities...
val publicDirectory = globalContext.getExternalFilesDir(null)
publicDirectoryName = publicDirectory?.toString() + "/"
fun redirectLogcatToFile() {
try {
val filename =
File(Utilities.publicDirectoryName + "logcat.txt")
filename.createNewFile()
val cmd = "logcat -v time -f " + filename.getAbsolutePath() +" -s " + logcatTag
Runtime.getRuntime().exec(cmd)
} catch (ex: Exception ) {
Utilities.toastOnAnyThread("Utilities.redirectLogcatToFile: " + ex.message)
}
}
fun Log( message : String ){
Log.i(logcatTag, message )
}

Related

How to share split APKs created while using instant-run, within Android itself?

Background
I have an app (here) that, among other features, allows to share APK files.
In order to do so, it reaches the file by accessing the path of packageInfo.applicationInfo.sourceDir (docs link here), and just shares the file (using ContentProvider when needed, as I've used here).
The problem
This works fine in most cases, especially when installing APK files from either the Play Store or from a standalone APK file, but when I install an app using Android-Studio itself, I see multiple APK files on this path, and none of them are valid ones that can be installed and run without any issues.
Here's a screenshot of the content of this folder, after trying out a sample from "Alerter" github repo :
I'm not sure when this issue has started, but it does occur at least on my Nexus 5x with Android 7.1.2. Maybe even before.
What I've found
This seems to be caused only from the fact that instant run is enabled on the IDE, so that it could help updating the app without the need to re-build it all together :
After disabling it, I can see that there is a single APK, just as it used to be in the past:
You can see the difference in file size between the correct APK and the split one.
Also, it seems that there is an API to get the paths to all of the splited APKs:
https://developer.android.com/reference/android/content/pm/ApplicationInfo.html#splitPublicSourceDirs
The question
What should be the easiest way to share an APK that got to be split into multiple ones ?
Is it really needed to somehow merge them?
It seems it is possible according to the docs :
Full paths to zero or more split APKs that, when combined with the
base APK defined in sourceDir, form a complete application.
But what's the correct way to do it, and is there a fast and efficient way to do it? Maybe without really creating a file?
Is there maybe an API to get a merged APK out of all the split ones? Or maybe such an APK already exist anyway in some other path, and there is no need for merging?
EDIT: just noticed that all third party apps that I've tried are supposed to share an installed app's APK fail to do so in this case.
I am the tech lead #Google for the Android Gradle Plugin, let me try to answer your question assuming I understand your use case.
First, some users mentioned you should not share your InstantRun enabled build and they are correct. The Instant Run builds on an application is highly customized for the current device/emulator image you are deploying to. So basically, say you generate an IR enabled build of your app for a particular device running 21, it will fail miserably if you try to use those exact same APKs on say a device running 23. I can go into much deeper explanation if necessary but suffice to say that we generate byte codes customized on the APIs found in android.jar (which is of course version specific).
So I do not think that sharing those APKs make sense, you should either use a IR disabled build or a release build.
Now for some details, each slice APK contains 1+ dex file(s), so in theory, nothing prevents you from unziping all those slice APKs, take all the dex files and stuff them back into the base.apk/rezip/resign and it should just work. However, it will still be an IR enabled application so it will start the small server to listen to IDE requests, etc, etc... I cannot imagine a good reason for doing this.
Hope this helps.
To merge multiple split apks to an single apk might be a little complicated.
Here is a suggestion to share the split apks directly and let the system to handle the merge and installation.
This might not be an answer to the question, since it's a little long, I post here as an 'answer'.
Framework new API PackageInstaller can handle monolithic apk or split apk.
In development environment
for monolithic apk, using adb install single_apk
for split apk, using adb install-multiple a_list_of_apks
You can see these two modes above from android studio Run output depends on your project has Instant run enable or disable.
For the command adb install-multiple, we can see the source code here, it will call the function install_multiple_app.
And then perform the following procedures
pm install-create # create a install session
pm install-write # write a list of apk to session
pm install-commit # perform the merge and install
What the pm actually do is call the framework api PackageInstaller, we can see the source code here
runInstallCreate
runInstallWrite
runInstallCommit
It's not mysterious at all, I just copied some methods or function here.
The following script can be invoked from adb shell environment to install all split apks to device, like adb install-multiple. I think it might work programmatically with Runtime.exec if your device is rooted.
#!/system/bin/sh
# get the total size in byte
total=0
for apk in *.apk
do
o=( $(ls -l $apk) )
let total=$total+${o[3]}
done
echo "pm install-create total size $total"
create=$(pm install-create -S $total)
sid=$(echo $create |grep -E -o '[0-9]+')
echo "pm install-create session id $sid"
for apk in *.apk
do
_ls_out=( $(ls -l $apk) )
echo "write $apk to $sid"
cat $apk | pm install-write -S ${_ls_out[3]} $sid $apk -
done
pm install-commit $sid
I my example, the split apks include (I got the list from android studio Run output)
app/build/output/app-debug.apk
app/build/intermediates/split-apk/debug/dependencies.apk
and all apks under app/build/intermediates/split-apk/debug/slices/slice[0-9].apk
Using adb push all the apks and the script above to a public writable directory, e.g /data/local/tmp/slices, and run the install script, it will install to your device just like adb install-multiple.
The code below is just another variant of the script above, if your app has platform signature or device is rooted, I think it will be ok. I didn't have the environment to test.
private static void installMultipleCmd() {
File[] apks = new File("/data/local/tmp/slices/slices").listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getAbsolutePath().endsWith(".apk");
}
});
long total = 0;
for (File apk : apks) {
total += apk.length();
}
Log.d(TAG, "installMultipleCmd: total apk size " + total);
long sessionID = 0;
try {
Process pmInstallCreateProcess = Runtime.getRuntime().exec("/system/bin/sh\n");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(pmInstallCreateProcess.getOutputStream()));
writer.write("pm install-create\n");
writer.flush();
writer.close();
int ret = pmInstallCreateProcess.waitFor();
Log.d(TAG, "installMultipleCmd: pm install-create return " + ret);
BufferedReader pmCreateReader = new BufferedReader(new InputStreamReader(pmInstallCreateProcess.getInputStream()));
String l;
Pattern sessionIDPattern = Pattern.compile(".*(\\[\\d+\\])");
while ((l = pmCreateReader.readLine()) != null) {
Matcher matcher = sessionIDPattern.matcher(l);
if (matcher.matches()) {
sessionID = Long.parseLong(matcher.group(1));
}
}
Log.d(TAG, "installMultipleCmd: pm install-create sessionID " + sessionID);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
StringBuilder pmInstallWriteBuilder = new StringBuilder();
for (File apk : apks) {
pmInstallWriteBuilder.append("cat " + apk.getAbsolutePath() + " | " +
"pm install-write -S " + apk.length() + " " + sessionID + " " + apk.getName() + " -");
pmInstallWriteBuilder.append("\n");
}
Log.d(TAG, "installMultipleCmd: will perform pm install write \n" + pmInstallWriteBuilder.toString());
try {
Process pmInstallWriteProcess = Runtime.getRuntime().exec("/system/bin/sh\n");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(pmInstallWriteProcess.getOutputStream()));
// writer.write("pm\n");
writer.write(pmInstallWriteBuilder.toString());
writer.flush();
writer.close();
int ret = pmInstallWriteProcess.waitFor();
Log.d(TAG, "installMultipleCmd: pm install-write return " + ret);
checkShouldShowError(ret, pmInstallWriteProcess);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
try {
Process pmInstallCommitProcess = Runtime.getRuntime().exec("/system/bin/sh\n");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(pmInstallCommitProcess.getOutputStream()));
writer.write("pm install-commit " + sessionID);
writer.flush();
writer.close();
int ret = pmInstallCommitProcess.waitFor();
Log.d(TAG, "installMultipleCmd: pm install-commit return " + ret);
checkShouldShowError(ret, pmInstallCommitProcess);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
private static void checkShouldShowError(int ret, Process process) {
if (process != null && ret != 0) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String l;
while ((l = reader.readLine()) != null) {
Log.d(TAG, "checkShouldShowError: " + l);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Meanwhile, the simple way, you can try the framework api. Like the sample code above, it might work if the device is rooted or your app has platform signature, but I didn't get a workable environment to test it.
private static void installMultiple(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
try {
final int sessionId = packageInstaller.createSession(sessionParams);
Log.d(TAG, "installMultiple: sessionId " + sessionId);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
File[] apks = new File("/data/local/tmp/slices/slices").listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getAbsolutePath().endsWith(".apk");
}
});
for (File apk : apks) {
InputStream inputStream = new FileInputStream(apk);
OutputStream outputStream = session.openWrite(apk.getName(), 0, apk.length());
byte[] buffer = new byte[65536];
int count;
while ((count = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}
session.fsync(outputStream);
outputStream.close();
inputStream.close();
Log.d(TAG, "installMultiple: write file to session " + sessionId + " " + apk.length());
}
try {
IIntentSender target = new IIntentSender.Stub() {
#Override
public int send(int i, Intent intent, String s, IIntentReceiver iIntentReceiver, String s1) throws RemoteException {
int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE);
Log.d(TAG, "send: status " + status);
return 0;
}
};
session.commit(IntentSender.class.getConstructor(IIntentSender.class).newInstance(target));
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In order to use the hidden api IIntentSender, I add the jar library android-hidden-api as the provided dependency.
Those are called split apks. Which is mainly used in the PlayStore. As you may know, PlayStore only shows apps to the user if it's compatible with the device. Same in this case. The split has files varies from devices. Like if you used Different resources for Different devices which makes app really heavy. By making splits, it saves space for downloading and running for the user by only downloading the usable split apks.
Is it possible to merge them into single apk?
Yes. I used an app called Anti Split which allows that. Plus Apk Editor Ultra has same.
Can we save it into a single file?
Yes you can. As like for Anti Split, you have to first backup the app. Like in this case you have to back it up as apks file or xapk which is called bundled app in Android Studio. I have created a library for doing this. It's working perfectly for me. Am using it to backup apps into xapk which can later be installed using SIA app or XAPK Installer or we can use xapk file to merge it and make apk
For me instant run was a nightmare, 2-5 minute build times, and maddeningly often, recent changes were not included in builds. I highly recommend disabling instant run and adding this line to gradle.properties:
android.enableBuildCache=true
First build often takes some time for large projects (1-2mins), but after it's cached subsequent builds are usually lightnight fast (<10secs).
Got this tip from reddit user /u/QuestionsEverythang which has saved me SO much hassling around with instant run!

Unable to merge videos using FFMPEG Commands

I am trying to merge two videos in Android using FFMPEG and I have been following the Android War Zone blog which gives great ideas and simple methods to integrate FFMPEG in our project. However, I am facing issues in merging two videos.
Command :
vk.run(new String[]{
"ffmpeg",
"-f",
"concat",
"-i",
list,
"-s",
"hd720",
"-c",
"copy",
"-b",
br_from_db + "k",
path + "/" + "merged_video_3.mp4"
}, work_path, getActivity());
And the "list" in the above command is the one where I am facing a issue.It throws me the following error when I use the following method :
Code :
private String generateList(String[] inputs) {
File list;
Writer writer = null;
try {
list = File.createTempFile("ffmpeg-list", ".txt");
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(list)));
for (String input : inputs) {
writer.write("file '" + input + "'\n");
Log.d(TAG, "Writing to list file: file '" + input + "'");
}
} catch (IOException e) {
e.printStackTrace();
return "/";
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Log.d(TAG, "Wrote list file to " + list.getAbsolutePath());
return list.getAbsolutePath();
}
Error :
12-16 19:49:57.416 5437-5437/? E/ffmpeg4android﹕ Command validation failed.
12-16 19:49:57.416 5437-5437/? E/ffmpeg4android﹕ Check if input file exists: /data/data/com.family45.golive.family45v1/cache/ffmpeg-list-1803386407.txt/storage/emulated/0/DCIM/Camera/dec24.mp4 /storage/emulated/0/DCIM/Camera/vid2.mp4
12-16 19:49:57.416 5437-5437/? W/System.err﹕ com.netcompss.ffmpeg4android.CommandValidationException
12-16 19:49:57.416 5437-5437/? W/System.err﹕ at com.netcompss.loader.LoadJNI.run(LoadJNI.java:34)
12-16 19:49:57.416 5437-5437/? W/System.err﹕ at com.netcompss.loader.LoadJNI.run(LoadJNI.java:49)
I obtained the command from this stack question. Its accepted but I am facing the above issue. I am very sure that the videos are present in their respective locations and all the paths are right but I cant seem to make it work.
Any insights on this is highly appreciated. Thanks in advance.
Update :
Call to generateList:
ArrayList<String> paths_to_merge = new ArrayList<String>();
paths_to_merge.add(path + "/" + "dec24.mp4");
paths_to_merge.add(path + "/" + "vid2.mp4");
LoadJNI vk = new LoadJNI();
String[] v12 = new String[paths_to_merge.size()];
v12 = paths_to_merge.toArray(v12);
String list = generateList(v12);
I am not sure what went wrong in my code, I am still not able to come with the right list. However, I found another command which seems to be working good.
Command :
vk.run(new String[]{"ffmpeg","-y","-i",path + "/" + "num1.mp4","-i",path + "/" + "num2.mp4","-i",path + "/" + "num3.mp4","-i",path + "/" + "num4.mp4",
"-i",created_folder + "/" + "created_video2.mp4","-strict","experimental",
"-filter_complex",
"[0:v]scale=640x480,setsar=1:1[v0];[1:v]scale=640x480,setsar=1:1[v1];[2:v]scale=640x480,setsar=1:1[v2];[3:v]scale=640x480,setsar=1:1[v3];" +
"[4:v]scale=640x480,setsar=1:1[v4];[v0][0:a][v1][1:a][v2][2:a][v3][3:a][v4][4:a] concat=n=5:v=1:a=1",
"-ab","48000","-ac","2","-ar","22050","-s","640x480","-r","30","-vcodec","mpeg4","-b","2097k",path + "/" + "numbers_video_m.mp4"},path,getActivity());
As you can see in the command, I have appended 5 videos for the purpose of testing but I believe that we can add more videos dynamically and this works without any issues for me.
Things to be noted :
"-i",path + "/" + "num1.mp4"
represent the input and you can append as many as you want.
[0:v]scale=640x480,setsar=1:1[v0];
and add this according to the number of inputs accordingly as [0:v]...[1:v].. and so on.
[v0][0:a]
and also this parameter to be added according the number of inputs.
concat=n=5:v=1:a=1
Give the value of n according to the number of videos.
So those are the main things that needs to be taken care of.

How to change DNS in android 4.x+ using internal linux commands within app?

I have been working on this small project in college about changing the default DNS of wifi network to a custom DNS like Google, OpenDNS, Metacert, etc.
I know I have to write a shellscript inside the app's code that would edit the hosts file in the filesystem.
The problem is I have no idea where to start from. I have researched on google for some time and I couldn't figure anything.
If anyone knows about it, please guide me. Please tell me the name of the file to be edited, its location, what commands are required and how to run those commands' combination as a shellscript on a click of a button on the UI of app.
EDIT : I'm stuck only at this. Any help will be greatly appreciated.
I'm not sure about which files you would have to edit but this should give you the tools you need to do that.
The first thing you need to do is root the phone if you haven't already. If it's not rooted, you'll run into an issue like: Working Directory : null environment when running Process.Builder on android
There are a lot of guides available for that online. Install SuperSU as well. In order to run shell commands or scripts you should look at the ProcessBuilder class in Android:
http://developer.android.com/reference/java/lang/ProcessBuilder.html
I've given some sample code below to help you along the way. You could execute this in an OnClick() for a button.
/**
* Runs the shell command.
*
* #param command an array of Strings. command[0] contains the name of the
* shell command. command[1]... contains parameters.
*
* #return the text outputted by the command to stderr or stdout
*/
String runCmd(String[] command, boolean readOutput,
boolean waitForExit) {
ProcessBuilder probuilder = new ProcessBuilder()
.command(command)
.redirectErrorStream(true);
String output = "";
Process process;
// Log.d("MyShellCommand", "Executing " + command[0]);
try {
process = probuilder.start();
} catch(IOException e) {
return e.getMessage();
}
if (readOutput) {
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
// Log.d("MyShellCommand", "Read a line: " + line);
output += line + "\n";
}
} catch(IOException e) {
output = e.getMessage();
}
}

How to read logcat written through my application in all android os

I need to capture all the logs written through my application. I know from Jetllybean OS we can read need only our application log. But when I tried by using command "logcat -d" using exec method by application and I did not get any data.
Please help me on this.
Thanks,
Saravanakumar
This is the example that I was playing around with before that will generate a log text file in local storage:
private static String generateLogcatLogCommond = "logcat -d > /sdcard/IssueReport/log.txt";
public static String generateLogcatLog() throws InterruptedException {
try {
File issueReport = new File(Environment.getExternalStorageDirectory(), "IssueReport");
if (!issueReport.exists())
issueReport.mkdir();
File logFile = new File(issueReport,"log.txt");
logFile.createNewFile();
Process process = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(generateLogcatLogCommond);
logLocation = "/sdcard/IssueReport/log.txt";
Log.d("Client", logLocation);
} catch (IOException e) {
e.printStackTrace();
}
return logLocation;
}
What the above code is doing is using 'sh' to run 'logcat -d' command and save it as a file locally. This will get ALL the logcat log. For you, you can change that to 'logcat -s ""' and it will save all logcat log of your application to a file.

Building a Terminal Emulator for Android

I've been trying to build a Terminal Emulator for Android. Being pretty new to this, my idea was to execute each command and store the output in a file, whose contents would be displayed after each execution.
Pseudo Code :
public Boolean execCommands(String command) {
try {
rt = Runtime.getRuntime();
process = rt.exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("echo $ \""+command+ "\" >> /sdcard/Android/data/terminalemulatorlog.txt\n\n\n");
/**** Note : String command = (EditText)findViewById(R.id.command).getText().toString(); ****/
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
}
// Error Handling
displayOutput(); //Loads and displays the Text File (/sdcard/Android/data/terminalemulatorlog.txt)
return true;
}
This piece of code works except for a few special commands (Eg. 'clear').
But what I'm more concerned about are the following problems :
Each time a command is to be executed, I end up seeking SuperUser permissions (second line of code). And I'd like to do away with this.
In cases when the user enters one command followed by another,
Such as :
cd /sdcard
touch File.txt
The File.txt is created in '/' and not in '/sdcard'. As of now to avoid this, I'm keeping a track of all the 'cd' commands to figure out what the present working directory is. And I'm hoping that there is a better way around this.
I'd be grateful if someone could help me out here.
Not sure if you are still needing this or not, but here is how I am issuing multiple commands at one time and not using "su" to have them run.
try {
String[] commands = {
"dumpstate > /sdcard/LogFiles/dumpstate.txt",
"dumpsys > /sdcard/LogFiles/dumpsys.txt",
"logcat -d > /sdcard/LogFiles/log.txt",
"cat /sdcard/LogFiles/dumpstate.txt /sdcard/LogFiles/dumpsys.txt /sdcard/LogFiles/log.txt > /sdcard/LogFiles/bugreport.rtf" };
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
This a bit late but here a few ways of doing this.
1)
Instead of using su as a starting point use /system/bin/sh.
and after calling
rt.exec("/system/bin/sh");
You should hold onto the Output Stream and Input Stream to give further commands.
After you issued a command you should echo a magic line like "---EOF---" and stop reading input after reading that line. If you don't have this you'll end up with the read function from the InputStream blocking.
2) Pipe the data to a native process you've written that simply moves the data on to your Android Application with a terminating character or string attached to the end.
I am not entirely sure how to do this, but it is essentially the same as the previous method just relies on you native application as a middle man.
This will get you close to a functioning "Terminal Emulator".
3)If you wan't a true Ternimal Emulator then there's no other way to do it than : using a native application that opens a connection to a psuedoterminal.
Here's some basic information of how to open a pty : link
Terminal Emulator is a open source project that uses this technique.
Have a look here
Regarding problem 1:
Each time a command is to be executed, I end up seeking SuperUser permissions (second line of code). And I'd like to do away with this.
Thanks to Xonar's suggestion from another answer:
After you issued a command you should echo a magic line like "---EOF---" and stop reading input after reading that line.
Solution in Kotlin:
private lateinit var suProcess: Process
private lateinit var outputStream: DataOutputStream
private fun getSu(): Boolean {
return try {
suProcess = Runtime.getRuntime().exec("su")
outputStream = DataOutputStream(suProcess.outputStream)
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}
private fun sudo(command: String): List<String>? {
return try {
outputStream.writeBytes("$command\n")
outputStream.flush()
outputStream.writeBytes("echo ---EOF---\n")
outputStream.flush()
val reader = suProcess.inputStream.bufferedReader()
val result = mutableListOf<String>()
while (true) {
val line = reader.readLine()
if (line == "---EOF---") break
result += line
}
result
} catch (e: Exception) {
e.printStackTrace()
null
}
}
private fun exitTerminal() {
try {
outputStream.writeBytes("exit\n")
outputStream.flush()
suProcess.waitFor()
} catch (e: Exception) {
e.printStackTrace()
} finally {
outputStream.close()
}
}
//Activity method
override fun onDestroy() {
super.onDestroy()
exitTerminal()
}

Categories

Resources