There was a bit of a learning curve to using Android Studio, but not as steep as it was to get the Android SDK running in Eclipse.  Android Studio is based on Jetbrains IntelliJ IDEA, which is a mouthful and a whole new IDE for an Eclipse user.

But here is an example  of the niceties that may or may not have been available in the former Eclipse based Android IDE.

QtrExceptionI was working on a Time Recorder app for Android.  Initially I thought that all the app needed to do was log and display Start and End times for activities like Project Work Shifts and Breaks.  Soon, the paradigm shifted to recording and displaying Time Spans.  I was working on the display part, and wanted to show Spans falling withing a defined Calendar week.  In my DBHelper Class, I generated an SQLite SELECT statement, and initially it was broken.  It would always SELECT all Span Entries in the DB, not honoring the first and last parameters that I thought would qualify the result.  I added some conditions to the SELECT statement, making it more complex, and got the result displayed on the emulator in this pic.  OK, what went wrong?

Looking at Android Studio’s Logcat window gave me a clue.  It highlights the Exception cause in blue, and it is a clickable link to the source line that caused the error.

QtrLogcat1

And the clickable link brought me to this:

QtrBadSql
Which, in text, was this:

public ArrayList<TimeSpanEntry> getSpansInDateRange(String uName, String jName, long first, long last) {
    long uId = getUserIdFromUname(uName);
    long jId = getJobIdFromName(jName);
    ArrayList<TimeSpanEntry> entries = new ArrayList<>();

    String sql = "SELECT * FROM " + TABLE_NAME_ENTRIES +
            " WHERE (" + COL_NAME_USER_ID + "=" + Long.toString(uId) +
            " AND " + COL_NAME_JOB_ID + "=" + Long.toString(jId) +
            " AND ((" + COL_NAME_START_TIME + " BETWEEN " +
            Long.toString(first) + " AND " + Long.toString(last) + ") " +
            " OR (" + COL_NAME_END_TIME + " BETWEEN " +
            Long.toString(first) + " AND " + Long.toString(last) + "))";

    Log.d(TAG, "SQL = " + sql);

    Cursor c;
    c = db.rawQuery(sql, null);

    if(c.moveToFirst()) {
        while (!c.isAfterLast()) {
            TimeSpanEntry e = cursorToSpanEntry(c);
            c.moveToNext();
            entries.add(e);
        }
        c.close();
    }
    return entries;
}

And the actual logcat entry said:

Caused by: android.database.sqlite.SQLiteException: near ")": syntax error: , while compiling:
SELECT * FROM entries WHERE (user_id=1 AND job_id=1 AND ((start_time BETWEEN 1433128718062 AND 1433733518062)  OR (end_time BETWEEN 1433128718062 AND 1433733518062)

So, it was a bad SQL statement.  Since I had used rawQuery() there was no syntax checking that Studio’s code scanner could help with.  I went to the Logcat line where the code logged the actual SQL statement executed, and copied it.  I next went to Studio’s Terminal window, opened up an ADB shell to the emulator, and started sqlite3.  And pasted Logcat’s SQL statement into it.

QtrSqliteWindow

SELECT * FROM entries WHERE (user_id=1 AND job_id=1 AND ((start_time BETWEEN 1433126674976 AND 1433731474976)  OR (end_time BETWEEN 1433126674976 AND 1433731474976));

SQLite3 told me

"Error: near ";": syntax error "

.  I had missed a close parenthesis.  So, I tried:

SELECT * FROM entries WHERE (user_id=1 AND job_id=1 AND ((start_time BETWEEN 1433126674976 AND 1433731474976) OR (end_time BETWEEN 1433126674976 AND 1433731474976)));

And got:

1|1|0||1433260445941|1433264172300|0|1|1
1|1|0||1433280265924|9223372036854775807|0|1|2

I went back to the Studio Editor window, added my paren, and got rid of the exception AND my routine was selecting the proper entries for the specified range.

Summary

Studio has a bunch of tools and windows available to code and debug Android development.

  • Code scanner and auto-complete rocks with JavaDoc available Ctl+Q
  • LogCat window with clickable links to exceptin causes in source code (in your project, not just the java libraries).
  • Terminal window for running ADB shell.

In a previous project, I was downloading the SQL db from the target emulator to the host machine, and using an SQLite browser program to examine and debug SQL problems.  Maybe it’s me that’s improving, not just the tools.