Generate JAR from Android Library with Dependencies, NoClassDefFound - android

I have an Android Library module that I need to ship as a JAR. The library has no resources, so I think it's valid to extract the classes.jar from the generated aar file. However, the classes in my library have remote dependencies, so when I try to import classes.jar and instantiate a class defined there, I get a NoClassDefFound problem.
I have read about using the transitive keyword (How do I ship an Android Library (aar) with remote dependencies (gradle)?), but I want to allow users to just drop in JARs to accommodate those not using gradle, so my problem is slightly different.
I can browse to my local gradle cache and find all my library's dependencies (as JARs), so I could pull all these and have users drop them in, but it seems inelegant. Is there a way I can either a) have the jars of the remote dependencies output to a folder in my build directory at library compile time or b) have gradle export all transitive dependencies into my aar?
My gradle.build:
apply plugin: 'android-library'
apply plugin: 'maven'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 16
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:appcompat-v7:20.0.0'
// my two cloud endpoints libraries
compile ([group: 'com.appspot.super_endpoint_999', name: 'hello', version: 'v1-1.18.0-rc-SNAPSHOT'])
compile files('libs/advancedhello-v1-1.19.0-SNAPSHOT.jar')
compile 'com.google.android.gms:play-services:4.2.+'
compile('com.google.api-client:google-api-client:1.17.0-rc') {
exclude(group: 'xpp3', module: 'xpp3')
exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
exclude(group: 'junit', module: 'junit')
exclude(group: 'com.google.android', module: 'android')
}
compile('com.google.api-client:google-api-client-android:1.17.0-rc') {
exclude(group: 'com.google.android.google-play-services', module: 'google-play-services')
}
compile('com.google.http-client:google-http-client-android:1.17.0-rc') {
exclude(group: 'com.google.android', module: 'android')
}
compile 'com.google.guava:guava:14.0.+'
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
}

Related

Build Failed: Program Type Already Present

I have searched online about this error and the issue always seems to end up being some sort of dependency conflict. I think I am supposed to add exclude after certain dependencies, but I am unsure which one(s). It's also unclear based on the error which dependency I should actually exclude; all I know is that the group is probably com.android.support...
Here's what I've tried:
Adding multiDexEnabled true to my defaultConfig block in build.gradle.
Cleaning the project in Android Studio.
Manually deleting (from the file system) the entire .gradle directory.
I've made sure compileSdkVersion and targetSdkVersion are the same.
I've made sure all my Android dependencies use the same version (namely 26.1.0).
Despite all this, I still get this error when I build:
Program type already present: android.support.compat.R$bool
and from the Java compiler:
Caused by: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
F:\ExampleProject\app\build\intermediates\transforms\dexBuilder\debug\115,
F:\ExampleProject\app\build\intermediates\transforms\externalLibsDexMerger\debug\0
Here is my module's build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.myapp.exampleproject"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-compat:26.1.0'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.1.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.1.0'
}
and lastly, my project's build.gradle file:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Any help in rectifying this would be greatly appreciated.
This is a problem with Glide 4.1.0. Use the version 4.1.1 instead which fix the bug by remove the R*.class file from their dependencies. (source)
This is probably because of Glide library. Try to exclude the support library from it with:
// add support-fragment removed from Glide.
implementation "com.android.support:support-fragment:26.1.0"
implementation ('com.github.bumptech.glide:glide:4.1.0') {
exclude group: 'com.android.support'
exclude module: 'support-fragment'
exclude module: 'appcompat-v7'
}
You can see the support library inside Glide at Glide build.gradle
Solution:
implementation ('com.github.bumptech.glide:glide:4.1.0') {
exclude group: 'com.android.support'
exclude module: 'support-fragment'
exclude module: 'appcompat-v7'
}

plugin with id 'com.android.application' not found Android studio

I'm getting "plugin with id 'com.android.application' not found" error in my app after adding one library project in my app, but now I removed that library project from my app but still same error is persist. I googled a lot and tried almost all the solution but yet unable to resolve this error. For more information, please check the following "app module's build.gradle"--
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
applicationId "com.lemo.wikinews"
minSdkVersion 15
targetSdkVersion 23
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
compile project(':easyAndroidAnimationsLibrary')
compile project(':liveSdk')
compile project(':main')
compile project(':zbarSample')
compile project(':stickyHeaderLibrary')
compile project(':starBar')
compile project(':facebookSDK')
compile project(':library')
compile project(':ffmpeg4android_lib')
compile project(':socialNetworkingLib')
compile 'com.google.android.gms:play-services:9.6.1'
compile files('libs/nmdp_speech_kit.jar')
compile files('libs/AndroidSwipeLayout-v1.1.8.jar')
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
compile files('libs/CWAC-SackOfViewsAdapter.jar')
compile files('libs/devsmartandroid.jar')
compile group: 'com.google.api-client', name: 'google-api-client', version: '1.6.0-beta'
compile files('libs/google-api-services--v1-1.3.0-beta.jar')
compile files('libs/httpclient-4.4.jar')
compile group: 'oauth.signpost', name: 'signpost-commonshttp4', version: '1.2'
compile group: 'oauth.signpost', name: 'signpost-core', version: '1.2.1.2'
compile group: 'oauth.signpost', name: 'signpost-jetty6', version: '1.2.1.1'
compile 'com.android.support:multidex:1.0.0'
'4.4-alpha1'
compile 'com.squareup.picasso:picasso:2.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'org.apache.httpcomponents:httpclient:4.5'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpmime:4.3.5'
version: '4.3.5.1'
compile('org.apache.httpcomponents:httpmime:4.3')
{
exclude module: "httpclient"
}
}
And below is my top level 'builg.gradle'--
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
allprojects {
repositories {
jcenter()
}
}
Also please let me know if I can provide more information to understand the problem. Thank you.
Bellow is my way to resolve a import problem,the project use a old studio plugin. Forgive my poor English.
Edit your top moudle build.gradle ,edit gradle plugin to your studio's have . then maybe show this,
Error:Minimum supported Gradle version is 2.14.1. Current version is 2.2.1. If using the gradle wrapper, try editing the distributionUrl in C:\Users\Administrator\Desktop\SelectCityDome\gradle\wrapper\gradle-wrapper.properties to gradle-2.14.1-all.zip
Fix Gradle wrapper and re-import project
Gradle settings follow the step fix it.
JUST SEE THIS PICTURE

Dependency httpclient - AndroidStudio

my run and debug buttons in my Android Studio are disabled.
I have seen many answers about this but the solution are not correct in my case.
This is my error in my gradle console:
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for debug as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for release as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
Incremental java compilation is an incubating feature.
This is my build.gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "myProjectPackageName"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
maven {
url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
}
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.0'
// Zxing libraries Scan
compile 'com.embarkmobile:zxing-android-minimal:2.0.0#aar'
compile 'com.embarkmobile:zxing-android-integration:2.0.0#aar'
compile 'com.google.zxing:core:3.0.1'
//Google api books
compile 'com.google.apis:google-api-services-books:v1-rev80-1.21.0'
compile 'com.google.android.gms:play-services-appindexing:9.4.0'
// Dependency for Google Sign-In
compile 'com.google.android.gms:play-services-plus:9.4.0'
compile 'com.google.android.gms:play-services-auth:9.4.0'
//Facebook Login
compile 'com.facebook.android:facebook-android-sdk:4.13.2'
// Dependency for Firebase
compile 'com.google.firebase:firebase-messaging:9.4.0'
compile 'com.google.firebase:firebase-database:9.4.0'
compile 'com.google.firebase:firebase-storage:9.4.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
}
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
}
apply plugin: 'com.google.gms.google-services'
Thanks a lot for the help
You need to exclude dependencies inside build.gradle files.
compile('com.google.apis:google-api-services-books:v1-rev80-1.21.0') {
exclude module: 'httpclient'
exclude group: 'org.apache.httpcomponents'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
Try to exclude it from all configurations.
configurations {
compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
}
Restart studio and then check again. Or either select app module from drop down menu beside Run button.

Android Studio not recognizes Espresso imports

I am trying to write a test case using Espresso.
I am using Android Studio 1.5.1 (Stable channel), Gradle plugin 1.5, Gradle 2.7.
The problem is that Android Studio doesn't recognize any import related to Espresso (and not only)
So, I tried to clean the project, rebuild, invalidate cache and restart, but nothing.
I added these dependencies in my app module:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
I added the runner in defaultConfig:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
I created the test suite class under androidTest folder:
What am I doing wrong please?
UPDATE
Here is (part) of my build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 5
versionName '1.4'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
//my build type configs
}
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
}
lintOptions {
disable 'InvalidPackage'
}
dexOptions {
incremental true
preDexLibraries = false
jumboMode = false
javaMaxHeapSize "2g"
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.viewpagerindicator:library:2.4.1'
compile project(':libraries:RITracking')
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-annotations:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:gridlayout-v7:23.1.1'
compile 'com.google.code.gson:gson:2.5'
compile 'com.google.android.gms:play-services-plus:8.4.0'
compile 'com.google.android.gms:play-services-base:8.4.0'
compile 'de.greenrobot:eventbus:2.4.1'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.7.2'
compile 'com.facebook.android:facebook-android-sdk:4.9.0'
compile 'com.cocosw:bottomsheet:1.2.0#aar' //Bottom Sheet that implement material design used for ShareDialog
compile 'com.github.bumptech.glide:glide:3.6.1' //Glide library
compile 'com.googlecode.libphonenumber:libphonenumber:7.2.3' //Library used to parse/merge phones number to E164 format
compile 'me.leolin:ShortcutBadger:1.1.3#aar' //Used to show badge on application icon, library is optimized to work on most of devices
compile 'com.stripe:stripe-android:1.0.3' //Stripe payment gateway, used to integrate credit card payment
provided 'org.projectlombok:lombok:1.16.6'
apt "org.projectlombok:lombok:1.16.6"
compile 'com.jakewharton:butterknife:7.0.1'
apt "com.jakewharton:butterknife:7.0.1"
compile 'de.greenrobot:greendao:2.1.0' //Green Dao library is ORM implementation for Android SQL lite
compile files('libs/libammsdk.jar')
compile files('libs/apptimize-android-2.9.1.jar')
testCompile 'junit:junit:4.12'
androidTestCompile "com.android.support:support-annotations:23.1.1"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
}
Perhaps your Build Variant is on "release" mode. you have to change it to debug.
UPDATE 30/11/2016
Just like #Jaymes Bearden said in comment below.
Use testBuildType. Android Studio 2.2, gradle 2.2.2
android {
testBuildType "yourBuildType"
}
OLD ANSWER
I found not solution but some source of problem. In my project I have, a lot of BuildTypes. Especially more than one debug build.
Everything works only in default debug build type. I think that this is some internal Android Studio error.
My AS version - 2.1
You need to use debug build variant. With other build options espresso will not be recongnised.
I'm pretty sure, that you're something missing in your configuration. compare your build.gradle with mine below
def ASVersion = '23.1.1'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
dataBinding {
enabled = true
}
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
applicationId "com.piotr.awesome"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
androidTestCompile "com.android.support:support-annotations:$ASVersion"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile 'com.android.support.test:runner:0.4.1'
compile "com.android.support:appcompat-v7:$ASVersion"
compile "com.android.support:support-v4:$ASVersion"
compile "com.android.support:design:$ASVersion"
}
Also choose File -> Invalidate cache/restart and try first option.
Hope it help
In Android Studio:
Open Run menu -> Edit Configurations -
Add a new Android Tests configuration
- Choose a module
Add a specific instrumentation runner:
android.support.test.runner.AndroidJUnitRunner
Also try to update the Android Support Library from SDK Manager.
Hope this helped.
You can also try this topic: link
Good luck!
I had the same problem. Here is how i fixed it:
Go the src directory of your project in finder/explorer, select the directory containing your test.
Rename the directory with the prefix "androidTest" followed by the name of the flavor you are trying to test.
Open Android Studio, Invalidate Caches and Restart.

Roboelectric and Android Studio

I've been trying to do it for a few days, with no result. I need to set up Robolectric in Android Studio (0.8.9, latest version).
I followed different tutorials Android Unit and Integration testing, Roboelectric installation for Unit testing, Android Gradle app with Roboelectric, How to run Roboelectric JUnit tests but always got some kind of error.
So I created module specially for testing:
Kedzoh (Project) build.gradle:
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
}
}
allprojects {
repositories {
jcenter()
}
}
app build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId 'com.dev.kedzoh'
minSdkVersion 9
targetSdkVersion 19
versionCode 14
versionName '1.6.7'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
}
kedzoh-tests build.gradle:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.2'
}
At this point I cannot import Robolectric classes, it gives me error. When I add apply plugin: 'robolectric' to kedzoh-tests build.gradle, it asks for 'android' plugin. After I add it, it complains there is no Manifest and fails to build.
I'm not sure that this is the right configuration, since it never worked really. Could anyone give some advice how to set Robolectric in Android Studio, please?
EDIT:
I tried the answer below, but still stuck with "Class not found" error:
There already different project templates for robolectric. Did you try https://github.com/nenick/android-gradle-template ?
I think you maybe making your life more difficult and complicated by creating your test module outside of the main 'app' module. Most of the tutorials I have seen have an androidTest folder in the app module that can contain your Robolectric tests. All of the older tutorials based around Eclipse I have seen, seem to instruct us to create test modules separate to the main project.
If I were using Robolectric I would setup the project like I have created for you https://github.com/marcthomas2013/Kedzoh. I would use the androidTest folder to put my tests and configure the gradle files as follows.
Note that the dependencies for the tests are included using the androidTestCompile declaration and application dependencies are declared using just compile.
Another item in this gradle file is the robolectric section where it is including and excluding classes in the androidTest folder. Anything that isn't in the espresso folder will be run using the gradlew test target and everything including the espresso folder will be run when calling gradlew connectedAndroidTest target.
app gradle file
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 18
targetSdkVersion 18
versionCode 2
versionName "1.0.0-SNAPSHOT"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
}
}
sourceSets {
androidTest {
setRoot('src/androidTest')
}
}
}
robolectric {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
repositories {
mavenCentral()
}
compile 'com.sothree.slidinguppanel:library:2.0.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
// Espresso
androidTestCompile files('libs/espresso-1.1.jar')
androidTestCompile files('libs/testrunner-1.1.jar')
androidTestCompile files('libs/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1'
androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
project build file
Here we include the robolectric class path so that we can use the robolectric gradle commands in the config file above.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
Can you explain what errors that you are having as there could be many possible errors based on how you are working, also is there a particular reason for creating a separate test module.
The project that I have provided is based on the deck-gradle setup (https://github.com/robolectric/deckard-gradle) there are a number of tips in here about JUnit conflicts etc. that you could be having trouble with.
Once you have got this far in order to be able to debug the unit test in the application you will have to manually tweak the classpath when launching the test (Not Pretty at all!), based on the information in this article https://github.com/codepath/android_guides/wiki/Robolectric-Installation-for-Unit-Testing
You will need to run your unit test, wait for it to fail, copy the -classpath parameter and it's value (It will be quite long and starts and finishes with ") and copy this into your VM Options in your run configuration, then at the end of that classpath add a ; or a : depending on your OS path delimeter and add the path to your test classes which will be something like <ABSOLUTE_PATH_TO_PROJECT>/build/test-classes this will add your test classes to the class path and then Android Studio should be able to run them.
This is also assuming that the steps to run the gradle testClasses has been done from the article http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/

Categories

Resources