Wednesday, April 23, 2014

Gradle Android : Multiple project setup

For a Multi-project setup, the Android documentation for gradle states :
"A multi-project setup usually works by having all the projects as sub folders of a given root project."

 + app/
 + libraries/
    + lib1/

But I have a different situation where my library project and the actual project is not under the same root.

So, my library project is inside its own directory. Then I have my main project in some other directory wanting to refer to this library project. I had a hard time to figure this out with gradle. So, hopefully this helps someone.

So basically, the structure is as follows:

    |---- foolib-root
                |---- foolib
                           |---- build.gradle
                           |---- src
                           |---- libs
                |---- build.gradle
                |---- settings.gradle

    |---- project1-root
                |---- project1
                           |---- build.gradle
                           |---- src
                           |---- libs
                |---- build.gradle
                |---- settings.gradle

Just, FYI, the build.gradle inside foolib-root/foolib contains:

apply plugin: 'android-library'

indicating that it is a library module.

Now, the task is to refer to library project foolib from project1.

I have found there are 2 things you need to do to achieve this:

1. The settings.gradle in project1-root must define the library project in its scope and provide a path for it. You can do this by having the following line in it:

 project(':foolib').projectDir = new File(settingsDir, '../Libraries/foolib-root/foolib')

2. Add this library project into the dependencies of project1 by adding the following to its build.gradle:

dependencies {
    compile project(':foolib')

This did the trick for me. Not sure if this is a right way to do it. But serves my purpose.

Friday, April 11, 2014

Debugging Cursor Values

Many times, you will have to query content providers. For that you'll either use a content resolver or use the CursorLoader class. In both cases you get back a Cursor object back.

For quick debugging, you'd want to examine the contents of the cursor. For quick debugging purpose, it is painful to extract all the columns coz you need to know the column data types and names etc...

Cursor cursor = getContentResolver().query(<uri and other params>);
if (cursor != null) {
    while (cursor.moveToNext()){
          intValue = cursor.getInt(cursor.getColumnIndex(<Column name from provider contract>));
          stringValue = cursor.getString(cursor.getColumnIndex(<Column name from provider contract>));
          longValue = cursor.getLong(cursor.getColumnIndex(<Column name from provider contract>));
          //..... and so on for all the columns you need to debug

I came across this simple method in the DatabaseUtils class which, for some reason, I never noticed :) It has there been since Api level 1 :O

Super useful for quick and dirty debugging.

public static String dumpCurrentRowToString (Cursor cursor)

So now the code becomes:

while (cursor.moveToNext())
       Log.d(TAG,  DatabaseUtils.dumpCurrentRowToString(cursor));

Makes debugging life so easy!