Error when setting up unit testing with Gradle - android

I'm trying to set up Android unit testing following this tutorial: http://www.peterfriese.de/android-testing-with-robolectric/
But then I realized I don't know enough about the build system to understand what's really going on.
So I have some questions about part of my build.gradle below
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
}
and
dependencies {
compile 'com.android.support:appcompat-v7:+'
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
testCompile 'com.squareup:fest-android:1.0.+'
instrumentTestCompile 'junit:junit:4.10'
instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
instrumentTestCompile 'com.squareup:fest-android:1.0.+'
}
When I do classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT', how does Gradle know where com.squareup.gradle is?
Also, right now, I get an error saying Gradle can't find the Robolectric library org.robolectric:robolectric:2.3-SNAPSHOT. Is it something I have to manually add or is the build system supposed to somehow automatically take care of it?

Gradle will find com.squareup.gradle from the configured repositories -- since it doesn't exist in Maven Central, it'll look in the Sonatype repo and find it there.
The Robolectric library should be available to Gradle, as visiting the snapshot repository URL manually shows it to be there. You could have an earlier version cached: mark the repository as 'changing', refresh dependencies, or simply rm -rf .gradle/caches to force Gradle to look again. See this answer for more: How can I force gradle to redownload dependencies?

Related

multiple errors after opening project on different computer (and then reopening on first computer)

Yesterday I opened the Android project on another computer (files were on my onedrive, so I never 'moved' the files') to show the project to someone.
Now I reopened the project on my actual computer and lots of errors are generated (see image). The project worked fine before.
They were both windows computers.
I tried resyncing the gradle and rebuilding the project but the issue persists. What did I do wrong and more importantly: how to fix it (I'm very new to Android).
The strange thing is that I can still run the app without errors.
Any suggestions?
You are missing dependencies.
From the looks of it, you are missing Android Support dependencies and
Volley
Confirm your build.gradle has the appropriate library references and repos to pull it and that the new machine has the sdk installed as well.
If you are using pre-28 I would expect to see in your parent project gradle.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71' //if using kotlin
classpath 'com.google.gms:google-services:4.1.0' //if using google services
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven {url 'https://maven.google.com/'} //older android studio
maven { url "https://jitpack.io" } //post api 28 needed
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Then in your app level build.gradle you should have dependencies to get your volley and support libraries like the following example if you are post API 28:
NOTE* If you are pre-28 then update the namespaces to non-androix pathways.
dependencies {
implementation 'com.google.firebase:firebase-crash:16.2.1' //crash reporting
//to compile #synchronized things and java references
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.0"
//Android ui support
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
//Networking
implementation 'com.android.volley:volley:1.1.1'
// [START firebase]
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-appindexing:16.0.2'
implementation 'com.google.android.gms:play-services-base:15.0.2'
// [END firebase]
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.6'
}
Then whenever you open on a new machine just check that you have the SDK path accurate. For example the local.properties should never be checked into the repo as it has a hard coded path for your "local" properties.
Next make sure your SDK path is good in the menu, it's the folder with the 3 little blue squares:
If all of these are good, then do a build.clean and rebuild to see if that clears you up. If you still have issues you can try
File->Invalidate Cache and Reset
Lastly, if that still doesn't work, you may have build cache issues, so delete all your iml files, idea files, build folders and import the project fresh from code only. I'm guessing you are copying the physical folder of the project from one computer to another which would cause issues.
Hope that helps.
Happy Coding.

Failed to resolve: com.android.databinding:library:3.1.2

After updating android studio 3.1.2 my existing project gives error at
dataBinding.enabled = true
error is as follows-
Failed to resolve: com.android.databinding:library:3.1.2
Failed to resolve: com.android.databinding:adapters:3.1.2
my gradle dependency are as follows-
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.1.0'
implementation 'com.android.support:recyclerview-v7:25.1.0'
implementation 'com.android.support:preference-v7:25.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.0-beta3'
implementation 'com.firebase:firebase-jobdispatcher:0.5.0'
// Instrumentation dependencies use androidTestCompile
// (as opposed to testCompile for local unit tests run in the JVM)
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support:support-annotations:25.1.0'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test:rules:0.5'
}
I also tried
android.databinding.enableV2=true
but its also not working
When I tried to update build tool version to 4.4 then I found this error. I f I do not update the build tool version then its working fine.
check it this below code in your project level gradle file ..
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
According to the Android Team answer you had to add
google()
too all repositories
and priority is important. so other repositories must be added after google()
Downgrade to 3.1.0 in project level build.gradle file, then rebuild the project.
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
I hope this helps
I also has the same issue I ensured that I added google() to all repositories and also I downgraded gradle to 3.1.0 from 3.1.4 and my project was successfully build.
Another TIP: We can also add the below code to our build.gradle(project) which takes cares of libraries conflicts with new dependencies
configurations.all {
resolutionStrategy {
force"com.android.support:supportannotations:$androidSupportVersion"
force "com.android.support:support-v4:$androidSupportVersion"
force "com.android.support:appcompat-v7:$androidSupportVersion"
force "com.android.support:design:$androidSupportVersion"
force "com.android.support:recyclerview-v7:$androidSupportVersion"
}
}
And you can keep your libraries in a separate file such as libraries.gradle and include them in build.gradle(project)
buildscript
{
apply from: "./libraries.gradle"
}
I added Data-Binding to my project and the next time I build project, I faced the same issue, more specifically logcat displayed this message:
Failed to resolve: com.android.databinding:library:3.1.2
Failed to resolve: com.android.databinding:adapters:3.1.2
As per accepted answer, I checked my Project Level Gradle File to see whether it was missing google() in its repositories, but it was already there.
Build multiple times, but all in vain.
Then I did what my Android Master once told me to do, just in case when everything fails.
File -> Invalidate Caches/Restart.
Next time the project opened it build successfully.

Failed to resolve: com.android.support.design:25.4.0

I added the following line to my build.gradle(Module:app):
compile 'com.android.support:design:25.4.0'
But when executing Gradle I'm getting
Failed to resolve: com.android.support.design:25.4.0
I got that the support code from the android support design library and added it to a new project. I added it to the dependency section as such:
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.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:25.4.0'
}
Any ideas on what I'm doing wrong?
Important: The support libraries are now available through Google's Maven repository. You do not need to download the support
repository from the SDK Manager. For more information, see Support
Library Setup.
Step 1: Open the build.gradle file for your application.
Step 2: Make sure that the repositories section includes a maven section with the "https://maven.google.com" endpoint. For example:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Step 3: Add the support library to the dependencies section. For example, to add the v4 core-utils library, add the following lines:
dependencies {
...
compile "com.android.support:support-core-utils:25.4.0"
}
A more updated version of the answer of "Bhavesh Patadiya" :
In your project build.gradle file, add google() into the repositories blocks:
repositories {
jcenter()
google()
}
Update the same file with a newer Gradle version:
classpath 'com.android.tools.build:gradle:2.3.3'
If the above cause you new issues or the same issue, exit Android-Studio, and delete the "gradle" folder/s (maybe also ".gradle" folder) and the "build" folder and sub-folders, and then open Android-Studio again.
Mr. Bhavesh Patadiya give us a good solution. However, I'd like to share something more, to make fix process more explicit.
There are two "build.gradle" files under the project directory. Their pathes are to be "Your-project-root-dir/build.gradle" and "Your-project-root-dir/app/build.gradle" respectively. When you see the error information in your android studio, and try to trace the file, you will probably open the second one.
You should add this statement in the first file ("Your-project-root-dir/build.gradle").
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
and add the statements in the second build.gradle ("Your-project-root-dir/app/build.gradle")
dependencies {
...
compile "com.android.support:support-core-utils:27.0.2"
}
Always keep appcompact version and support lib versionssame, so change com.android.support:design:25.4.0 to com.android.support:design:25.3.1
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
You need to update the android support Repository in the SDK manager . Also the Design Library depends on the Support v4 and AppCompat Support Libraries.
Same version android support must be the same with others..
compile 'com.android.support:appcompat-v7:25.3.1' <-- same
compile 'com.android.support:design:23.3.1' <-- same
after adding :
maven {
url "https://maven.google.com"
}
make sure your Gradle sync is on ONLINE mode
you can check it from:
Android studio -> Preferences -> Build, Execution, Deployment ->
Gradle -> Offline work (make sure this check box is not selected)
This problem occurs when there is andoridtestImplementation is added in app.build.
Remove testImplementation,androidTestImplementation from the app.build, that solves this issue.
Above answers did't resolve anything for me.
Tried syncing the project- Failed.
Tried building the project -Failed
Problem found :
Sdk Support Repository was corrupted
.
Fix:
Go to the SDK manager, click the "SDK Tools" tab. If the check-mark
for "Support Repository" is selected, unselect it and click OK. This
will delete all of the files in the repository. Then recheck the
check-mark, click GO and reinstall the repository.
If you still have the issue, check the project settings for offline mode. if offline mode is on, then off and sync the project. That fixed my issue.
There is no library by that name. There is com.android.support:recyclerview-v7:25.4.0.
Failed to resolve com.android.support:support-compat:25.4.0
Failed to resolve com.android.support:support-core-ui:25.4.0
I am trying to include this library to my project by adding
compile 'jp.wasabeef:recyclerview-animators:2.2.7'
so remove this line from gradle
my error just resolved

Android Studio: Dependency issues

compile 'com.github.RogaLabs:social-login:1.2.1'
compile 'com.github.mukeshsolanki:social-login-helper:1.0.2'
compile 'com.github.robertsimoes:Shareable:0.1.0'
compile 'com.github.fccaikai:BottomMenuTutorial:1.0.1'
I tried some other libraries too, they were also giving such type of errors but when I used Glide or Picasso library they synced perfectly.
but getting following error
I am using Gradle version: 2.14.1
At first make sure you add jitpack.io in build.gradle Root level Section .
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
After that Clean-Rebuild and Run .

Android library project can not resolve jitpack dependency

I have an android library uses a module on jitpack, so I change my gradle.build
apply plugin: 'com.android.library'
repositories {
mavenCentral()
// jitpack maven
maven {
url "https://jitpack.io"
}
}
...
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.3.0'
// this is the library
compile 'com.github.ybq:Android-SpinKit:1.0.4'
}
But gradle still can't resolve this library
Error:Failed to resolve: com.github.ybq:Android-SpinKit:1.0.4
Did I missed something ?
A simple answer is that this library is not available in the gradle. Kindly recheck if you are spelling this dependency correctly. This library might be removed by the provider so its not working.
Works fine here.
Try running with -i or -d to get more information about what is happening.
Or maybe now it works and it was a temporary problem? If so, try running with --refresh-dependencies which makes Gradle forget about whether a repository has a dependency or not and requeries it.

Categories

Resources