Add GitHub library as dependency to Android Studio project - android

I am trying to implement ActionBar-PullToRefresh from https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/QuickStart-ABC. I just made the switch from Eclipse to Android Studio so I am totally new to AS and Gradle.
chrisbanes writes on the site:
The easiest way to add ActionBar-PullToRefresh to your project is via
Gradle, you just need to add the following dependency to your
build.gradle:
dependencies {
mavenCentral()
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Does this mean that I don't have to download the library and Gradle takes care of it so that I always have the latest version? I just don't know where to put the above line. I have two gradle.build files one in my root that looks like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
and the one in my project which looks like:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.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']
}
// 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')
}
}
Do I have to add a repository somewhere?

It will work when you put this line in your project build.gradle, in the dependencies section:
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
Also, add:
repositories {
mavenCentral()
}
So:
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Gradle will download the needed resources automatically for you.

Use https://jitpack.io/
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.User:Repo:Tag'
}

This is for Kotlin DSL (build.gradle.kts):
repositories {
// Other repositories...
maven("https://jitpack.io")
// OR maven { url = uri("https://jitpack.io") }
}
There are two places inside which you can insert the above code block:
The traditional way: inside top-level build.gradle.kts file
New way: inside a dependencyResolutionManagement { block in settings.gradle.kts file;
read more about this new feature at Gradle user guide: Centralized Repository Declaration

Related

Gradle DSL method not found: 'androidtestcompile()'

I am getting an error like this when I am trying to import build.gradle file which I exported from Eclipse
build.gradle code:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
app module build.gradle which has been generated by Eclipse looks like this
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':android-crop-master:lib:src:main')
compile project(':sharebubbles_menu')
compile project(':braintree-1.2.5-project')
compile project(':facebook')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
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')
}
}
The following is screenshot.
You should update the version of gradle/gradle-plugin/AS that you are using.
Use these versions:
- AS 1.1.0
- Gradle 2.2.1
- Gradle plugin 1.1.0
Then change your build script:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
And your module script:
apply plugin: 'com.android.application'
dependencies {
// The same
}
android {
//Rename your instrumentTest folders to androidTest, e.g. git mv app/src/instrumentTest app/src/androidTest.
// Move the tests to tests/java, tests/res, etc...
androidTestCompile.setRoot('tests')
}
}
All info about the Instrumentation Tests migration is here
I was following the instructions from the Android Developer's page for Instrumented Unit Tests.
https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html
I was getting the same error. The errors went away when I moved my edits from the gradle.build (Project: xxx) file to the gradle.build (Module: App).
Thankfully I noticed that there are two gradle.build files or I would still be trying to compile now.

Gradle in Android Studio: Failed to resolve third-party libraries

I have been trying to switch my project from Intellij to Android Studio, which has required me to create a build.gradle file. I know I can add each of these as a library dependency, but I ideally want to be able to get the maven repository dependency working.
Every time I sync, my support libraries are synced fine, but for each third-party library, I get something like
"Error:(30, 13) Failed to resolve:
com.facebook.android:facebook-android-sdk:3.23.1"
for each library.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
// Google Play Services
compile 'com.google.android.gms:play-services:6.5.87'
// Support Libraries
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:gridlayout-v7:21.0.3'
compile 'com.android.support:mediarouter-v7:21.0.3'
compile 'com.android.support:palette-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'
compile 'com.android.support:support-annotations:21.0.3'
compile 'com.android.support:support-v13:21.0.3'
compile 'com.android.support:support-v4:22.0.0'
// third-party libraries
compile 'com.amazonaws:aws-java-sdk:1.9.24'
compile 'com.facebook.android:facebook-android-sdk:3.23.1'
compile 'com.github.markushi:android-ui:1.2'
compile 'de.hdodenhof:circleimageview:1.2.2'
compile 'it.neokree:MaterialNavigationDrawer:1.3.2'
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
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')
}
}
Add:
repositories {
mavenCentral()
}
to the build.gradle. Now you have repositories defined only in build script which resolves dependencies only for the buildscript itself not for the project.
Just to share infomation, I got same problem and the solution was different.
In my case, proxy server was used and it causes the problem. I needed to configure https proxy settings, as discussed in gradle behind proxy in Android Studio 1.3.
If you use VPN | Proxy on your system then use your proxy info in the gradle.properties file in your project like the following lines of code:
# HTTP Proxy
systemProp.http.proxyHost={Host Address}
systemProp.http.proxyPort={Port Number}
systemProp.http.proxyUser={Proxy Username}
systemProp.http.proxyPassword={Proxy Password}
systemProp.http.nonProxyHosts={NonProxy Hosts Address} # like: 127.0.0.1,localhost
# HTTPS Proxy
systemProp.https.proxyHost={Host Address}
systemProp.https.proxyPort={Port Number}
systemProp.https.proxyUser={Proxy Username}
systemProp.https.proxyPassword={Proxy Password}
systemProp.https.nonProxyHosts={NonProxy Hosts Address} # like: 127.0.0.1,localhost
Now just replace {.....} in the above code with appropriate data
Also you can set Android studio proxies like the following image by your proxy info in File>Settings:
Now test again...!
repositories {
jcenter {
url "http://jcenter.bintray.com/"
}
// mavenCentral()
}
this helps
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
in projects's gradle
Maybe this help
allprojects {
repositories {
jcenter()
}
}
well , if your network connection is fine(wheter using proxy | VPNs or not), simply just try turn off'offline mode' and sync when using android studio with gradle 2.3,this worked for me :)
Most of the settings do not currently work with new Android updates, so the solution currently is to add this section to buuild.Gradle(Project:)
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
}
}
******************************************
// Add this section if it does not exist
repositories {
mavenCentral()
}
******************************************
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
**********************************************
// Add this section if it does not exist
gradlePluginPortal()
**********************************************
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

Gradle DSL method not found: "classpath()"

I am working on an Android project, which used Gradle as mentioned below.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:darkmoon:darul-android:vitamio:vitamio')
compile project(':Dev:adt-bundle-mac-x86_64:sdk:extras:google:google_play_services:libproject:google-play-services_lib')
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
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')
}
}
But when I build it, keep receiving this error: "Gradle DSL method not found", and it pointed to the
following line:
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:epsilonmobile:darul-android:vitamio:vitamio')
compile project(':Dev:adt-bundle-mac-x86_64:sdk:extras:google:google_play_services:libproject:google-play-services_lib')
}
Apologize if this question is a bit noob, I'm new to both Gradle and Android Studio
A classpath configuration is only available for buildscript dependencies. You need to get rid of the line classpath 'com.android.tools.build:gradle:0.12.+' in the top-level dependencies block. (Gradle plugins need to be declared under buildscript { dependencies { ... } }.)
In a gradle script, the buildscript is a special section where you can declare dependencies of the build script itself (i.e. binaries required by the build process).
The gradle build process is nothing more than a java process and so it supports normal classpath dependencies.
com.android.tools.build:gradle:0.12.+ identify a binary required by the build process (it contains code able to understand/execute the android section of the build script).
The android apk that will be build by this script don't needs the binary com.android.tools.build:gradle:0.12.+ to run on your android device (i.e. the apk is of course already build when it run on the device) : there is no reason to declare it again in the top level dependencies
(those are the dependencies required by your app)

Add Cardslib library To Android Studio 0.8.1

This is my first project using Android Studio so spare me if you find this question naive. I am trying to include the Cardslib library to my project in Android Studio (version 0.8.1). Initially I tried to include it by adding the following line in build.gradle:
compile 'com.github.gabrielemariotti.cards:library:1.7.3'
But it returned the following error (upon sync)
Error:Failed to find: com.github.gabrielemariotti.cards:library:1.7.3
The I tried to include the jar file by,
Downloading it from maven repository
Adding jar file to libs folder.
Adding following line in build.gradle
compile files('libs/library-1.7.3-sources.jar')`
Though gradle project sync without any error but I am not able to create simple cards i.e still not working for me.
I wanted the first method to work since Android Studio would then handle everything but I guess I am doing something horribly wrong.
[Edit] - Adding the build.gradle code
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.github.gabrielemariotti.cards:library:1.7.3'
}
android {
compileSdkVersion 19
buildToolsVersion '20.0.0'
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')
}
}
Add this block in your script to tell gradle where it can find the repo with libs.
repositories {
mavenCentral()
}
So your script will be something like this:
.......
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.github.gabrielemariotti.cards:library:1.7.3'
}
.......

"Could not resolve all dependencies" with 3rd party libs (from Maven Central)

In my build.gradle, I define some 3rd party libs, all of which are available in Maven Central.
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.guava:guava:15.0'
compile 'com.netflix.rxjava:rxjava-core:0.14.2'
compile 'com.netflix.rxjava:rxjava-android:0.14.2'
}
(We also have manually managed jars under libs (for Eclipse users), and I used to use those
for Gradle builds too, with compile fileTree(dir: 'libs', include: '*.jar'), but I'm trying to move away from that towards simpler, automated dependency management.)
Anyway, for some reason fetching the deps fails:
* What went wrong:
A problem occurred configuring root project 'MyApp-Android'.
> Could not resolve all dependencies for configuration ':_debugCompile'.
> Could not find com.google.code.gson:gson:2.2.4.
Required by:
:MyApp-Android:unspecified
> Could not find com.google.guava:guava:15.0.
Required by:
:MyApp-Android:unspecified
> Could not find com.netflix.rxjava:rxjava-core:0.14.2.
Required by:
:MyApp-Android:unspecified
> Could not find com.netflix.rxjava:rxjava-android:0.14.2.
Required by:
:MyApp-Android:unspecified
What am I doing wrong?
I was wondering if I should have
repositories {
mavenCentral()
}
...also on the top level of the build file (not just inside buildscript), but adding that didn't help.
Resolution
I was too hasty; that did help with "Could not resolve dependencies". After that I got compilation errors, but that was due to in fact having some more dependendies in the code (and in libs) than the four entries in dependendies above.
So in my case I had to add the top-level repositories pointing to Maven Central plus some additional dependiencies that we actually had:
compile 'com.squareup.okhttp:okhttp:1.2.1'
compile 'com.android.support:support-v4:13.0.0'
compile 'com.android.support:support-v13:13.0.0'
The (original, broken) full build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.1'
}
}
apply plugin: 'android'
dependencies {
// compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.guava:guava:15.0'
compile 'com.netflix.rxjava:rxjava-core:0.14.2'
compile 'com.netflix.rxjava:rxjava-android:0.14.2'
}
android {
compileSdkVersion 19
buildToolsVersion "19"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
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')
}
}
Indeed you need to add a
repositories {
mavenCentral()
}
at the top level of your build.gradle to specify where to search for your dependencies.
buildscript {
repositories {
google()
=> mavenCentral() <=
}

Categories

Resources