How to run ./gradlew test - android

I'm trying to test an app from the command line. When I run my test from the Android Studio, everything goes fine. But when I try to run the command:
./gradlew test
I get this error:
Task 'test' not found in root project 'mvp_kotlin_example'.
I don't have problems using ./gradlew build or ./gradlew clean. I just created a project to try the ./gradlew test and it worked.
In mvp_kotlin_example I am using Robolectric for tests and Kotlin for development.
So how can I make my tests work from the command line?
Edit:
This is a open source project, so all the code can be found in the
https://github.com/leandroBorgesFerreira/mvp-kotlin-example
This is my build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply from: '../jacoco.gradle'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "br.com.simplepass.simplepassnew"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled = true
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.1.4"
testCompile 'com.squareup.okhttp3:mockwebserver:3.3.1'
testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
testCompile "org.mockito:mockito-core:2.4.2"
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'br.com.simplepass:loading-button-android:1.5.0'
compile 'org.jetbrains.anko:anko-sdk15:0.9' // sdk19, sdk21, sdk23 are also available
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.google.dagger:dagger:2.8'
kapt 'com.google.dagger:dagger-compiler:2.8'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
repositories {
mavenCentral()
}
kapt {
generateStubs = true
}
Edit 2:
This is what I see after running ./gradlew tasks (it's a lot less tasks than the usuall)
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'mvp_kotlin_example'.
components - Displays the components produced by root project 'mvp_kotlin_example'. [incubating]
dependencies - Displays all dependencies declared in root project 'mvp_kotlin_example'.
dependencyInsight - Displays the insight into a specific dependency in root project 'mvp_kotlin_example'.
help - Displays a help message.
model - Displays the configuration model of root project 'mvp_kotlin_example'. [incubating]
projects - Displays the sub-projects of root project 'mvp_kotlin_example'.
properties - Displays the properties of root project 'mvp_kotlin_example'.
tasks - Displays the tasks runnable from root project 'mvp_kotlin_example'.
Other tasks
-----------
clean

Here the problem has nothing to do with your build.gradle your project was missing the settings.gradle file that tells gradle to include the app folder as a module. I've created PR #1 on your project to resolve this defect, when merged you should be able to run ./gradlew clean test from the root project directory.
https://github.com/leandroBorgesFerreira/mvp-kotlin-example/pull/1
Solution is to just add a oneline settings.gradle file to the root project that declares the modules included in the build.
$ cat settings.gradle
include 'app'

Related

Not able to copy configurations dependencies after upgrading Gradle plugin for Android Studio to 3.0.1 and Gradle to 4.1

I used to copy 'compile' dependencies to a specific folder using this simple gradle task :
task copyLibs(type: Copy) {
from configurations.compile
into "$project.rootDir/reports/libs/"
}
But it stopped working just after upgrading my Android project using gradle plugin 3.0.1 for Android Studio and Gradle tool to 4.1. As the dependency configuration 'compile' is now deprecated by https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations I changed it to 'implementation'. However, I am not able to use my copyLibs task as resolving configuration 'implementation' directly is not allowed as per Gradle build error output :
$ ./gradlew.bat clean build
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:copyLibs'.
> Resolving configuration 'implementation' directly is not allowed
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
See following my current build.gradle file for app module : apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "newgradle.com.testingnewgradle"
minSdkVersion 21
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:support-v4:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
task copyLibs(type: Copy) {
from configurations.implementation
into "$project.rootDir/reports/libs/"
}
build.dependsOn copyLibs
If I use 'compile' it works but I would like to be compliant with the latest recommendation on this plugin the usage.
I need help to upgrade my copyLibs task in order to work as before upgrading my enviromment.
I was using gradle plugin 2.2.3 for Android Studio and Gradle tool 2.14.1.
instead of using configurations.implementation, the best option is to use configurations.runtimeClasspath.
You can also think about:
compileClasspath
default
I make the configuration resolvable, so there is no exception when getting the dependencies
configurations.implementation.setCanBeResolved(true)
configurations.api.setCanBeResolved(true)
println configurations.implementation.resolve()
println configurations.api.resolve()
Another suggestion.
I created my custom config and then used it as configurations.customConfig:
configurations {
customConfig.extendsFrom implementation
}
The copy task must be correspondingly edited:
task copyLibs(type: Copy) {
from configurations.customConfig
into "$project.rootDir/reports/libs/"
}
It doesn't look as if there's a way to acquire the list of implementation dependencies and the compileClasspath mentioned at the Gradle ticket Rafael posted won't work if you're working with Android directly, like my case where I need the dependencies to be exported so that Unity3D can package them up for release.
So.. it looks like the only solution in this case is to use the deprecated compile type.
This probably won't help or have a better way to solve but...
You can put your dependencies in a way that is possible to copy doing the following:
android { ... }
// Add a new configuration to hold your dependencies
configurations {
myConfig
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// Now you have to repeat adding the dependencies you want to copy in the 'myConfig'
myConfig fileTree(dir: 'libs', include: ['*.jar'])
myConfig 'com.android.support:appcompat-v7:26.1.0'
myConfig 'com.android.support:support-v4:26.1.0'
...
}
task copyLibs(type: Copy) {
// Now you can use 'myConfig' instead of 'implementation' or 'compile'
from configurations.myConfig
into "$project.rootDir/reports/libs/"
}
This also helps if you have a Jar task that stops placing the dependencies in to the jar file because you changed from compile to implementation.
You can use:
from {configurations.myConfig.collect { it.isDirectory() ? it : zipTree(it) }}
Instead of:
from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}
I started getting this error after upgrading from gradle 5.5 to 5.6, and it happens when I try to sync the project in intelliJ.
Thanks to this answer on another question, I solved it by applying the idea plugin to all projects and then running gradle cleanIdea and after that everything started working again.
Another day, another #inexplicable solution to a problem.
there is some documentation about this, at least at this moment. You can not directly use 'implementation' configuration, but only use one that extends this. Now you can create a custom configuration, but there are preconfigured extensions already, like 'compileClasspath'.
compileClasspath extends compileOnly, implementation
Compile classpath, used when compiling source. Used by task compileJava.
Documentation is here: https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management

no test task in gradle build

I am trying to build and imported Android project in Android Studio/Eclipse.
My goal is to write automated test to the current project. First, I am trying to build the project and then to make an apk file of it so I will be able to execute real device/emulator tests on it.
Here are my available Gradle tasks
There is no build or test or assemble and etc. tasks which are I am looking to use so I will reach my goal.
Here is my project tree and both build.gradle files
`
apply plugin: "java"
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13
testCompile 'junit:junit:4.12'
}
and
apply plugin: 'com.android.application'
version = "1.2"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "sdk.mobfox.com.appcore"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
multiDexEnabled true
archivesBaseName = "MobFox-Android-SDK-Client-" + version + ".apk"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize = "4g"
}
lintOptions {
abortOnError false
}
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.google.android.gms:play-services-ads:11.0.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.danikula:videocache:2.7.0'
testCompile 'junit:junit:4.12'}
I tried to open the project in Android Studio but got the same state.
I opened a new Gradle project in Eclipse and saw that the tasks I am looking for are available there - I believe because of the 'java-library' plugin which added to the build.gradle root file but I use the same plugin in my root build file and did not receive what I expected.
I was succeeded to execute the Gradle "tasks" which gave me the next response in console :
Working Directory: C:\Users\orit\Desktop\mobFox\MobFox-Android-SDK-master\MobFox-Android-SDK-master
Gradle User Home: C:\Users\orit.gradle
Gradle Distribution: Specific Gradle version 4.1
Gradle Version: 4.1
Java Home: C:\Program Files\Java\jdk1.8.0_91
JVM Arguments: None
Program Arguments: None
Gradle Tasks: tasks
:tasks
All tasks runnable from root project
Build tasks
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Documentation tasks
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
buildEnvironment - Displays all buildscript dependencies declared in root project 'MobFox-Android-SDK-master'.
components - Displays the components produced by root project 'MobFox-Android-SDK-master'. [incubating]
dependencies - Displays all dependencies declared in root project 'MobFox-Android-SDK-master'.
dependencyInsight - Displays the insight into a specific dependency in root project 'MobFox-Android-SDK-master'.
dependentComponents - Displays the dependent components of components in root project 'MobFox-Android-SDK-master'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'MobFox-Android-SDK-master'. [incubating]
projects - Displays the sub-projects of root project 'MobFox-Android-SDK-master'.
properties - Displays the properties of root project 'MobFox-Android-SDK-master'.
tasks - Displays the tasks runnable from root project 'MobFox-Android-SDK-master'.
Verification tasks
check - Runs all checks.
test - Runs the unit tests.
Rules
Pattern: clean: Cleans the output files of a task.
Pattern: build: Assembles the artifacts of a configuration.
Pattern: upload: Assembles and uploads the artifacts belonging to a configuration.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
**1. What is the reason I cannot get all the task I got when I open a new project?
How can I add these tasks so I will be able to create apk file, build and execute integration tests?**
task, you write in project level build.gradle file. See below pic for the reference.
You are posting module level build.grale file.
Applying com.android.application should be enough, you should not apply java plugin on the root either and add dependencies there.
Operate on your app project, with Java specific stuff.
For more reference how to structure your project.
https://developer.android.com/studio/build/index.html

No Gradle "build" task despite build.gradle applying plugin com.android.application

Somehow I don't have a build task:
$ ./gradlew tasks --all
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating] [wrapper]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
components - Displays the components produced by root project 'apps-android-commons'. [incubating]
dependencies - Displays all dependencies declared in root project 'apps-android-commons'.
dependencyInsight - Displays the insight into a specific dependency in root project 'apps-android-commons'.
help - Displays a help message.
model - Displays the configuration model of root project 'apps-android-commons'. [incubating]
projects - Displays the sub-projects of root project 'apps-android-commons'.
properties - Displays the properties of root project 'apps-android-commons'.
tasks - Displays the tasks runnable from root project 'apps-android-commons'.
BUILD SUCCESSFUL
Despite my commons/app/build.gradle that applies (as described in this other QA) a relevant plugin:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile 'fr.avianey.com.viewpagerindicator:library:2.4.1.1#aar'
compile 'in.yuvi:http.fluent:1.3'
compile 'com.android.volley:volley:1.0.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.8.4'
compile 'ch.acra:acra:4.5.0'
compile 'org.mediawiki:api:1.3'
compile 'commons-codec:commons-codec:1.10'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
//noinspection GradleDependency - old version has required feature
compile 'com.google.code.gson:gson:1.4'
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "fr.free.nrw.commons"
minSdkVersion 11
targetSdkVersion 23
ndk {
moduleName "libtranscode"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
lintOptions {
disable 'MissingTranslation'
disable 'ExtraTranslation'
}
}
QUESTION: Why is that, am I doing something wrong?
Here is my project structure:
ROOT
/commons
/app
... and I execute ./gradlew in ROOT.
If your folder is:
ROOT
/commons
/app
... then execute .gradlew in the commons folder, not in ROOT.
Thanks #Opal for the tip!

google play service error

I got this below error
Error:(1, 1) A problem occurred evaluating project ':app'.
> Failed to apply plugin [id 'com.android.application']
> Gradle version 2.10 is required. Current version is 2.8. If using the gradle wrapper, try editing the distributionUrl in C:\Users\TARUN\Desktop\GingerBuds\gradle\wrapper\gradle-wrapper.properties to gradle-2.10-all.zip
Then I changed the above gradle path, Then following problem occurred,
I am getting this exception, while building my project . I rebuild and clean the project and updated google-play-services , then also i got error. can anyone please help me how to solve this.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.io.FileNotFoundException: C:\Users\TARUN\Desktop\Gingerbuds\app\build\intermediates\exploded-aar\com.google.android.gms\play-services\8.4.0\jars\classes.jar (The system cannot find the path specified)
Build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.kitchenvilla.gingerbuds"
minSdkVersion 15
targetSdkVersion 23
versionCode 24
versionName "1.2.2"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
android {
useLibrary 'org.apache.http.legacy'
}
android {
publishNonDefault true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.mcxiaoke.volley:library:1.0.15'
compile 'org.apmem.tools:layouts:1.10#aar'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.pkmmte.view:circularimageview:1.1'
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
}
This bug has been fixed according to Google dev blog: http://tools.android.com/tech-docs/new-build-system
2.0.0-alpha6 (2016/1/15)
Instant Run
Fix alpha5 reported issues :
- cannot build when importing play-services.
You should update android gradle tools version
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha6'
}
This exception is thrown during a failed attempt to open the file denoted by a specified Path-name.
You can use
compile 'com.google.android.gms:play-services:8.3.0'
Update your classpath
classpath 'com.android.tools.build:gradle:2.0.0-alpha2'
this will solve it
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.google.gms:google-services:2.0.0-alpha2'
}
and
compile 'com.google.android.gms:play-services:8.3.0'
update gradle version to alpha9 in project level gradle file
classpath 'com.android.tools.build:gradle:2.0.0-alpha9'
i tried to import admob ads to a new app i was making and i ran into this problem...... i found the solution the be the StartAppInApp file being located in the wrong spot or not being there..... my app needed the StartAppInApp-3.1.1.jar file to be located in the libs folder and when i looked at my app there was no libs folder located there at all .....
open your main app folder... then click app and you should have 3 folders.... src ... build.... and libs .....
in my case there was no libs folder..... so i created the libs folder and then went and copied the StartAppInApp-3.1.1.jar file from a different android project i had and i pasted it to the new libs folder i made in my current project... the error is now GONE!
i also made sure to include
compile files('libs/StartAppInApp-3.1.1.jar')
in the dependencies of my app gradle file
not sure if this is a solid answer but if anyone has this problem double check your StartAppInApp-3.1.1.jar location and version and check your gradle dependencies

Error:Configuration with name 'default' not found when trying to import project as library into Android Studio

I checked all the other threads about this topic but couldn't find an answer.
I am trying to import the Twoway View Project as a library into Android Studio.
Both projects run fine on their own but I always get the same Gradle Error: Error:Configuration with name 'default' not found when trying to import.
I have the project copied into a "libraries" directory in the root folder of my project and the following gradle structure:
settings.gradle of my project:
include ':libraries:twoway-view-master',':app'
build.gradle of "app":
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "app.com.jeldrik.teacherslittlehelper"
minSdkVersion 13
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 project(':libraries:twoway-view-master')
and in twoway-view-master build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
That twowayview-master build.gradle file isn't a buildscript for a standalone module; it lacks any sort of apply plugin statement that would tell Gradle how it should compile something. This looks like the top-level build file of a multimodule-structured project. In your settings.gradle file, you should point at the module in the project you're trying to include, not the build file at the top level.
Did you try it out using File -> New Module?
Or try setting the dependencies from here as well : File -> Project Structure -> Dependencies
I have recently got into same issue. As Scott said we have to include individual modules in our project's build.gradle file. This TwoWayView library has 3 different modules
core
layouts
sample
Say if you want to add core and layouts, add the below lines in your project's build.gradle file (Assuming you have twoway-view-master folder inside libraries folder which is inside your app folder).
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':your-app-folder:libraries:twoway-view-master:core')
compile project(':your-app-folder:libraries:twoway-view-master:layouts')
}
Then add the same path to your project's settings.gradle file
include ':your-app-folder:libraries:twoway-view-master:core'
include ':your-app-folder:libraries:twoway-view-master:layouts'
NOTE: The build.gradle files inside core and layouts have wrong path to gradle-mvn-push.gradle file. So change the path from
apply from: "${rootDir}/gradle/scripts/gradle-mvn-push.gradle"
to
apply from: "${rootDir}/your-app-folder/libraries/twoway-view-master/gradle/scripts/gradle-mvn-push.gradle"
If you still get error in layouts' build.gradle file, change this line
compile project(':core')
to
compile project(':your-app-folder:libraries:twoway-view-master:core')
Do the same change if you're also using sample's build.gradle file in your project.

Categories

Resources