Run JUnit tests in Gradle for Android app - android

I want to run simple, plain JUnit tests in my Android app project, using Gradle at the same time to write Activity tests afterwards. It took a loong time to configure Gradle and make it work, but, anyway, now I'm stuck trying to make JUnit tests just compile.
I checked this link, but when I run gradle I get the following error:
DummyTest.java:3: error: package junit.
framework does not exist
import junit.framework.Assert;
^
\DummyTest.java:8: error: cannot find symbol
Assert.assertEquals(5,3);
^
symbol: variable Assert
location: class DummyTest
So, junit is not found...
The following is my full gradle.build file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile files('libs/joda-time-2.3.jar')
compile files('libs/android-support-v4.jar')
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.8.2'
}
android {
compileSdkVersion 17
buildToolsVersion '17.0.0'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
}
unitTest {
java.srcDir file('test')
resources.srcDir file('test/res')
}
}
defaultConfig {
minSdkVersion 14
versionName '1.0'
versionCode 1
targetSdkVersion 17
}
}
// add the unitTest task
task unitTest(type:Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
build.dependsOn unitTest

AndroidStudio and the new Android Gradle plugin are now offering official unit test support.
This is supported from Android Studio 1.1+ and Android Gradle plugin version 1.1.0+
Dependencies can now be declared as testCompile:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
More details here: Unit testing support - Android Tools Project Site.

Have a look at this stackoverlfow thread: junit testing with gradle for an android project
It looks like he's doing exactly what you need.
I think the problem in your script is, that you havn't declared a
unitTest sourceset explicitly.
cheers,
René

Related

Gradle error in Android Studio: Could not find “com.android.tools.build:gradle:7.1.1.”

I am trying to import a project from Eclipse into Android Studio.
In Eclipse I exported it to gradle and then imported it into Android Studio.
Note: I am new to gradle and have no insight to problem.
Now I get this error:
> Configure project :
Running gradle version: 6.8
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'aBooks'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not find com.android.tools.build:gradle:7.1.1.
Searched in the following locations:
-
https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/7.1.1/gradle-7.1.1.pom
If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
Required by:
project :
The link given leads to a “404 Page not found”.
The gradle files on my system with “gradle-7” are:
The gradle file :
buildscript {
// added crl https://tomgregory.com/anatomy-of-a-gradle-build-script/
repositories {
mavenCentral()
}
println "Running gradle version: $gradle.gradleVersion"
dependencies {
classpath 'com.android.tools.build:gradle:7.1.1' // **original error here**
// https://mvnrepository.com/artifact/com.android.tools.build/gradle
// implementation group: 'com.android.tools.build', name: 'gradle', version: '7.1.0-alpha01'
// https://mvnrepository.com/artifact/com.android.tools.build/gradle/7.1.0-alpha01
}
}
apply plugin: 'com.android.application' // 'android'
dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
implementation project(':CRLibs')
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
android {
compileSdk 26
buildToolsVersion "32.0.0"
buildFeatures {
prefab true
prefabPublishing true
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// 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')
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependenciesInfo {
includeInApk true
includeInBundle true
}
// no joy
plugins { // added crl https://developer.android.com/studio/releases/gradle-plugin#groovy
id 'com.android.application' version '7.0.0-alpha13' apply false
id 'com.android.library' version '7.0.0-alpha13' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}
}
I tried changing ‘7.1.1’ to ‘7.2’ no change. I have no clue on how to fix this. All the answers given in my searches do not match the versions here or were no help.
EDIT1:
Added info. when I go to file->Project Structure & Project, I see
Note that the Gradle version is blank. If I enter 6.8 or 7.1.1 and click OK, I get the same results and then if I go back the Gradle version is blank.
Gradle version 6.8 is identified at the current version(see line 2 of the error message. Don't know if this means anything.
This is the list of my Gradel-6* files:
Add google() to your repositories { } block. The Android Gradle plugin is located there.
Have the same problem
this was my solution
classpath 'com.android.tools.build:gradle:7.2.0'

Cannot import Eclipse project into Android Studio

I'm trying to import an Android project build on Eclipse into my Android Studio IDE. I'm following the steps to import properly the project with a build.gradle file but when I'm trying to compile, I get this weird error: Plugin with id 'android' not found. I have this version of Android Studio: Android Studio (Preview) 0.4.3. Here is my build.gradle:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:DesignDialogLibrary')
compile project(':workspace:HoloColorPickerLib')
compile project(':workspace:MbAdBleamSdkReaderLibrary')
compile project(':workspace:ViewPagerLibrary')
compile project(':workspace:PullToRefreshLibrary')
compile project(':workspace:SlidingMenuLib')
}
android {
compileSdkVersion 17
buildToolsVersion "18.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
You're missing this block in your build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
This block tells Gradle where to find the "android" plugin that you're missing.
It's standard in projects created in newer versions of Android Studio to put this in the build.gradle that's in your project's root directory and omit it from the one that's in your module's directory, so look there and see if looks like it's in order.
If you don't have that root-level build.gradle file and you've only got one directory and one build file, then add this block to that file. Be aware, however, that Android Studio currently has a few bugs that prevent the Project Structure dialog from working properly with projects in this single-module format, though I believe they'll otherwise build and run fine.

Gradle android-library build fails. java.lang.IllegalArgumentException: already added

I'm trying to build my android library project, which contains sub test project. Command to build: gradle clean connectedCheck
After task :dexTest I got error:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lorg/json/simple/ItemList;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lorg/apache/commons/lang3/CharRange$1;
Command gradle clean build ends successful. So my problem is that i can't run my tests. When i change plugin, in my build.gradle file, from 'android-library' to 'android' everything works fine.
Also, i'm added --info param and got this, before error
command: /home/username/adt-bundle/sdk/build-tools/18.1.1/dx --dex --output /home/username/android/build/libs/android-test.dex /home/username/android/build/classes/test /home/username/android/build/dependency-cache/test /home/username/android/libs/json_simple.jar /home/username/android/build/bundles/debug/classes.jar /home/username/android/build/bundles/debug/libs/json_simple.jar /home/username/android/libs/android-support-v4.jar /home/username/android/build/bundles/debug/libs/commons-lang3-3.1.jar /home/username/android/build/bundles/debug/libs/android-support-v4.jar /home/username/android/libs/commons-lang3-3.1.jar
What was that? Why it uses libraries from folder build/bundles/debug/libs/? This is the reason why I had error java.lang.IllegalArgumentException but why gradle takes libraries from that dir i don't understand.
Here my build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.3'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 18
testPackageName "ua.cooperok.stringcalc.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets {
main {
manifest.srcFile file('AndroidManifest.xml')
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest {
manifest.srcFile file('tests/AndroidManifest.xml')
java.srcDirs = ['tests/src']
resources.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
}
Maybe it's a bug in android-library plugin?
While I do not know exact answer to the question, my colleagues from JetBrains supporting gradle in IntelliJ IDEA advised to ask the question at https://code.google.com/p/android/ because it's clearly an issue (and may be a bug) in Google Android Gradle Plugin for Eclipse, given that you work in Eclipse, as far as I recall from our conversation.
Now problem is no longer relevant. In build tools from version 0.7.2 it is fixed.
Piece of updates from tech doc
- Solve issue with local jar when testing library projects.

Gradle import issues - Android

I am currently attempting to switch my project over to using gradle builds. While using the old build system I linked to the private/internal/hidden android API's by compiling the source and linking the classes.jar files before the Android libraries. I am currently trying to do the same thing in gradle with no luck at all.
With my current build.gradle file I receive the following error:
Gradle: A problem occurred evaluating project ':Launcher2'.
> Could not find method external() for arguments [file collection] on project ':Launcher2'.
If I remove these lines before "apply plugin: android"
dependencies {
compile files('libraries/classes.jar')
}
I receive errors about missing com.android.internal and com.android.common packages which are located in my classes.jar file.
Here's my build.gradle file.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
dependencies {
compile files('libraries/classes.jar')
}
plugin: 'android'
android {
buildToolsVersion "17.0"
compileSdkVersion 10
dependencies {
compile fileTree(dir: 'libraries', include: '*.jar')
compile project(':ActiveAndroid')
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
Edit: After reading the gradle documentation a bit more I was able to successfully link the libraries into the project but the order of dependencies is wrong. The equivalent to what I am trying to do is adding both .jar files to the top of the classpath so that they are declared before the android SDK jar files.
I was digging through the source for the android gradle plugin, it seems that this may not be possible. The code seems incomplete and will not build. If anyone has any insight I would really appreciate it. Otherwise it looks like I will have to switch back to using Ant for now.

Gradle can't find dependency (Android support library)

I have a problem that Gradle can't find my dependency (Android support library).
My build.gradle looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/FlurryAgent.jar')
compile group: 'com.google.android', name: 'support-v4', version: 'r7'
compile files('libs/YouTubeAndroidPlayerApi.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 11
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
When I build (on commandline, no IDE) I get the following message:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'AndroidCalculator'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':compile'.
> Could not find com.google.android:support-v4:r7.
Required by:
:AndroidCalculator:unspecified
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Why am I not allowed to add the Android Support library like this?
You have declared a repository dependency, but haven't declared a repository. Hence the dependency cannot be resolved. (Repositories/dependencies in the buildscript block are strictly separated from repositories/dependencies in the main build script.)
http://pastebin.com/FmcCZwA5
This paste is elaborate project with AndroidAnnotations, Dagger, Jackson and Robolectric.
all you need is add
repositories {
mavenCentral()
}
replace
compile group: 'com.google.android', name: 'support-v4', version: 'r7'
with (line 44 of the code linked above)
compile 'com.android.support:support-v4:18.0.+'
Gotchas: Last bit will works on Android Studio 0.2+ only if you had a fresh install. Since 0.2 Studio is shipped with its internal m2 repo to provide support and google api libraries so if you upgraded from previous versions your SDK doesn't have it.
also make sure local.properties file is present in root folder and sdk.dir points to SDK
You need to add additional dependency in dependencies tag. If you have android-support-v4.jar library in your libs folder, try to add code listed below:
dependencies {
compile files('libs/android-support-v4.jar')
}

Categories

Resources