Android command-line: gradle and testing

For android projects, some engineers use Android Studio (new), some use Eclipse with ADT (old), few like me still savor command line, this blog post is about handling (building, installing and testing) android projects from command line.

  1. To create android project
    $ android create project --target 4 --name TestAndroidApp --path ./test_android_app --activity Main --package net.ashishb.TestAndroidApp --gradle --gradle-version 1.0.+ 
  2. After changing to directory test_android_app (cd test_android_app), fix a bug
    # Replace "runProguard" with "minifyEnabled" in build.gradle
    $ sed -i '' 's/runProguard/minifyEnabled/' build.gradle

     

  3. Some useful gradle commands
    $ gradle tasks  # Lists all tasks.
    $ gradle assembleDebug  # Assemble debug build
    $ gradle installDebug  # Install debug build
    $ gradle assembleRelease  # Install release build
  4. The code will be in src directory, eg. for the Main activity, code is in src/main/java/net/ashishb/TestAndroidApp/Main.java and test is in src/androidTest/java/net/ashishb/TestAndroidApp/MainTest.java
  5. For testing, modify the Main class and add square method,
    public static int square(int x) {
          return x * x;
    }
    

    And in MainTest

    public void testSquare() {  // Tests must start with test prefix.
       this.assertEquals(100, Main.square(10));
    }
  6. Modify build.gradle to add a testing config
    android {
        ...
    
        defaultConfig {
          testApplicationId "net.ashishb.TestAndroidApp.test"
          testInstrumentationRunner "android.test.InstrumentationTestRunner"
          testHandleProfiling true
          testFunctionalTest true
        }
    
        ...
    }

     

  7. Run the test(s)
    $ gradle installDebug installDebugTest && adb shell am instrument -w -e class net.ashishb.TestAndroidApp.MainTest#testSquare net.ashishb.TestAndroidApp.test/android.test  # Runs the testSquare test.
    
    $ gradle installDebug installDebugTest && adb shell am instrument -w  net.ashishb.TestAndroidApp.test/android.test.InstrumentationTestRunner  # Runs all tests.
  8. Complete code can be seen at https://github.com/ashishb/android_gradle_demo

Android, Gradle and compile-time only dependencies

Android plugin for Gradle does not support Java-style compile time only dependencies.

After spending a few hours on trying to build android app targeted for Amazon SDK (without using Amazon’s Android specific plugin but just their jar stubs for maps, ADM and Home widget), I finally found that the one way to support compile-time dependencies is following.

For application project

configurations {
    provided
}

dependencies {
    // ...
    provided fileTree(dir: "${project.rootDir}/path_to_libs_dir", include: '*.jar')
}

// Android's version of sourceSets.main.compileClasspath
android.applicationVariants.each { variant ->
    variant.javaCompile.classpath += configurations.provided
}

For the library project

configurations {
    provided
}

dependencies {
    // ...
    compile fileTree(dir: 'libs', include: '*.jar')
    provided fileTree(dir: "${project.rootDir}/patch_to_libs_dir", include: '*.jar')
}

android.libraryVariants.all { variant ->
    // Exclude the jar files from making its way into the final apk.
    // Irrespective of what the path_to_libs_dir is the final jar files end up in libs dir.
    variant.packageLibrary.exclude('libs/lib1.jar')
    variant.packageLibrary.exclude('libs/lib2.jar')
    // ...
}

References

  1. https://stackoverflow.com/questions/16613722/gradle-configurations-not-working-as-expected-in-new-android-build-system
  2. http://stackoverflow.com/a/24157721