Paul MacGuiheen
Paul MacGuiheen

Reputation: 638

Is it possible to both develop and debug android app's on a chromebook?

I'm using crouton to create a linux desktop on my chromebook. Here I have installed Android Studio and started working on a simple android app. I can build an apk, move it to the Downloads folder and then flip over from linux to ChromeOS and run the app. (I use APK Installer - works fine).

I'd like to be able to see the logcat from my app (well actually i'd like to see all the diagnostics you get when running in an emulator in Android Studio - but i'd settle for logcat).

Anything i've read about using adb expects you to have a dev machine where Android Studio is and a target machine where your app is running. Using crouton the linux desktop and ChromeOS are on the same machine and only one can be running at a time as they share the same resources, etc. I tried a few app's but none were able to show the logcat from my app running on chromebook - they don't even recognize it is running. Anyone got any ideas on how to view the logcat for this particular setup?

Upvotes: 0

Views: 323

Answers (1)

Paul MacGuiheen
Paul MacGuiheen

Reputation: 638

So far I have found a way to get logcat and am settling for that ... for now

In the main Activity onCreate call this method;

    public static void saveLogcatToFile(Context context) {
            File outputFile = new File(context.getFilesDir(), "logcat.txt");

            try {
                @SuppressWarnings("unused")
                Process process = Runtime.getRuntime().exec("logcat -df " + outputFile.getAbsolutePath());
            } catch (IOException e) {...

In the onCreate of another Activity fill a TextView with the logcat using;

    public static String readLogcatFromFile(Context context) {
            File logFile = new File(context.getFilesDir(), "logcat.txt");
            if (logFile.exists() == false) { ...

            String logContents = context.getString(R.string.EMPTY_STRING);
            FileInputStream fileInStream = null;
            try {
                fileInStream = new FileInputStream(logFile);
                logContents = convertStreamToString(fileInStream);
            } catch (Exception e) { ...
            } finally { ...
                fileInStream.close();
            }
            return logContents;
    }

    private static String convertStreamToString(InputStream is) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
    }

The logs keep appending for each run until you uninstall (this deletes the log file). I find it particularly useful when I break something and my app just dies on startup as I can revert to a prior commit and take a look in the logs to see what happened

Upvotes: 0

Related Questions