Android Studio missing external dependencies - gradle

I have a library project. I want to use Android's new build system. Currently I'm encountering a quite annoying scenario.
I have my dependencies defined on gradle.build but they never appear under External Libraries in Android Studio. Hence all the references to those libraries are marked as errors.
When I run gradle dependencies on the command line it shows the full dependencies tree and compiles successfully. The problem clearly is with Android Studio.
I tried to restart the IDE/OS but nothing.
This is my gradle.build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.1'
}
}
apply plugin: 'android-library'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'junit:junit:4.11'
compile 'org.robolectric:robolectric:2.1:jar-with-dependencies'
compile 'com.google.android:android:4.1.1.4'
compile 'com.google.android:support-v4:r7'
compile 'info.cukes:cucumber-java:1.1.3'
compile 'info.cukes:cucumber-junit:1.1.3'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
versionCode 1
versionName "0.3-SNAPSHOT"
minSdkVersion 15
targetSdkVersion 17
}
}
UPDATE
This issue seems to be fixed on latest Android Studio version (0.2.5)

You can use 'Tools->Android->Sync project with Gradle files'. It will resolve the dependencies, download, and add them to external libraries.

Unfortunately, in the current version of Android Studio, the IDE is not completely integrated with the build system (gradle). You have to add the library once in gradle.build for compilation, and once via the GUI for code completion.
Right click on your project, select "Open Module Settings". Acknowledge the warning. Select "Libraries", "+", and add the library you are using. You can search for libraries on Maven in the dialog that appears. You should select your libs dir for the jar. Finally, add the library to your code's module. If your app is MyApp, you probably have MyApp and MyAppProject. You need to add it to MyApp. (You can probably also directly add it from the "Modules" page.)
To additionally get gradle to add the jar to your apk, make the following changes to your grade file. Replace:
compile 'org.jsoup:jsoup:1.6.1'
and similar with
compile files('libs/jsoup-1.6.1.jar')
Now it should all work.

I commented following line from build.gradle file and it started working.
//compile files('libs/android-support-v4.jar')
Previously dry build operation would succeed but when I would try to run on device there would be error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;

Related

Issues when Integrating Crashlytics in Android Studio

I'm Getting this Error when I try to integrate Crashlytics with my Project.
I used the Crashlytics plugin to generate the code . But when I do a sync in Gradle it gives e the following error. I have also attavhed the build.gradle
Error:Unable to find method 'org.apache.http.conn.ssl.SSLConnectionSocketFactory.(Ljavax/net/ssl/SSLContext;Ljavax/net/ssl/HostnameVerifier;)V'.
Possible causes for this unexpected error include:Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
I have tried Invalidating the cache and also Redownload dependencies and also stoping the Gradle build and starting it.
I'm also connected to the Internet.
The Build works fine if I remove the crashlytics dependency.
Can anyone tell e how to fix this.
build.gradle
buildscript {
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
}
}
dependencies {
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:support-v13:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:palette-v7:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile files('libs/volley.jar')
compile('com.crashlytics.sdk.android:crashlytics:2.6.5#aar') {
transitive = true;
}
}
Solved the Issue. This happened because I was using an older Version of java which has an Incompatable httpcomponents jar, after switching to the latest version the issue was resolved.

How to fix Plugin with id 'com.github.dcendents.android-maven' not found. in android studio

I am creating an android application in android studio. I am adding a library to my project which was downloaded from here . When i was adding this library to my project it was showing an error called "Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not found.". Please tell me how to fix it.
In top level build.gradle dependencies, paste this classpaths.
I wonder why cant i've seen this method in websites.
Anyway, I fixed it using this way.
dependencies {
//YOUR DEPEDENCIES
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
There come two scenarios here:
If you are using apply plugin: 'com.github.dcendents.android-maven' in your module level gradle.
This plugin is generally used to distribute and upload your library project to bintaray.
In this case, all you have to do is make use of correct combinations of gradle plugin and maven-gradle-plugin.
The combination of the following versions is working for me:
Inside project level gradle
classpath 'com.android.tools.build:gradle:2.3.0+'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
and in gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
In module level gradle:apply plugin: 'com.github.dcendents.android-maven'
You can check the compatible versions list from Github
If you are not using it, as it is in your case just remove
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
and
apply plugin: com.github.dcendents.android-maven
Just delete the line "apply plugin: 'android-maven'"
in the beginning of build.gradle,
apply plugin: 'com.android.application'
apply plugin: 'android-maven'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
the project doesn't need maven.
Try to add these lines to your project's build.gradle file into dependencies block:
classpath 'com.github.dcendents:android-maven-plugin:1.2'
Like this
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
It just worked for me.
For a Gradle 4.1+ you could do in Project-level build.gradle:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
plugins {
id "com.jfrog.bintray" version "1.7.3"
id "com.github.dcendents.android-maven" version "2.0"
}
Add these lines in project.gradle dependencies:
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
Just add this two line in your gradle file
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
For a Gradle 7, I deleted this line in the build.gradle module level:
apply plugin: 'com.github.dcendents.android-maven'
With reference to this answer posted above, I encountered another problem No service of type Factory available in ProjectScopeServices after using it.
I fixed it by including com.github.dcendents:android-maven-gradle-plugin:1.4.1 ( as mentioned in this answer to the above linked question) instead of com.github.dcendents:android-maven-plugin:1.2 in the dependencies in the project gradle.
Don't be too confused. android-maven-gradle-plugin:1.4.1 is only an updated version of the android-maven-plugin:1.2 . As mentioned in the Readme in the git repo for this plugin, dcendents mentioned that he was requested to rename the plugin name by Android maven plugin developers.
This is all depend your gradle version. please check https://github.com/dcendents/android-maven-gradle-plugin i found my solution in there.
dependencies {
// The gradle plugin and the maven plugin have to be updated after each version of Android
// studio comes out
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
finally i can solve this error after trying three days
the solution is very simple just remove the module or library project completely from your project and use gradle dependency instead.
Just copy this in your app module's build.gradle inside dependencies closure
dependencies {
// YOUR OTHER DEPENDENCIES
compile 'com.github.navasmdc:MaterialDesign:1.+#aar'}
to success make this steps when you r online
If in your project any module using this id then you must declare below two dependency at your project level build.gradle file -
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
the problem is that android just, don't know the repos and you must specify the repository like that:
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.github.dcendents:android-maven-gradle-plugin:2.1")
}
}
apply plugin: 'com.github.dcendents.android-maven'
add following under project level gradle file
dependencies {
...
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
Upgrade your Github library version as your Gradle version and Github library version doesn't match.
Check the version compatibility here:
https://github.com/dcendents/android-maven-gradle-plugin
It probably causes from android sdk . So i don't know how to occur it but i solved it following these steps.
If you see these warnings on the begining of the console when you enter cordova build or run etc.
ANDROID_SDK_ROOT=undefined (recommended setting)
ANDROID_HOME=C:\anypath (DEPRECATED)
Firstly, need to configure ANDROID_HOME path
https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html#setting-environment-variables
In windows pc, you should enter path from environment variables settings.
https://www.youtube.com/watch?v=S5wqTSuL3j4 ( this video may help you )
Option-1 Then you can add new gradle version https://gradle.org/install/
Option-2 Replace gradle version on /platforms/android/app/build.gradle
from classpath 'com.android.tools.build:gradle:X.X.X
to classpath 'com.android.tools.build:gradle:3.0.1
You can find the right version of gradle by entering command editor:
gradlew --version or gradle --version
or checking this file in your project then find "distributionUrl"
platforms\android\gradle\wrapper\gradle-wrapper.properties
distributionUrl=\https://services.gradle.org/distributions/gradle-4.4-all.zip
Hope it helps you.

Gradle 'Build Script error' occurs when I attempt to use testCompile in dependencies

I'm working with Android Studio and in my dependencies for my application I attempting to add a testCompile dependency as listed here: http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
When I sync my file I get the error:
I don't understand what is going on, my gradle build file in my root folder is set to classpath 'com.android.tools.build:gradle:0.12.+' and that's the most recent version. Why doesn't it recognize testCompile? I don't want to deploy test dependencies to production... Any helps would be appreciated.
EDIT: Here is the project build file
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
and here is the src build file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 17
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.edu.myApp"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/scribe-1.3.5.jar')
compile files('libs/json_simple-1.1.jar')
compile 'com.google.android.gms:play-services:5.0.77'
// Can't be higher than 19 if we want to support smaller android versions
compile 'com.android.support:appcompat-v7:19.+'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
// This Mockito includes all dependancies (great for us)
testCompile "org.mockito:mockito-core:1.9.+"
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile 'org.objenesis:objenesis:1.2'
}
You should use androidTestCompile, not testCompile. If this is due to modifying the dependency scope via the Project Structure dialog, then there's a bug where it uses the wrong statement to set up the dependency. I've filed https://code.google.com/p/android/issues/detail?id=74771 for this.
I stumbled upon this post a year later. I was having this problem because I inherited an older project that was using an out-of-date Gradle build tools setting in the Gradle build file. I fixed this problem by updating Gradle build tools, which you need to do apparently by incrementing the version number in the Gradle build file:
dependencies {
classpath 'com.android.tools.build:gradle:1.X.X'
}
where X.X is the latest gradle build tools version number. Right now I'm using Android Studio v1.2 and I've got my build tools version number set to:
dependencies {
classpath 'com.android.tools.build:gradle:1.2.2'
}
I came across this problem when I was trying to implement unit tests, which are now built-in to Android Studio apparently.
Hope this helps someone with the same problem.
I got this "method not found 'testcompile()'" error also. The problem was I had testCompile 'junit:junit:4.12' in my project build.gradle file and not in by app build.gradle file. How could I have known it was in the wrong file? The project file only states "NOTE: Do not place your application dependencies here; they belong in the individual module build.gradle files". Such a simple answer no wonder I couldn't find it.
I have a library project. I believed the Android documentation and put the testCompile statement in my top level build.gradle file. Turns out it actually had to go in my module build.gradle file.

Unsupported Gradle DSL method found: 'compile()'!

I'm going through Google's documentation on "Add Google Play Services to Your Project" in Android Studio:
https://developer.android.com/google/play-services/setup.html
I'm using that documentation to modify the build.gradle file of a freshly created Android project. In Step 2 (Add Google Play Services to Your Project), it states:
Add this line:
apply plugin: 'android'
Under Dependencies, add this:
compile 'com.google.android.gms:play-services:5.0.77'
It also says to update that version after updating Google Play Services, which is now at 18 according to Android SDK Manager.
Here is my entire build.gradle file at the top-level (parent of this file is the root folder).
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'android'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile 'com.google.android.gms:play-services:18'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Upon saving, it prompts for a Sync. I Sync it, but get:
Build script error, unsupported Gradle DSL method found: 'compile()'!
Error:(10, 0) Possible causes could be:
- you are using a Gradle version where the method is absent
- you didn't apply Gradle plugin which provides the method
- or there is a mistake in a build script
I'm using Android Studio 0.8.2. I didn't install Gradle, just using the plugin that came with Android Studio.
It's interesting to note that the build.gradle file generated when I made this new project says:
//NOTE: Do not place your application dependencies here
But Google's documentation says (which conflicts with the above):
Note: Android Studio projects contain a top-level build.gradle file and a build.gradle
file for each module. Be sure to edit the file for your application module.
What's wrong with my build.gradle file (or environment)?
The Google documentation you quoted is correct, and doesn't conflict. There's more than one build.gradle file. Instead of putting dependencies in the top-level one as you have, put them in the build file that's in your module's directory.
Also, don't put an apply plugin: 'android' statement in that top-level build file; it will cause an error.
You can also add dependencies through the Project Structure UI, which does the right thing.
Do not add dependencies in your project by editing its most 'external' build.gradle (YourProject/build.gradle). Edit the one that is under the 'app' module instead (YourProject/app/build.gradle).
There, by the way, you will find the declaration of one dependency, such as:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
This block will be just below android { ... } configuration block.
In my case, I am just adding leeloo dependencies, so it became:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'net.smartam.leeloo:oauth2-client:0.1'
compile 'net.smartam.leeloo:oauth2-common:0.1'
}
Then sync your project and dependencies will be downloaded. Hope it helps!
the compile-time dependencies should reside in the dependencies block under allprojects, not under buildscript:
apply plugin: 'android'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
allprojects {
repositories {
jcenter()
}
dependencies {
compile 'com.google.android.gms:play-services:18'
}
}
This should work fine.
Think of “Gradle DSL method” as a Java method. So in Gradle, methods can be distinguished by either {} or “.”. So
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
is the same as
dependencies.compile fileTree(dir: 'libs', include: ['*.jar'])
where both “dependencies” and “compile” are methods.
So you are including a method somewhere in your build.gradle file that is not supported by your program. For example, make your dependencies this:
dependencies {
nothing 'this.does.nothing.build:gradle:0.7.+'
}
Which is the same as writing:
dependencies.nothing 'this.does.nothing.build:gradle:0.7.+'
And you will see an error saying “unsupported Gradle DSL method found: ‘nothing()’!”
Obviously "nothing" is not a real method. I just made it up.
So one of your "compile" methods inside your build.gradle is wrong.
When I faced this problem I used android developer UI to import dependencies as follows:-
1 Go to View ---> Open Module Settings
Select Dependency tab. Click + to add a dependency and select Library dependency. Choose the downloaded library here.

Android Gradle dependencies - only Maven?

I'm trying to get to know the Android Studio / Gradle build system, having come from Eclipse and Ant. In particular, I don't understand how the dependencies block in my build.gradle file works.
My current project has the following structure:
In my project I am using both the android support library (v4), and the jxl spreadsheet library. My build.gradle (the one inside the sub-module, not the root level one) currently looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
buildTypes {
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
}
}
productFlavors {
defaultFlavor {
proguardFile 'proguard-rules.txt'
}
}
}
dependencies {
compile 'net.sourceforge.jexcelapi:jxl:2.6.+'
compile 'com.android.support:support-v4:13.0.+'
//compile fileTree(dir: 'libs', include: '*.jar')
}
On the second last line, you can see I've tried using the local copies of android-support-v4.jar and jxl.jar. I've also tried using the lines
compile files('libs/android-support-v4.jar')
compile files('libs/jxl.jar')
However, whenever I try to use the local .jar files, my build fails, saying that the Android support and jxl libraries cannot be found. I've seen lots of posts saying that you can simply use local .jars like this, however I cannot get this to work. If possible, I would like to be able to use my locally stored .jar files so I can work offline, when Maven isn't available.
Can anyone tell me why using the local .jar files doesn't work?
EDIT: I've also tried restarting the Android Studio IDE, and cleaning and re-building my project after adding the local jar dependency lines.
It's not finding the libs directory because it needs to be located at your module root instead of inside src/main. The paths in build.gradle are relative to the location of the build.gradle file, which lives in your module root.
For the Android support library, I'd recommend using the Maven dependency (e.g. compile 'com.android.support:support-v4:13.0.+') instead of including the jar file. With the support library, the Android Gradle plugin actually looks for it in your SDK instead of going out to the network for it; you have to have the support repository installed via the SDK manager. This is actually a little confusing and trips up a lot of users since it's not well-documented. But if you access it this way instead of just including the JAR, then the build system can be smarter about not trying to include duplicate copies of it and causing errors if you include other library projects that depend on it.

Resources