I'm starting a simple java test project using sqlite4java and building using java.
I can get the core sqlite4java library downloaded easily, but I'm not sure what the best (any!) way to get gradle to download the native libraries and put them in the right place.
This is my build.gradle file:
apply plugin: 'java'
/* We use Java 1.7 */
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
sourceSets {
main {
java.srcDir 'src'
output.classesDir = 'build/main'
}
test {
java.srcDir 'test'
output.classesDir = 'build/test'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}
But when I run a simple test I get:
TestTest > testSqlite4Basic FAILED
com.almworks.sqlite4java.SQLiteException at TestTest.java:15
Caused by: java.lang.UnsatisfiedLinkError at TestTest.java:15
(I'm building from within IntelliJ, but am using the gradle build options - so I don't think it's ItelliJ stuffing up the class path when running the tests...)
I'm pretty sure the first time I tried to build I got some message about being unable to unpack the libsqlite4java-osx ZIP file, (not surprisingly as maven central says its a dylib file).
What do I need to do to make gradle do the right thing?
ASIDE: I'm able to get the code to run by manually copying the downloaded .dylib from the maven cache into somewhere listed in my java.library.path (Think I used $HOME/Library/Java/Extensions on my mac). But this seems contrary to the whole point of packaging all setup and dependencies in the .gradle file, and wont let me distribute anything easily later.
I suppose, you need to use additional gradle plugin to handle native libraries or make your own specific tasks, to upload and put native libs in right place, in order to they could be found and linked.
At the moment I know only about one such a plugin, hope it can solve your problem https://github.com/cjstehno/gradle-natives
Edit:
The problem with plugin in your case is the fact, that your dependecny "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392" is native lib by itself, not a jar with included native lib as I supposed. So, in that case, you can simply add this dependency in dependencies par of build script, as it'a already done, and then create a custom copy task, to put it in any place you need. Tried to do it with gradle 2.6 on Win7, look like:
task copyNtiveDeps(type: Copy) {
from (configurations.compile+configurations.testCompile) {
include "libsqlite4java-osx-1.0.392.dylib"
}
into "c:\\tmp"
}
In your case, you just need to set "into" property to some path from java.library.path. And the second, you can make this task runs automaticaly with gradle task properties dependsOn and mustRunAfter.
Here's a complete answer based on Stanislav's comments.
apply plugin: 'java'
/* We use Java 1.8 */
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'
repositories { mavenCentral() }
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}
sourceSets {
main {
java.srcDir 'src'
output.classesDir = 'build/main'
}
test {
java.srcDir 'test'
output.classesDir = 'build/test'
}
}
/* Copy the native files */
task copyNativeDeps(type: Copy) {
from (configurations.compile+configurations.testCompile) {
include "*.dylib"
}
into 'build/libs'
}
/* Make sure we setup the tests to actually copy
* the native files and set the paths correctly. */
test {
dependsOn copyNativeDeps
systemProperty "java.library.path", 'build/libs'
}
And example test source to run for it:
import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteStatement;
import org.junit.Test;
import java.io.File;
public class SqliteTest {
#Test public void aTest() throws Exception {
SQLiteConnection db = new SQLiteConnection(new File("/tmp/database"));
db.open(true);
SQLiteStatement st = db.prepare("SELECT name FROM dummy");
try {
while(st.step()) {
System.err.printf("name = %s\n", st.columnString(1));
}
} finally {
st.dispose();
}
}
}
For compiling your own sqlite3 libraries, you can check github android-sqlite3
For the current Android Studio (3.3.1), you can simply add the .so files inside app/build.gradle like below:
android {
...
sourceSets {
main {
jniLibs.srcDirs += ['<your-path>/your-libs']
}
}
...
}
Explanation
jniLibs.srcDirs is the gradle path pointing at jni libraries, operator += denotes the additional jni libraries besides the default ones inside app/src/main/jniLibs as below:
app/
├──libs/
| └── *.jar <-- java libs
├──src/
└── main/
├── AndroidManifest.xml
├── java/
└── jniLibs/ <-- default directory for jni libs, i.e. <ANDROID_ABI>/**.so
And please note that the jni libraries have to be organized according to android ABI, i.e.
your-libs/
├── arm64-v8a/ <-- ARM 64bit
│ └── lib-your-abc.so
├── armeabi-v7a/ <-- ARM 32bit
│ └── lib-your-abc.so
├── x86_64/ <-- Intel 64bit
│ └── lib-your-abc.so
└── x86/ <-- Intel 32bit
└── lib-your-abc.so
I used this method to setup sqlite4java in anroid studio 3.1.4.
First download the library from this link:
https://bitbucket.org/almworks/sqlite4java
The download will contain a zip file containing a folder named android and other .jar , .so and .dll files.
Copy the three folders in android i.e 'armeabi', 'armeabi-v7a' and 'x86' into a folder named 'jniLibs'.
Copy the above folder i.e 'jniLibs' into yourproject/app/src/main/ folder.
Then implement the gradle dependencies for java4sqlite:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "your.project.name"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
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.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:26.1.0'
implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
implementation "com.almworks.sqlite4java:sqlite4java:1.0.392"
implementation "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Related
i saw similar questions, but not found accepted answers.
Problem - i have my own android library with some tiny functions.
My library uses others - f.e. Hawk (no sql database).
My library gradle file:
apply plugin: 'com.android.library'
buildscript {
repositories {
maven { url "https://www.jitpack.io" }
}
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.github.orhanobut:hawk:1.23'
testCompile 'junit:junit:4.12'
}
Library works fine. And if i use it as project inside another project - it work too. But when i generate *.aar file (with gradle -> assembleRelease) and include into separate project - it fails. Project see ONLY MY library's class. Hawk com.orhanobut.hawk package and ofc others (if i will use then) are not visible. So ClassNotFoundException comes.
If i remove
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro'
the result doesnt change.
I tried to add the following line into proguard file (library.pro)
-keep class com.orhanobut.hawk {public *;}
-keep class com.orhanobut.hawk.* {public *;}
Result is the same.
So i have two question:
1 - what should i do to make my main project see my library's third party dependencies?
2 - is is possible to obfuscate my library's code (only mine, not dependencies)?
what should i do to make my main project see my library's third party dependencies?
The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the module.
It means that, if you are importing a aar file using a flatDir repository you have to specify the dependencies also in your project.
You should use a maven repository, private or public, to avoid the issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
Another way to solve the problem of dependencies is to get the jar files of the dependencies you want to use and place them in the libs folder of your module. This will copy all the your dependency jars into your library's jar or aar.
Note that I have emphasized jar because you cannot include aar file in libs folder, gradle still doesn't support aar file inside aar out of the box. There are a few gradle plugins like fataar which solve that problem.
I'm getting an error right after I installed Android studio and Created a simple app.
Steps followed:
Fresh download & installed Android studio.
Created a new project.
When the project loaded, The gradle failed with error:
Error:The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Module Gradle File:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "info.ankitjc.happybirthday"
minSdkVersion 15
targetSdkVersion 25
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'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}
Project Gradle File:
// 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:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I searched for possible solutions here.
Android compile error; Java plugin has been applied, not compatible with android
Gradle error "Java plugin is not compatible with android plugins"
After File > Invalidate Cache/Restart
I think path has not been set as enviorment variable.
check have you declared the java sdk path . set the path as environment variable.
type "javac" in cmd(cmd must have admin previlliage).
If compile is not successfful
1 . Open the jdk location and copy the path of "bin"
2 . Open system properties in control panel.
3 . Advanced system settings -> then select "environment variables"
4 . click on "new"
5 . set variable name as "path" and variable value copied address
then try again
This means the Java plugin is being applied on top of the Android plugin. I don't see anything that stands out in your build files though. Some things to try:
Try doing a clean build from command-line. That will tell you if it's an Android Studio problem.
Create an empty app from a clean slate and see if it builds.
Update Android SDK to latest
Update Android Studio to the latest
Remove Android Studio project files and re-import
Make sure correct Java version is in your path and JAVA_HOME is set correctly
Check if there is any jars in your local lib folder that could be conflicting
Try using beta version of Android plugin compile 'com.android.tools.build:gradle:2.3.0-beta2'
I experienced this error while updating from KAPT to KSP. The docs say to add
id 'org.jetbrains.kotlin.jvm'
to the plugins block, but it appears that adding the Kotlin jvm plugin to the mix was causing my java plugin error. Remove the reference and it compiles. FYI the linked docs are more figurative than literal.
Not sure if this would help, but maybe you can try to explicitly add the java compilation option:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Try removing these two lines of code from your build.gradle
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
and
testCompile 'junit:junit:4.12'
Try this one
Procedure :-
1.right click on project ->Open Module Settings or F4
2.then goto SDK Location
3.after that Check on use embedded jdk(recommended)
you can see image given below
build and run project.
You can also add this in your build.gradle file
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
check if more than one jdk is installed.
I have strange problem and don't know how to resolve it.
I have an interface with default method, like this:
public interface ITest{
default String getText(){
return "ITest";
}
}
and class which implements this interface, like this:
public class TestClasssss implements ITest{
private String text;
}
And I trying to use this class inside my app unit tests project.
So, if I copy this classes inside my android's unit test project it compiles ok and all working as expected, however if this class and interface declared in app source folder, application do not compile and crash with
Error:(30, 10) error: cannot access ITest bad class file: ~\ITest.class
default method found in version 50.0 classfile
Please remove or make sure it appears in the correct subdirectory of the classpath.
So, how could I fix this strange behaviour?
My graddle config looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
}
repositories {
mavenCentral()
mavenLocal()
maven {
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
jcenter()
}
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
apply plugin: 'com.neenbedankt.android-apt'
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
}
}
android {
compileSdkVersion 24
buildToolsVersion '24.0.1'
defaultConfig {
applicationId "io.projectname"
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
testOptions {
unitTests.returnDefaultValues = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
sourceSets {
main {
res.srcDirs =
[
'src/main/res/controls',
'src/main/res/fragments',
'src/main/res/activities',
'src/main/res/views',
'src/main/res'
]
}
}
}
ext {
JUNIT_VERSION = '4.12'
DAGGER_VERSION = '2.2'
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.0.111-beta'
testCompile "org.robolectric:robolectric:3.1.1"
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.roughike:bottom-bar:1.3.4'
compile 'com.aurelhubert:ahbottomnavigation:1.2.3'
compile 'joda-time:joda-time:2.9.4'
compile 'com.annimon:stream:1.0.9'
compile 'com.kyleduo.switchbutton:library:1.4.1'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
compile('eu.davidea:flexible-adapter:5.0.0-SNAPSHOT') {
changing = true
}
compile 'com.github.aakira:expandable-layout:1.5.1#aar'
compile "cn.aigestudio.wheelpicker:WheelPicker:1.1.2"
compile 'net.sf.biweekly:biweekly:0.5.0'
//Dagger dependencies started
compile "com.google.dagger:dagger:$DAGGER_VERSION"
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
provided 'javax.annotation:jsr250-api:1.0'
compile 'javax.inject:javax.inject:1'
testCompile "junit:junit:$JUNIT_VERSION"
testApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
//Dagger dependencies finished
provided 'org.projectlombok:lombok:1.16.10'
}
Make sure that you set up source and target compatibility correctly
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
I was dealing with the same issue, it appears to be some kind of conflict between the compiled java source version accepted on android and the output produced by the code compiled with retrolambda.
The problem raises when you use the default notation for funcional interfaces method.
There's some kind of limited support for that in the retrolambda plugin, quoting the readme of the repo (orfjackal/retrolambda):
retrolambda.defaultMethods
Whether to backport default methods and static methods on interfaces.
LIMITATIONS: All backported interfaces and all classes which implement
them or call their static methods must be backported together,
with one execution of Retrolambda.
Disabled by default. Enable by setting to "true"
So you can try using this in your android module's build.gradle file:
...
android {
...
}
retrolambda {
defaultMethods = true
}
In my case this does not work, but my scenario was quite different than yours. I was having the retrolambda code on one library project and then trying to use that on another app project.
Also I could not get the Jack & Jill compiler to work (theres some kind of java8 support with Jack enabled, see Enable Java 8 Features and the Jack Toolchain at android reference), one cryptic "NullPointer" raises at ./gradlew assembleDebug, so I suggest to avoid jackOptions for now.
Good luck!
I think that you probably need to enable the Jack compiler. You need the following:
Android Studio > 2.1
Default methods are only available if you are targetting API Level 24
Your gradle needs to look something like this:
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
This is the Java 8 Language Feature Page in the Android Documentation
I had this issue after using dex2jar. According to https://gitlab.ow2.org/asm/asm/blob/master/asm/src/main/java/org/objectweb/asm/Opcodes.java#L264, version 50 of the JVM bytecode represents Java 1.6, while my dex file was designed to run on version 52 (1.8). I used another dex2jar utility (which should work on any jar, not just dex) to change the version to 1.8. The source is at https://github.com/pxb1988/dex2jar/blob/2.x/dex-tools/src/main/java/com/googlecode/dex2jar/tools/ClassVersionSwitch.java, the command is d2j-class-version-switch.sh 8 <source jar> <destination jar>.
Are you sure that your gradle build is using jdk1.8+? It seems like the build is trying to utilize a class that was built using 1.8 but being tested during build using a lower version.
try:
gradle clean build
OR
gradlew clean build
If you are using Android Studio try to Clean Project, then Build project. If it doesn't help too, then Click to File -> "Invalidate Caches / Restart". It must help
About a week ago I asked this question
Why is library module android.support.test not visible in add dependency
After much head scratching and project setup morphing I discovered that this app/build.gradle dependency configuration gets me so much further than ever before in that at least the project compiles but does not build when attempting to run:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "com.my.package.name"
minSdkVersion 8
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support.test.espresso:espresso-core:2.1'
compile 'com.android.support.test:testing-support-lib:0.1'
}
Notice compile instead of androidTestCompile on the testing support dependencies. Using androidTestCompile results in the problems noted in my previous question identified above.
Also notice the two srcDirs in the sourceSets declaration. That declaration was automatically set when I manually created the androidTest/java directory. Should this be declared differently?
Also too, getDefaultProguardFile is underlined in the ide with the tool tip "cannot resolve symbol 'getDefaultProguardFile'". In case that is crucial.
This new revelation, switching androidTestCompile to compile, leads me to believe there is something wrong with the way I set up the project structure for the unit test. Here is a picture of my project structure:
I should mention that this project/module structure was automatically created when migrating from Eclipse. By the icon decorations it looks like it at least recognizes my test class as a test class.
But now I am stuck at this error:
Error:Execution failed for task ':mymodule:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\myuser\AppData\Local\Android\sdk\build-tools\22.0.1\dx.bat --dex --no-optimize --output C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\dex\debug --input-list=C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Not sure if this is coming from my project setup or if it is coming from the libraries due to using compile on the support.test libraries.
Any advice on why androidTestCompile doesn't work is appreciated.
Also I just 2 days ago took all the latest updates from SDK Manager to 22.0.1 to no avail.
Another thing I noticed is the only jar I seem to find containing package com.google.common.* is in the espresso library. (com.google.common.* is a dependency of the testing libraries.) But when I inspect the espresso library jar with winzip all of the com.google.common.* package folder structure is there but there are no .class files in the package folders nor subfolders. Just mentioning that in case it lends a clue. Perhaps it is somewhere else also that I had missed.
Thanks!
Edit:
my source sets now look like this
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}
and dependencies
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
I still get can't resolve android.support.test in my test class (test is colored red on these lines):
import android.support.test.InstrumentationRegistry;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
shouldn't I see a testing-support, espresso-core and espresso-contrib in my External libraries?
Below is my working Espresso gradle setup based on Android Studio 1.1.0 + Espresso 2.0 + Support Library v11 + gradle environment. It looks likely that you missed androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0' library.
dependencies {
// ...
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
android {
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
packagingOptions {
//...
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['res']
}
}
}
About the directory, yes. just maintain androidTest and main under src folder. But, your setting does not make IDE to recognize androidTest folder as a test project folder. For that, two projects with same package name causes multi dex error I think. Use androidTestCompile to espresso-related lines in gradle dependencies and clean/rebuild project.
Note that you have to set all referenced projects gradle to same testInstrumentationRunner.
I also spent times to make it work. I wrote Korean tutorial to setup and run Espresso. Sorry about the language but screenshots might be helpful or you can try Google translate to read it.
EDIT. APPENDED BELOW
I thought why your IDE did not show green color in androidTest folder (means Test Project), and found your gradle setup seems to be wrong.
The line
{ main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } make IDE to recognize androidTest is a Project folder not Test Project folder. remove second one. androidTest will be automatically recognized as a Test Project.
Since Android Studio is going to be default IDE for Android Development, I decided to migrate my existing project into Android-studio. The project stucture seems different and the hierarchy of folders in my Project is as follows:
Complete Project
->.idea
-> build
-> Facebook SDK
-> MainProject
-> ... (Other Libraries)
build.gradle
local.properties
settings.gradle
...
External Libraries
-> Android API 8 Platform
-> Android API 18 Platform
-> Android API 19 Platform
-> 1.7 Java
-> support-v4-19.1.0
My MainProject has a libs folder which contains different jars used within the project. It surprisingly does not contain the android-support-v4 jar which was present in my eclipse project. So, it seems that the external Libraries folder at the root must take care of it.
But after import, when I tried to compile the project started throwing "Symbol not found error" for Certain Classes all relating to android support library.
For Eg: The auto complete in Android Studio gives me suggestion for NotificaitonCompat from android.support.v4.app.NotificationCompat, but when I try to compile my Project Module it says
Error:(17, 30) error: cannot find symbol class NotificationCompat Error:Execution failed for task ':app:compileDebugJava'.> Compilation failed; see the compiler error output for details.
This happens in many other classes too for the same support library. I tried to insert a jar and changed the same in the build.gradle for the MainProject, but the error persists.
I even tried restarting and building the project again, but nothing changed.
EDIT:
I am attaching the Gradle file inside the MainProject
build.gradle in MainProject Module
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "package.app"
minSdkVersion 8
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
dependencies {
compile project(':facebookSDK')
compile project(':library')
compile project(':volley')
compile 'com.google.android.gms:play-services:+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'com.android.support:support-v4:19.1.0'
compile files('libs/FlurryAnalytics_3.3.3.jar')
compile files('libs/universal-image-loader-1.8.4.jar')
....
}
This part of your build file:
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
is telling the build system to ignore support-v4, which is why it isn't compiling. Remove that.
In your build file, you have this, which is the correct way to include support:
compile 'com.android.support:support-v4:19.1.0'
If you have the support library jar file in the libs directory of any of your modules, remove it and make sure you refer to it this way -- if you include the library as a jar, you're likely to run into a problem where the jar is included multiple times which will result in a dex error.
If you have jar files in a 'libs' directory, you can specify it in you build.gradle file :
dependencies{
compile fileTree(dir: 'libs', include: ['*.jar']
compile 'com.android.support:support-v4:21.0.3' //to automatically add up-to-date support lib
}
Hope this helps!