Adding Maven dependencies to Gradle in Android Studio - android

I am trying to make use of some maven dependencies in a new Android project, but I am new to using Android-Studio and Gradle. I looked at a few other solutions such as this:
How to import Maven dependency in Android Studio/IntelliJ?
and thought it looked simple enough. So I modified the build.gradle file to this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android:android:4.1.1.4'
compile 'com.factual:factual-java-driver:1.7.7-android'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
but when I compiled the project I got the error:
Gradle: A problem occurred configuring root project 'LookAroundYouProject'.
> Failed to notify project evaluation listener.
> Main Manifest missing from /home/tstewart/AndroidStudioProjects/LookAroundYouProject/src/main/AndroidManifest.xml
I am not really sure what this is telling me. Currently it is just a simple hello world example and it worked before modifying the gradle file.
EDIT:
I moved the compile statements from the Top-level build file to the one within the application itself and for some reason this seemed to work and I no longer appear to be getting errors. Sadly I have no idea why this is the case.

Related

Gradle cant find plugin

I am trying to import an external library into my application, the library is FFmpeg. Here is a link to the module.
I add the library to my libs folder (after downloading it), then from File > New > Import module > select FFmpeg. Now within my apps build gradle I add the line
compile project(':FFmpeg')
Now when I try and sync the project I get this error:
Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not found.
When I open the file, this is the affected line
apply plugin: 'com.github.dcendents.android-maven'
Basically my end goal is to get the above lib into my project as an external library so I can modify the source code to fix some bugs within the library.
Any help is much appreciated, I have been trying to fix this problem for a really long time but I am not too good with Gradle besides just using dependencies.
Update 1
Apply problems are now fixed however it cant find variables like version name and rootProject.ext.compileSdkVersion as Integer
Error:(6, 0) Could not get unknown property 'VERSION_NAME' for project ':FFmpegAndroid' of type org.gradle.api.Project.
You need to add the plugin to your classpath root build.gradle. You can found it in the Gradle Android Maven plugin. Something like this:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// This is the classpath for the plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
allprojects {
repositories {
jcenter()
}
}
UPDATED:
You should look at the ffmpeg android gradle.properties and add it to your root project, here it is:
VERSION_NAME=0.3.2
VERSION_CODE=28
GROUP=com.writingminds
POM_DESCRIPTION=Java implementation of ffmpeg for Android
POM_URL=https://github.com/writingminds/ffmpeg-android-java
POM_SCM_URL=https://github.com/writingminds/ffmpeg-android-java.git
POM_SCM_CONNECTION=scm:https://github.com/writingminds/ffmpeg-android-java.git
POM_SCM_DEV_CONNECTION=scm:https://github.com/writingminds/ffmpeg-android-java.git
POM_LICENCE_NAME=GNU GPLv3
POM_LICENCE_URL=https://github.com/writingminds/ffmpeg-android-java/blob/master/LICENSE.GPLv3
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=hiteshsondhi88
POM_DEVELOPER_NAME=Hitesh Sondhi
The VERSION_NAME is needed in the Ffmpeg Android build.gradle:
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: "com.jfrog.bintray"
// This is the library version used when deploying the artifact
version = VERSION_NAME
...
ADVICE:
It's better if you run the ffmpeg-android-java as independent project and then configure it so it can be installed to local maven. Read the details at my answer https://stackoverflow.com/a/46330142/4758255
You have to add the plugin in your buildscript block. You can add in the top-level build.gradle file or in your module/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
//..current plugins
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
About the variables not found you have to check the build.gradle in the library on github
You have to define in your project these variables. Of course use your values.
ext {
compileSdkVersion = 22
buildToolsVersion = '22.0.1'
targetSdkVersion = 22
minSdkVersion = 16
versionCode = 28
versionName = "0.3.2"
}

Building library with gradle which is in a sibling directory

I'm setting up an Android Studio project. This project has a uses some other libs which are included in the Git repository as submodules. So they have to be in the same root folder. (If this is wrong feel free to write an answer too this would fix me problem too.)
As far I understood gradle the dependencies the have to be in a subdirectory to use the compile project ':subdir:subsubdir' command.
I found something about the include command but I did not get it how to use this, this seems also to require files in any subdirectory. So I digged a little more and found the flatDir command in the scope of repositories but how can I use that?
If helpful or not here is my build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile project('../LibraryProjectName:Library')
compile 'com.examlple.library:Library:1.0'
repositories {
flatDir {
dirs '../LibraryProjectName/Library'
}
}
}
I delt with the same tree hierarchy last week and made a simple sample to get the thing done.
Check out this sample on github.
The key is the use of the settings.gradle files. This sample can be built both from the root directory and from the DummyProject directory.

How do I use a library with my Android project using Gradle?

I'm only just starting with Android development so I'm sure this is incredibly basic and a dumb question, but I'm getting the following error when I try and include a library to use in my code with Gradle:
Gradle 'MyApp' project refresh failed: You are using an old, unsupported version of Gradle. Please use version 1.6 or greater.
The library I'm trying to use is ION. All I've tried so far is adding the following lines into the build.gradle file.
dependencies {
compile 'com.koushikdutta.ion:ion:1.1.5'
}
Which I guessed at based on this section of Koush's GitHub account, but what else do I need to do? Still download the jar and put it somewhere? I have looked at other questions/searched for guides on the basics of Gradle, but bizarrely I can't find a solid, definitive answer.
And how do I upgrade Gradle? Can it be done from within Android studio? I'm using Android Studio 0.2.13 on Windows 7 64bit:
SOLUTION: - Make sure you're putting the dependencies in the correct build.gradle file! Doh! It should be in the one just above your src folder, not the root!
Make sure that your buildscript contains the classpath with "gradle:0.5.+". Afterwards, navigate to the root directory of your project (one directory level above the build.gradle file) and (in the command line) run "./gradlew clean"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 18
}
sourceSets {
main {
manifest.srcFile 'MyApp\src\main\AndroidManifest.xml'
}
}
}
dependencies {
compile "com.koushikdutta.ion:ion:1.1.5"
}

Android studio gives error when i add dependencies to build.gradle

I am trying to add AdMob sdk to my android project using Android SDK.
Here is what is did.
i added admob jar file to the lib folder.
added as module
then added dependencies to my build.gradle.
Below is the build.gradle file looks as
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
dependencies{
compile files('libs/android-support-v4.jar','libs/GoogleAdMobAdsSdk-6.4.1')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
Below is the error.
Gradle 'StoriesIndian' project refresh failed:
Could not fetch model of type 'IdeaProject' using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
Build file 'C:\Users\lokesh\Desktop\MyStoryProject\StoriesIndianProject\StoriesIndian\build.gradle' line: 11
A problem occurred evaluating project ':StoriesIndian'.
A problem occurred evaluating project ':StoriesIndian'.
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection) values: [file collection]
Possible solutions: module(java.lang.Object
)
Please help, what do to make it work and where to include module java.lang.object.
thanks

Maven Dependencies with Android Studio / Gradle

I am using the Gradle build system bundled with Android Studio. So far, I am able to build multi-project setups using dependencies that are stored in my project structure. I would now like to use a maven dependency, to no avail. I have written a very simple build.gradle file that always fails :
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.android:support-v4:r7'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
with the following message :
* What went wrong:
A problem occurred configuring project ':absabs'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':absabs:compile'.
> Could not find com.google.android:support-v4:r7.
Required by:
absabs:absabs:unspecified
....
Caused by: org.gradle.api.internal.artifacts.ivyservice.ModuleVersionNotFoundException: Could not find com.google.android:support-v4:r7.
It happens with any artifact I have tried so far. Any idea of what's wrong ?
Thanks
The "repositories" block in the buildscript section only applies to the build environment. You also need to specify which repository to use when resolving dependencies for building your project, so you need to put something like the following in your build.gradle file:
repositories {
mavenCentral()
}
Your complete file would look something like this:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android:support-v4:r7'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
Note the two instances of "repositories". You can read more about what this actually means in the gradle docs.
Seems that current Android Studio version doesn't pick up new dependencies immediately. Try to restart IDE.
Edit:
This is not needed for Android Studio >= 0.1.4v. It has build in action Sync Project with Gradle file. You can find it under Tools > Android > Sync Project with Gradle file or just button in Toolbar.
http://tools.android.com/tech-docs/new-build-system/user-guide
Note: This only affects the code running the build, not the project. The project itself needs to declare its own repositories and dependencies. This will be covered later.
So you have to declare
repositories {
mavenCentral()
}
in your project scope once more.
I did it this way:
In Top build.gradle (Project: YourProject) I added:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.h2database:h2:1.4.187'
//NOTE: you can get the latest version in http://mvnrepository.com/artifact/com.h2database/h2
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
NOTE: I added this along with the predefined jcenter() repositories.
And then for my app/build.gradle file I added whichever library or dependency I needed on:
dependencies {
....//Add dependency here
}
I recently had to use a maven dependency in gradle, maybe this little example will be of use for someone.
In maven:
<dependency>
<groupId>com.turo</groupId>
<artifactId>pushy</artifactId>
<version>0.12.1</version>
</dependency>
In grade that turns into:
repositories {
mavenCentral()
}
dependencies {
compile 'com.turo:pushy:0.12.1'
}
maven
Maven continues using XML as the format to write build specification. However, structure is diametrically different. Maven has its own problems. Dependencies management does not handle well conflicts between different versions of the same library (something Ivy is much better at). XML as the build configuration format is strictly structured and highly standardized. Customization of targets (goals) is hard. Since Maven is focused mostly on dependency management, complex, customized build scripts are actually harder to write in Maven than in gradel.

Categories

Resources