Share compile dependencies across modules - android

I have an android project with two modules. Both modules depend on android support library. Currently the support library dependency is added to gradle scripts of both modules.
When I upgrade support library version, I have to upgrade the version in both gradle files. I may sometimes forget to do this in both files. So I need a way to declare the support version in a one common place.
This page describes one approach, where you declare the dependencies in a separate file and include it in the root gradle file.
This approach worked for me, but there are some limitations. For an example, if a new support library is available, in previous approach, android studio gave me an inspection warning that a newer version is available. With new approach, android studio no longer does that. Also, whenever you make a change in a gradle file, android studio asks for re-sync the project. But, if I made change to the separated gradle dependency file, android studio doesn't ask me to re-sync.
I tried to directly add the support dependencies to the dependencies section of the root gradle file like given below, but android studio gives me a warning.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
compile "com.android.support:appcompat-v7:23.2.1"
}
}
Warning:
Fix plugin version and sync project.
Does anyone know any other ways which I can declare dependencies in a central place?

The easiest thing to do is declare an extension variable in your buildscript block that manages the entire build. Let's take a Kotlin project, for example, where the version of kotlin must be consistent between all the build components. Here's a top-level build.gradle that defines the buildscript:
buildscript {
ext.kotlin_version = '1.0.1'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Notice that it defines ext.kotlin_version, then uses it below in the plugin dependency.
Then, in the main app module:
dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
I can use the variable again to use the same version of the kotlin stdlib compile dependency. But I only had to define the version string in one place. ext is a special gradle way of declaring extra properties for gradle domain objects. Defining one in buildscript makes it visible elsewhere for use.

Related

Add specific artifact to a Gradle project (Androind)

I need to use, in an Android project using Gradle, a specific artifact of a repositorty.
The artifact is kotlinx-serialization-cbor version 0.20.0 (available in the readme) from the kotlinx serialization GitHub project.
I don't know where and how to add this specific dependencies. (Probably I should add something in "gradle.build" file, the one marked as Project:YourProjectName or the one Module:app
Any help?
The README in the root of the github project explains how to add the plugins to your project. So long as your gradle install is up to date you just need to add a section at the top of your app level build.gradle like this:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.70' // or any other kotlin plugin
id 'org.jetbrains.kotlin.plugin.serialization' version '1.3.70'
}
In the same file make sure jcenter is included in your repositories:
repositories {
jcenter()
}
Then again in the same file add the basic library, as well as the cbor library to the dependencies:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // or "kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0" // JVM dependency
implementation "org.jetbrains.kotlinx:kotlinx-serialization-cbor:0.20.0"
}
You should probably look here https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-serialization-cbor/0.20.0 and copy-paste from Gradle/Maven etc (build system of your choice).

Android Studio: Gradle Sync fail with v4 support library

I have been following the Android fragments tutorial which requests that I set up my project to work with the v4 library by following the Support Library Setup document here.
Here are the steps I followed.
1) Make sure you have downloaded the Android Support Repository using
the SDK Manager.
2) Open the build.gradle file for your application.
3) Add the support library to the dependencies section. For example,
to add the v4 support library, add the following lines:
dependencies {
...
compile "com.android.support:support-v4:24.1.1"
}
After following the three steps I receive these two errors:
Gradle project sync failed...
Error:Could not find method compile() for arguments [com.android.support:support-v4:24.1.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Please install the Android Support Repository from the Android SDK Manager.
According to the Android SDK Manager, I do have the Android Support Repository installed.
I am going to continue investigating this issue as fragments appear to be a valuable tool to develop powerful Android mobile apps. Help approaching this issue would be appreciated.
As Sufian requested in the comments below, here are the contents of my build.gradle file.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
compile "com.android.support:support-v4:24.1.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Here is a screenshot of my Android Standalone SDK Manager showing that the Android Support Repository is installed.
There are two build.gradle files in your project, one rootlevel, and one for your application. You will also see a dependencies section in the other build.gradle. You need to place the compile "com.android.support:support-v4:24.1.1" in there.
You are adding compile "com.android.support:support-v4:24.1.1" in <PROJECT_ROOT>\build.gradle instead you should add the above dependency in <PROJECT_ROOT>\app\build.gradle

How to find out which the latest version of any dependency we include in Gradle is?

In gradle I do not use dynamic versions because that could lead to indeterminate builds.
So I state my dependencies like so:
compile 'com.squareup.okhttp:okhttp:2.2.0'
However, how do I find out if there is a new version available and what its number is?
You can use a gradle-versions-plugin to do it. This plugin provides a dependencyUpdates task, which
Displays a report of the project dependencies that are up-to-date, exceed the latest version found, have upgrades, or failed to be resolved.
All you need to do, is to apply, by configuration of build.script dependencies, as:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.11.3'
// classpath 'org.codehaus.groovy:groovy-backports-compat23:2.3.5' // uncomment if you're using Gradle 1.x
}
}
And applying the plugin itself, with:
apply plugin: 'com.github.ben-manes.versions'
You could use this site to search your library latest version.

Android studio, migrating gradle project to version 1.0

I want to migrate gradle project in Android Studio to version 1.0.
I have the next problem:
Warning:Project app: Only Jar-type local dependencies are supported. Cannot handle: /home/jaume/android-studio-0.8.14/projects/Rutacomandero/app/libs/google-gson-1.7.1-release.zip
Assuming "google-gson-1.7.1-release.zip" is just the gson dependency, then include it in your dependencies section of your build.gradle file, like so:
compile 'com.google.code.gson:gson:1.7.1'
You should be depending on jar files, not zips, and in the case of gson, grab it from central rather than keeping a copy in libs.
Also, 1.7.1 is old, since you're upgrading anyway, try 2.3.1
Had the same problem and I was able to resolve it by adding
repositories {
mavenCentral()
}
so use:
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.5'
}
I exampled version 2.5 but you should check for updates. see https://github.com/google/gson/releases
My answer was found at http://www.javacodegeeks.com/2013/08/getting-started-with-google-gson.html

Use android-maps-utils in Android Studio

I am trying to use this library [1] in an Android project either with Android Studio or with ADT. But it doesn't work at all. In ADT I don't know how to handle gradle stuff and in Android Studio, when I try to "Import Project", I get the error "Could not find com.google.android.gms:play-services:3.1.36.
(don't have enough reputation to post picture, it's on imgur with xswZ3.jpg)
I am not familiar with gradle and I only have a vague idea of what it does but all I want is to use something like BubbleIconFactory f = new BubbleIconFactory(this) in my own project.
Any help is appreciated!
[1] https://github.com/googlemaps/android-maps-utils
Perhaps your problem is needing the repositories outside of the buildscript block.
The repositories internal to the buildscript is for managing the gradle dependency itself, I believe. Here's how I resolved my problem with google-maps-utils as a library dependency. Hopefully this helps. I included my maps and support-v4 libs too.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
// Support Libraries
compile 'com.google.android.gms:play-services:4.1.32'
compile 'com.android.support:support-v4:19.0.1'
compile 'com.google.maps.android:android-maps-utils:0.3+'
}
com.google.android.gms:play-services:3.1.36 can be downloaded by going to your SDK Manager and installing the Extras->Google Repository package (you may want to install the Extras->Android Support Repository as well while you are there). These allow Gradle to automatically use these resources without the need for library projects or jars manually added to your project.
Add the following dependency to your Gradle build file:
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.2+'
}
You'll need to install the "Google Repository" from the Android SDK manager.
See demo/build.gradle for an example.
You can, of course, copy the library directory and use it like any other Android library project.
Let me know if this helps!
Chris
Steps:
First File>Project Structure>Click Plus Button >Import Graddle Project>Select the file(library folder) from the location where downloaded>CLick Ok.
Add this code to dependencies to that app module build.gradle file(remember there are two build.gradle files) :
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.4+'
}
Copy gradle.properties file contents of that Android-maps-util Library project app(found inside that project library folder) TO
gradle.properties file of your project(Simple copy and paste of content to the editor).
Click Sync Project with gradle files button. And you must be fine!

Categories

Resources