Android Gradle dependencies - only Maven? - 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.

Related

local dependencies from root multi project build in gradle

I have been working on an android app built using the ResearchStack skin framework pulled in through the gradle dependency:
implementation 'org.researchstack:skin:1.1.2'.
I am interested in possibly doing some customization to the underlying ResearchStack skin framework:
https://github.com/ResearchStack/ResearchStack.
Can anyone walk me through updating my project to be dependent on local ResearchStack build artifacts so that I can make changes to the framework and see those changes in my local application.
I tried just pointing to the build output:
implementation fileTree(dir: '../../ResearchStack/skin/build/outputs/aar', include: ['*.aar'])
but that leaves me with unresolved transitive dependencies and some build issues that I couldn't quite get worked out.
Thanks!!
John,
Put 'ResearchStack skin framework code' in another directory 'myResearchStack' at root level parallel to 'app' directory.
And update the root level 'build.gradle' as
apply plugin: 'com.android.application'
android { ... }
dependencies {
// Dependency on a local library module
implementation project(":myResearchStack")
}

.kts script in a Gradle project

I have a Kotlin Gradle project.
If I create a .kts file it runs in InteliJ alright except when it is in the /src/main/kotlin folder.
IDEA highlights the whole file in red.
Gradle throws out compilation exception.
The exception is
...src/main/kotlin/test.kts: (3, 1): Cannot access script base class 'kotlin.script.templates.standard.ScriptTemplateWithArgs'. Check your module classpath for missing or conflicting dependencies`.
What is the problem?
My build.gradle:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.0-rc-131'
}
group 'kotlin.tutorials.coroutines'
version '1.0-SNAPSHOT'
repositories {
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
mavenCentral()
jcenter()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
ext.ktor_version = '1.0.0-alpha-1'
ext.coroutines_version = '0.30.2-eap13'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "ch.qos.logback:logback-classic:1.2.3"
//KTOR features
compile "io.ktor:ktor-jackson:$ktor_version"
compile "io.ktor:ktor-auth:$ktor_version"
compile "io.ktor:ktor-auth-jwt:$ktor_version"
compile "io.ktor:ktor-freemarker:$ktor_version"
compile "io.ktor:ktor-html-builder:$ktor_version"
}
compileKotlin.kotlinOptions.jvmTarget = "1.8"
compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
.kts files should go to the src/main/resources folder since src/main/kotlin is for .kt files.
Scripts are a completely different animal in this sense, and you should use something like KtsRunner to execute them.
Related question is here.
If you just want to use scripts from IDEA, then you should use Scratch files which are supported out of the box.
The solution turned out to be very straight forward.
The compiler could not find utility classes that are usually added to any Kotlin script classpath. Adding one dependency to my build.gradle fixed it:
dependencies {
compile "org.jetbrains.kotlin:kotlin-scripting-jvm"
}
P.S.
I created 2 tickets to improve Kotlin script support in InteliJ:
https://youtrack.jetbrains.com/issue/KT-27542
https://youtrack.jetbrains.com/issue/KT-27544
If you care about those features, please vote them up!

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 Studio missing external dependencies

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;

Resources