Can i setup Robolectric in its own module - android

Is is possible to setup robolectric in its own module and add it as a dependency to my project? I can add it to my project module fine but id prefer if it was in its own module. Ive created a javaLibrary module and added the following code to the gradle build script
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
//Prior to AS 0.5.9: classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.0'
//previous plugin >> classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
}
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}
apply plugin: 'android'
apply plugin: 'android-test' //previously >> apply plugin: 'robolectric'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.irishtimes.newsapp.tests"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest.setRoot('src/tests') //note that this is androidTest instead of instrumentTest
}
}
dependencies {
androidTestCompile 'junit:junit:4.10'
//include the stable robolectric 2.3 library
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
//only include files that are suffixed with Test.class and set the max heap size
androidTest {
include '**/*Test.class'
maxHeapSize = "2048m"
}
Ive also added a blank manifest file and a dummy test in a src/tests/java. In my previous setup when i run ./gradlew test, my tests would run and id get feedback. Now when i run the same command my build script runs but it does not pick up my tests at all. If anyone has any reference to tutorials or advice or a quick solution that would be great. Thanks!

Yes its possible with a different plugin. Plugin supported by robolectric do not support this.
Plugin can be found here https://github.com/novoda/gradle-android-test-plugin
A ready to use example can be found here https://github.com/nenick/android-gradle-template

If you want to run tests from a separate folder, but not necessarily a separate module, I have had some success with the Android Studio Unit Test Plugin along with this gradle plugin.

Related

Android Studio Gradle does not download maven depedencies

I am quite new to Android development and trying to write some test code. I have downloaded Android Studio 1.3 version and created testapp. I am trying to add com.facebook.android:facebook-android-sdk:4.1.0 depedency but somehow it's not getting downloaded.
Here is Project level build.gradle file.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}}
Here is Module level build.gradle file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.testapp"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
compile 'com.android.support:appcompat-v7:22.2.1'
}
After reading other posts on similar topics I also confirmed that Gradle > Global Gradle settings > Offline work is not checked. Still somehow facebook depedency is not getting downloaded.
Any suggestions what could be the issue? Appreciate any pointers.
Thanks.
In your build.gradle you have to add
repositories {
jcenter()
}
It this way gradle knows where are the dependencies to download.
It is somenthing different from the repositories inside the buildscript block.
Otherwise you can add in the top-level build.gradle file:
allprojects {
repositories {
jcenter()
}
}
In this case, the repository block is valid for all modules in the project.
Could you post the error AndroidStudio is showing? (check on the Gradle Console, on the Run section and Android Monitor section)
One possible option that comes to my mind is that you don't have the lastest Android Support repository / Google repository installed: Check for updates on your Android SDK Manager.

Building app with library module cannot find AndroidManifest.xml

I want to build an Android App with AndroidAnnotations.
Here is a simplified version on GitHub which fails to build/pre-process: https://github.com/aiQon/androidannotationsexample
The app is composed of a main app module and a library module.
Both use AndroidAnnotations (AA). The lib module has a workaround to find the AndroidManifest.xml in debug builds (it refers to the release manifest, because gradle does not build the debug variant for libs).
However, the manifest is found for the library module but not for the main app.
The gradle file of the main app is:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.0.1"
defaultConfig {
applicationId "de.stelle_beratung.androidannotationslibraryexample"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apt {
arguments {
androidManifestFile variant.outputs.processResources.manifestFile
resourcePackageName android.defaultConfig.applicationId
}
}
dependencies {
compile project(":mylibrary")
apt "org.androidannotations:androidannotations:3.2+"
compile 'org.androidannotations:androidannotations-api:3.2+'
}
Gradle complains with:
http://pastebin.com/QZtnHcZD
Could someone please point me in the right direction on how to build this simplified project to have a controller Bean in the lib module and reference it successfully in the main app activity by using AA?
I appreciate any help.
Since android gradle plugin 14.4, the following line does not work anymore:
variant.outputs.processResources.manifestFile orvariant.processResources.manifestFile
You have to use this one instead:
variant.outputs[0].processResources.manifestFile
Please note if you have multiple outputs (when using splits), you may want to use another output (other index than zero).
Ok I got it. Was using Android Studio from the Canary Channel. Going back to Beta solved the issue because I could use android gradle plugin 0.12.2. This solved everything.

Gradle doesn't find dependency - Android Studio

Gradle no longer finds dependencies declared in the build.gradle. Everything was working fine and today when I open Android Studio I get an error :
Error:Failed to find: com.readystatesoftware.systembartint:systembartint:1.0.3
The library referenced is SystemBarTint. I had the library work previously using the same build.gradle file but now it no longer works.
My app/build.gradle looks like this :
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.adnan.systembartintdemo"
minSdkVersion 19
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
}
My root/build.gradle looks like this :
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
Any ideas what I am doing wrong here? Thanks !
Edit : Tried importing a project which I work on daily at my work, it fails to find any dependencies in that project either. Tried re installing Android Studio, still the same error
Ok finally got this fixed.The problem was weird, for some reason it was trying to pick up a local offline copy of the dependency, I went to Settings -> Compiler -> Gradle -> Enabled offline Work and disabled it again. Save and Apply changes.
And now it works !

Android Studio showing compiler errors for no reason

This started happening once I imported jcodec (http://jcodec.org/) for Android into the Studio as a module. The actual project itself compiles fine and works as expected on both devices and emulators but the compiler marks everything involving non-custom libraries as errors. Operations involving custom classes within the project does not have the compiler errors.
I tried the following:
1) Invalidate/Cache restart
2) Deleted the .idea folder and .iml files and reimported the whole project
3) Added the library via Project Structure and Synced Project with Gradle files
The worst part is that this inline compiler error happens for all projects in Android Studio (new and old) now.
I am using Android Studio version 0.5.4.
Is there anything I can do to fix this? Does it have anything to do with jcodec?
jcodec build.gradle files
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 16
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 11
targetSdkVersion 16
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
}
main project build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 16
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 11
targetSdkVersion 16
}
}
dependencies {
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'com.android.support:support-v4:19.1.0'
compile files('libs/android-async-http-1.4.4.jar')
compile project(':jcodec')
}

setup actionbar sherlock in android studio 0.4.2

I am developing an android project by using actionbrsherlock library. after adding the actionbarsherlock library in android studio , my directory structure is
MYAPPLICATION
- app
- gradle
- libraries
- actionbarsherlock (library project)
build.gradle
setting.gradle
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
compile project(":libraries:actionbarsherlock")
}
}
allprojects {
repositories {
mavenCentral()
}
}
setting.build
include ':app'
include ':libraries:actionbarsherlock'
after running following error occured,
A problem occurred evaluating root project 'MyApplication'. > No
signature of method:
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile()
is applicable for argument types:
(org.gradle.api.internal.project.DefaultProject_Decorated) values:
[project ':libraries:actionbarsherlock'] Possible solutions:
module(java.lang.Object)
UPDATE:
I upgraded my android studio to 0.4.4. and change the build.gradle file. one can find the whole setup on github https://github.com/manishpathak99/AndroidStudio.
First I will strongly recommend you to upgrade your studio to version 0.4.3 because there were some dependencies related issues in 0.4.2 release .
Answer :
You are adding module dependencies at wrong place in build.gradle file . Your module's build.gradle file should look like this :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
// Your all module dependencies should be defined here
compile project(":libraries:actionbarsherlock")
}
Recommended Solution :
ActionbarSherlock library has its maven dependency, In such cases no need to download the whole library and add it as as module in your project, instead add the maven gradle dependency in your module's build.gradle file like
dependencies {
compile 'com.actionbarsherlock:actionbarsherlock-i18n:4.4.0#aar'
}
That's it.

Categories

Resources