java


How do I structure my Android Studio project to avoid compilation issues with running the app with unit tests integrated


I am running the latest version of Robolectric, Android Studio, and Gradle.
As the title suggests ever since I worked on Robolectric test cases, I am having a hard time switching back to app and making the android app compile like it used to be.
Every time I try to run the app, I get the following compilation errors:
Error:(35, 23) error: package org.robolectric does not exist
Error:(36, 23) error: package org.robolectric does not exist
Error:(37, 34) error: package org.robolectric.annotation does not exist
Error:(38, 42) error: package org.robolectric.shadows.httpclient does not exist
Error:(44, 2) error: cannot find symbol class Config
Error:(43, 10) error: cannot find symbol class RobolectricTestRunner
Error:(197, 13) error: cannot find symbol variable FakeHttp
Error:(198, 13) error: cannot find symbol variable FakeHttp
Error:(218, 9) error: cannot find symbol variable FakeHttp
Error:(238, 25) error: cannot find symbol variable Robolectric
Error:(250, 25) error: cannot find symbol variable Robolectric
Error:(260, 25) error: cannot find symbol variable Robolectric
Error:(263, 28) error: cannot find symbol variable Robolectric
Error:(275, 38) error: cannot find symbol variable Robolectric
Error:(291, 25) error: cannot find symbol variable Robolectric
Error:(294, 28) error: cannot find symbol variable Robolectric
Error:(305, 32) error: cannot find symbol variable Robolectric
Error:(317, 25) error: cannot find symbol variable Robolectric
Here is my gradle file:
sourceSets{
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
java.exclude {'src/test'}
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
test {
java.srcDirs = ['src/test/java']
}
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile('com.facebook.android:facebook-android-sdk:3.22.0#aar') {
exclude module: 'support-v4'
}
compile files('libs/universal-image-loader-1.9.5.jar')
compile('com.crashlytics.sdk.android:crashlytics:2.5.0#aar') {
transitive = true;
}
compile files('libs/KochavaSDK.jar')
compile files('libs/Parse-1.9.1.jar')
compile files('libs/picasso-2.4.0.jar')
compile files('libs/adobeMobileLibrary-4.11.0.jar')
compile project(':cardview')
compile project(':CropImage')
compile project(':MapboxAndroidSDKNew')
compile project(':android-pdfview')
compile project(':UserVoiceSDK')
compile 'com.google.android.gms:play-services:9.2.1'
compile 'com.google.firebase:firebase-messaging:9.2.1'
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.+'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.+'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v13:23.0.1'
compile('com.googlecode.json-simple:json-simple:1.1.1') {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
compile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.1.2'
testCompile 'org.robolectric:shadows-multidex:3.1.2'
testCompile 'org.robolectric:shadows-httpclient:3.1.2'
}
For the current file structure, I have set the test and com.example.application folders underneath the src folder, which was causing these compilation issues.
I am aware that there are some work arounds for this fix, which consists of excluding the test folder by using 'java { exclude 'src/test'}' under the main sourceset, but I tried that and it didn't seem to work. I also tried to add compile 'org.robolectric:robolectric:3.1.2' and other robolectric libraries needed, but it's gotten too bothersome and now I need to set this project up properly to where I can achieve this desirable effect.
Any input is greatly appreciated.
EDIT: Reworded this question a little better.
Change java.exclude {'src/test'} in the sourceSets block to java.exclude 'test/**'
The original way was calling exclude with a Closure argument, a closure which always returned true, which meant exclude everything.
you may also want to pair it with java.include '**/*.java' (before the exclude) and resources.exclude '**/*.java' in the future. Not sure what your build is like but tasks like processResources may change because of it.

Related Links

Regex - trouble matching the pattern “(cmd: )”
All possible combinations and subsets of an array in java
Jbutton position
java applet audioclip no sound played [duplicate]
How to write a JUnit runner that runs a #BeforeCategory method before a TestCase in a #Category?
Unable to open log4j compressed files using winzip
Why does Temporal not extend Comparable in Java 8 jsr310
SSLProtocolException: handshake alert: unrecognized_name: client-side code workaround?
toByteArray in Python?
multiple processbuilder in a programme
Camera intent crashing my application after few pictures
Return value from method (in order to use that value in other methods inside the same Class)
Android - Wait for volley response to return
Unable to locate the error in fibonacci series using recursion
Coldfusion server is working fine but one site goes down just around two minutes
How to convert from org.apache.commons.net.ftp.FTPFile to String?

Categories

HOME
date
cloud
gremlin
view
iterator
objectgears
octobercms
jsp-tags
setup-deployment
cakephp-2.5
datastax-java-driver
mouse
paradox
android-youtube-api
virtualization
primary-key
session-variables
user-input
zend-framework3
php-7.1
moonmail
scriptcs
opencover
intentfilter
orleans
css-animations
wkwebview
restful-authentication
hammerspoon
crystal-reports-2010
fish
selectedindexchanged
ecto
google-cloud-endpoints-v2
contact-form
scorm2004
google-qpx-express-api
wallpaper
hockeyapp
libvpx
c11
xenforo
pdf-reactor
thin
logfiles
smartcontracts
network-flow
termination
mu
elgg
nativeapplication
directory-structure
sqlclient
flow-control
mediaelement
thrust
clang-static-analyzer
testng-dataprovider
oauth2client
cloudbees
ios8-today-widget
qtableview
ios9.1
generic-programming
iiviewdeckcontroller
proj4js
npapi
nstableviewcell
asp.net-dynamic-data
fpml
aapt
contenttype
c18
.aspxauth
listings
blending
padarn
random-seed
mt
online-compilation
amazon-appstore
spring-portlet-mvc
code-cleanup
nsdatecomponents
pydot
mod-auth
propagation
jmock
mysql-error-1005
infrastructure
gin
asp.net-profiles
ext3
database-management
multi-tier
spec#

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html