I just can't seem to get a test file to find Junit4. I've been trying forever and I'm just frustrated. It says it can't find symbol junit under org. Everything I've googled (for days), and even the android docs say this is how to do it. What am I doing wrong?
Here is my test class
import org.junit.Test;
import android.app.Application;
public class ApplicationTest {
// #Test
public ApplicationTest() {
}
}
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
//buildToolsVersion "21.1.2"
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "stuff.morestuffs"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
compile files('libs/gson-2.3.1.jar')
testCompile 'junit:junit:4.12'
}
heres my project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
I'm using Android Studio 1.4
For unit tests (JVM tests) the root source directory should be test, not androidTest as for Android instrument tests.
Under "Build Variants", I had "Android Instrument Tests" selected. When I changed this to "Unit Tests", Android Studio correctly recognized my file.
I solved this problem when I added following dependency to build.gradle:
androidTestCompile 'junit:junit:4.12'
If you have to test more than some separate methods and you want to test your Activity or Fragments you have to add it.
Related
I'm trying to use MaterialDialog in my project, but this library doesn't support my Right to Left language and I must edit that. After downloading the project and importing it into current project I get this error when building my project:
Error:(2, 0) Plugin with id 'com.novoda.bintray-release' not found.
After many searches on the web I find this link to resolve this. But that doesn't work for my project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
/* I'm adding this below line */
classpath 'com.novoda:bintray-release:0.2.7'
}
}
allprojects {
repositories {
jcenter()
}
}
If you have downloaded the actual library and have imported it to Android studio and modified it then you want to add the dependency in your gradle.build (Module:app). The code you are showing is from gradle.build (Project: NameOfYourProject) so I would delete that line you added there.
Go to the build.gradle (Module:app) file which will look something like shown below and add the library as shown.
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.yourappname"
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-project.txt'), 'proguard-rules.pro'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile project(':nameOfTheLibrary')
}
Add
dependencies {
compile 'com.afollestad:material-dialogs:0.7.7.0'
}
and press the sync button.
I have been trying to use the SlidingMenu library in my Android Studio project following the guide provided in this question:
How to import slidingmenu on Android Studio?
I've been able to obtain the described file structure and sync&build the project without errors. Yet, when trying to import the Sliding Menu to my apps source files, I'm getting a symbol resolve error.
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; // Error message: Cannot resolve symbol 'SlidingMenu'
import com.jeremyfeinstein.slidingmenu.lib.BuildConfig;
The second import is the only one suggested by Android Studio but I can't even find the BuildConfig file in my project when following the given path.
What do I need to change to be able to import all the classes in com.jeremyfeinstein.slidingmenu.lib to my own classes?
My apps gradle files looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.me.appname"
minSdkVersion 17
targetSdkVersion 21
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.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile project(':libraries:SlidingMenu')
}
The SlidingMenu gradle file:
buildscript {
// define the repo which is to use
repositories {
mavenCentral()
}
// define the classpath for Gradle Android Plugin
dependencies {
classpath 'com.android.tools.build:gradle:1.0.+'
}
}
// declaring that the project is a library
apply plugin: 'android-library'
// declaring all dependencies the project needs
dependencies {
compile 'com.android.support:support-v4:19.0.0'
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
// this values you can read out from the Manifest (but I add the right values for you)
minSdkVersion 17
targetSdkVersion 21
}
// because Android Studio has a different file structure than Eclipse
// you have to say Android Studio where the files are located
sourceSets{
main{
java.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
manifest.srcFile 'AndroidManifest.xml'
}
}
}
Project settings gradle:
include ":libraries:SlidingMenu", ':app'
Get the AAR port of the library (v1.3).
The GitHub Pages is down, so you'll have to import that AAR manually:
Top build.gradle file:
repositories {
flatDir {
dirs 'libs'
}
}
Place the AAR file in the libs folder of your module, then in your module build.gradle file:
dependencies {
compile(name:'library-1.3', ext:'aar')
}
I did a new install of Android Studio 1.0.2. Getting the following error when opening my first existing android project:
Gradle 'MyApplication' project refresh failed
Error:Artifact 'support-v4.jar (com.android.support:support-v4:21.0.3)' not found.
Searched in the following locations: file:/C:/Users/Ntokozo/AppData/Local/Android/sdk/extras/android/m2repository/com/android/support/support-v4/21.0.3/support-v4-21.0.3.jar
And if I navigate to that searched location I found two jar files which are: support-v4-21.0.3-sources.jar and support-v4-21.0.3-javadoc.jar
Here is my build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Gradle file under App folder:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.ntokozo.myapplication"
minSdkVersion 15
targetSdkVersion 21
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.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
}
Thank you in advance, please bare in mind in responses that I'm a novice and I might not know advanced features.
After re-installing windows OS and a new Android Studio setup the problem disappeared. I don't know what was the problem at first.
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.
For my Android project I set up Gradle with Jackson 2.2.x as follows:
// build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 18
}
}
dependencies {
compile 'com.android.support:appcompat-v7:18.0.0'
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.+'
compile 'com.fasterxml.jackson.core:jackson-core:2.2.+'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.+'
}
I simply use the ObjectMapper here:
import com.fasterxml.jackson.databind.ObjectMapper;
// ...
ObjectMapper objectMapper = new ObjectMapper();
try {
Content content = objectMapper.readValue(inputStream, Content.class);
} catch (IOException e) {
e.printStackTrace();
}
When I run gradle installDebug and launch the relevant part of the application it crashes:
java.lang.NoClassDefFoundError: com.fasterxml.jackson.databind.ObjectMapper
Thoughts:
I noticed that in the error message com.fasterxml.jackson... is mentioned while com.fasterxml.jackson.core is defined in build.gradle. Is there a mismatch causing the problem?
I temporarily moved the dependencies block into the android block since I found other build.gradle configurations structured this way. However it seems to make no difference.
Gradle and Android don't always place nicely with dependencies(yet). Running
gradle clean
seems to fix most problems for me.
Note: If that didn't work, you can run
dexdump classes.dex | grep 'Class descriptor'
on the classes.dex file in the APK. That will check to see if the class is included in the classes.dex file. (Sometimes it's useful if you want to double check whats going on).
Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.app.test"
minSdkVersion 9
targetSdkVersion 22
versionCode 4
versionName "1.3"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0' `enter code here`
compile 'com.fasterxml.jackson.core:jackson-databind :2.5.3'
}
Add permission and dependency into you Gradle and then build gradle you will get Object wrapper class