http://www.tutorialspoint.com/junit/junit_environment_setup.htm
I have done everything like in this step by step tutorial and still when I try to write the line
import org.junit.*;
The "junit" part is lighten red and the error message is "cannot resolve symbol "junit"".
The tutorial you are following seems to be written for command line usage. (Download jar, set classpath etc). So the same wouldn't work in IntelliJ.
IDEs actually make these a lot easier.
To solve your current dilemma, try this. Place the cursor on the red 'junit' (which is basically a broken import) and choose "Add Junit4 to classpath". This will add the necessary junit jars that IDEA ships with to your classpath.
Open up your build.gradle and make sure JUnit is there
dependencies {
testCompile 'junit:junit:4.12'
// other dependencies...
}
And for a better link for Android-specifics, read Getting Started with Testing from the Android docs.
Related
I'm pretty new to android studio, but i got this error and i can't run my project. I saw a lot of answers which suggest to add the support-annotations dependency, but I'm using appcompat so i don't need that. Any thoughts?
Do you have the Android Support Library?
To do it add this to your build.gradle file of the "app" module:
dependencies {
...
implementation "com.android.support:support-annotations:27.1.0"
}
Then sync the project.
Sometimes you can do this automatically by pressing Alt+Enter over the error message and selecting the solution.
You need to fetch the libraries that contain the imports, in your build gradle file of your module add the line inside dependencies tag:
api 'com.android.support:support-annotations:X.X.X'
or
implementation 'com.android.support:support-annotations:X.X.X'
Where X.X.X is the version of the rest of your support libraries.
You should read this article that explains how to implement this with more detail.
I have an Android project with the following dependencies:
-- Android App
---> MySDK.Jar
------> 'org.apache.commons:commons-lang3:3.5'
This is MySDK.jar that has a dependency on commons-lang3.
I'm working on Android Studio and I'm thus using Gradle.
Here is my problem:
I have shared "MySDK.Jar" to someone and he has built his own Android App on top of it.
It works but we have seen that the compiler doesn't notice the missing dependency on 'org.apache.commons:commons-lang3:3.5'. At run-time there will be a crash if the code using 'org.apache.commons:commons-lang3:3.5' is called. One may not notice the problem if he doesn't call the code using this library.
I know that we can solve this issue by adding the following line to Android App build.gradle file:
compile 'org.apache.commons:commons-lang3:3.5'
I'm wondering if there is a way to get a compile error indicating such missing dependencies? It is indeed better to see the dependency problem at compilation time rather than at runtime.
What are the recommended good practices for this?
Thanks!
commons-lang3 is a transitive dependency of Android App. As such, it is often not needed for compilation - there are exceptions, especially regarding multiple levels of inheritance. So at compile time you (usually) do not know whether you miss a transitive dependency that you need at runtime.
This is where Gradle comes in. Gradle can (as Maven) resolve dependencies transitively from a Maven repository (as MavenCentral). If you put MySDK into a Maven repository (like Nexus or Artifactory, which have open source versions), everyone using MySDK will automatically draw commons-lang3 so you will not miss anything at runtime.
If you are just adding the jar file in your project you can't warning about the missing dependencies.
To do it you have to publish the jar file in a maven repo.
In this way you have a pom file which describes the dependencies that gradle has to download.
Provide a method like MySDK.init() int your MySDK.jar,call a method whe is belong to org.apache.commons:commons-lang3:3.5' in the MySDK.init() method, then put init() into onCreate() of your Application,
Another way is,putorg.apache.commons:commons-lang3:3.5 into MySDK.jar,
Hope it helps you :)
I'm trying to import this as a library in a project I'm working on in Android Studio:
https://github.com/d4rken/myolib
I cant workout exactly how to do it. even before I try to do this, I tried to open the project using new->Import project-> (selecting the settings.gradle). This then complained about not knowing about:
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
So I tried adding these manually using the advice on:
Importing github projects as library to existing project
Adding external library in Android studio
But its still complaining about no cached version for offline mode. If I do disable work in offline mode then, well it just sits there forever. I want to give it everything manually because in my experience disabling work in offine mode usually doesn't help.
I completely understand this isn't a new topic on Stackoverflow. The issue of how best to import libraries as .jar, .aar, .os etc. etc has been covered a few times. And yet most answers are subtly different and some work sometimes, others work other times.
Does anyone know of a detailed explanation about how I should achieve this ?
If anyone has been successful in importing this project and using its libraries I will be eternally grateful if you could tell me how you achieved such wizardry.
You import the library through your app's build.gradle file. An import section looks something like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.1.1'
compile "com.android.support:support-v13:24.1.1"
}
So to import the library you just linked, you would simply add this line to your dependencies section, as per the readme:
compile 'eu.darken.myolib:myolib:0.0.4'
I am starting with Robolectric framework 3.0. for android testing. I am goingthrough there site and they mention the first step as adding in build gradle
testCompile "org.robolectric:robolectric:3.0"
I have done that and did a complete clean build.
Now once I start writing my first test case I get comilation issue-- cannot find symbol class RunWith for the annotation #
RunWith(RobolectricGradleTestRunner.class)
Please tell me, what I am missing? I thought mavencentral() will download the respective jar file.
Thanks in advance.
You can refer this this link.
Make sure your test case locations are proper.
Android Studio defaults to looking for tests in the following locations:
Unit Tests => src/test/java
I have been struggling with unit-test-android problem for quite a long time. I have seen this, this and that, finally I found the gradle-android-test-plugin and even got it working. I can now run the tests with gradlew test command.
But, writing those tests in IDE (Android Studio or IntelliJ 13) is far from comfortable, because it does not see the junit & Robolectric dependencies added with testCompile dependency.
Is there any way to add these dependencies to the IDE classpath but still avoid to package them in the production app (thus, AFAIU compile dependency cannot be used)?
I had the same problem with IntelliJ 14.1.3 today. The solution was to run the steps outlined here. Basically:
Add JUnit and other dependencies via testCompile 'junit:junit:4.+', etz
Put test sources in src/test/java/...
To make the IDE find the test-dependencies (gradle will find them fine), open the "Build Variants"-view and set "Test Artifact" to "Unit Test". In "Project Structure", the testing dependencies should show up in your module with the "Test"-scope
The commandline to run a test is testXxx, where Xxx is the build-type (debug/release/etz).
The important step here is the one in the "Build Variants" view. After you change it to "Unit Test", it will index and your libraries and full auto-completion are available.
For my Android test dependencies, I use instrumentTestCompile instead of testCompile. This works for me when running my tests in Android Studio. Hope this helps.
You can use the built-in idea plugin. That should set up test dependencies for you. You'll need to import the plugin:
apply plugin: 'idea'
Then run gradle idea, to generate module file (*.iml) and re-load your project. Note you'll have to be using non-directory based idea configuration for this to work.
In IntelliJ IDEA you need to configure couple things in your build.gradle
// add idea plugin
apply plugin: 'idea'
// make sure `configurations.testCompile` is added to idea.module
idea {
module {
scopes.TEST.plus += [ configurations.testCompile ]
}
}
For more info see:
http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
Any dependency included with testCompile will be automatically imported into IDEA.