Here is a quick tip and an easy way to generate material color palettes for your app quickly:
http://www.materialpalette.com/
Just pick you two colors: primary and accent, and your download the palette. Pick “xml” to get the Android resource file.
Here is a quick tip and an easy way to generate material color palettes for your app quickly:
http://www.materialpalette.com/
Just pick you two colors: primary and accent, and your download the palette. Pick “xml” to get the Android resource file.
Since Android Studio became available there was some amount of confusion on how configure unit tests to use Robolectric, how to get code coverage working. There are different solutions, but I’ll explain mine.
So, I’ll be using:
Here I’ll create android library as my main project and java project for my unit tests. Steps are the same for application project (with some small differences).
Let’s start with the library project. Right now it’s now very straightforward how to create a library only project (I’m sure it will get better over time, as Android Studio matures). So, here is one way:
it should look something like this:
But it’s still an application, even though it’s called “lib”. There are few more steps:
apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
Now we got the library.
As I said before, I prefer to keep unit test in the separate project. To do this, we’ll need to add a separate Java project where all our tests will go. So, add a new module to the project, select “Java Library” from the “More Modules”. Give it a name, like “tests”, and click “Finish”. Once the project is created, add a folder for actual tests. Full path should be: $MODULE_DIR$/src/test/java/$PACKAGE, for instance “tests/src/test/java/com.example.demolib”. I just change the name of the “main” folder in tests project to “test”.
Let’s start with configuring dependencies and Robolectric. There are few steps that need to be taken here:
build.gradle at this point should look something like this:
evaluationDependsOn(':lib')
apply plugin: 'java'
def testIncludes = [
'**/*Tests.class'
]
def libraryModule = project(':lib')
def firstVariant = libraryModule.android.libraryVariants.toList().first()
dependencies {
compile libraryModule;
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile firstVariant.javaCompile.classpath
testCompile firstVariant.javaCompile.outputs.files
testCompile files(libraryModule.plugins.findPlugin("com.android.library").getBootClasspath())
}
tasks.withType(Test) {
scanForTestClasses = false
includes = testIncludes
}
Go ahead a add your tests now.
We got the Robolectric tests running, we can now add Jacoco code coverage report. To do so requires just a ‘simple’ change in build.gradle (and I put ‘’ around simple, as it took me some time to find a solution, and it’s not mine: http://minimalbible.github.io/android-and-jacoco/. Read the article, it’s good, and I’m not going to repeat it here).
An updated version of the build.gradle should look as following:
evaluationDependsOn(':lib')
apply plugin: 'java'
apply plugin: 'jacoco'
def testIncludes = [
'**/*Tests.class'
]
def jacocoExcludes = [
'android/**',
'**/*$$*'
]
def libraryModule = project(':lib')
def firstVariant = libraryModule.android.libraryVariants.toList().first()
dependencies {
compile libraryModule;
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile firstVariant.javaCompile.classpath
testCompile firstVariant.javaCompile.outputs.files
testCompile files(libraryModule.plugins.findPlugin("com.android.library").getBootClasspath())
}
def buildExcludeTree(path, excludes) {
def tree = fileTree(path).exclude(excludes)
tree
}
jacocoTestReport {
doFirst {
// First we build a list of our base directories
def fileList = new ArrayList()
def outputsList = firstVariant.javaCompile.outputs.files
outputsList.each { fileList.add(it.absolutePath.toString()) }
// And build a fileTree from those
def outputTree = fileList.inject { tree1, tree2 ->
buildExcludeTree(tree1, jacocoExcludes) +
buildExcludeTree(tree2, jacocoExcludes)
}
// And finally tell Jacoco to only include said files in the report
// For whatever reason, firstVariant.javaCompile.exclude(jacocoExcludes) doesn't work...
classDirectories = outputTree
sourceDirectories = files(libraryModule.android.sourceSets.main.java.srcDirs)
}
reports {
xml.enabled true
}
}
tasks.withType(Test) {
scanForTestClasses = false
includes = testIncludes
}
To run the test and code coverage report you can run the following Gradle command from Terminal window:
gradlew test jacocoTestReport
Once it’s done, you should have a report in ‘build’ folder of your ‘tests’ project (under “tests\build\reports\jacoco\test”).
Of course you can run tests from command line (from Terminal for instance) with ‘gradlew test’ command. But if you want to run them from Android Studio UI, you can’t just select ‘Run All Tests’ in context menu and run them. There is some configuration that needs to be done before that. Open up “Run/Debug Configuration” window and add the following:
After all the changed are saved, you can the configuration:
Sample project can be found on the GitHub: https://github.com/iuriioapps/AndroidStudio_Robolectric_Jacoco.