Add specific artifact to a Gradle project (Androind) - android

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).

Related

Third Party Android Library Format

I have downloaded a lot of libraries from Github. When I want to add it in android studio, there's no .jar file in every library I have. Why is it that it has no jar file ? How do I add those libraries in android studio?
Whenever possible, you should find the correct line to add to the dependencies block in your build.gradle file. For example, look at the README for the Boom Menu library which you mentioned in your comment. Under the heading Gradle & Maven, you see
compile 'com.nightonke:boommenu:2.1.0'
Add this line to the dependencies block in build.gradle, sync Android Studio, and then use the library as you wish.
libraries from GitHub... Example is the Boom Menu library
This one? ... Well, you're just not reading the documentation
Otherwise, if there isn't a BinTray dependency.
This is exactly what JitPack is for
To get a Git project into your build:
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.User:Repo:Tag'
}
Regarding your questions
Why is it that it has no jar file ?
Because GitHub isn't meant for storing (potentially large) binaries of compiled code, and many Android libraries prefer BinTray over GitHub releases.
How do I add those libraries in android studio?
You could clone them directly into your project
app/
build.gradle
library/
build.gradle
settings.gradle
build.gradle
In settings.gradle
include :app, :library
In app/build.gradle
dependencies {
compile project(":library")
}

Share compile dependencies across modules

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.

How can I specify the latest commit version in Gradle

I'm using Gradle in my Android project,and I have added some dependencies in build.gradle file.For some reasons,I want to point to the latest commit for one of my dependencies.For example:
dependencies {
...
compile 'com.github.ozodrukh:CircularReveal:1.1.0#aar'
}
I'm specifying CircularReveal's version to be 1.1.0#aar,and I know currently it has fixed some bugs but have not released it yet.How can I specify a commit in Gradle?I know some basics about Cocoapods,and it can be done like this:
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af'
Can it be done in Gradle? Any help would be greatly appreciated.
You can not do this directly from Gradle, but there are Gradle plugins and tools you can use to achieve this.
You can do this using Jitpack, an external tool. All you need to do is specify Jitpack as a repository:
repositories {
maven {
url "https://jitpack.io"
}
// Define your other dependency repositories, if any
}
Then include your dependency:
dependencies {
compile 'com.github.ozodrukh:CircularReveal:25aeca505d'
// Include your other dependencies, if any
}
You can also use use the gradle-git-repo-plugin from Layer, but I haven't tried this one yet. An advantage(?) of this plugin is that it clones the repository on your local machine and adds it as a dependency from there.
Ugo response is probably the correct one, here's an alternative for some specific cases:
dependencies {
...
compile 'com.github.ozodrukh:CircularReveal:1.1.+#aar'
// or 1.+ or even just +
}
That puts you on the latest version, no matter which one it is. Now, if you repository builds on a CI environment and deploys snapshots to Sonatype or a similar service, you can do
repositories {
maven {
url "https://project.sonatype.io"
}
}
And along with the other change you'll end up in the -SNAPSHOT versions. This behaviour reports warnings on build because your builds will not be reproducible, but that's a given if you're targeting CI versions.

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

How to compile forked library in Gradle?

I want to compile the following library in my project in build.gradle:
https://github.com/theDazzler/Android-Bootstrap
It is forked from https://github.com/Bearded-Hen/Android-Bootstrap, but no documentation in the repository explains how to include in in project.
I tried something like this:
compile 'com.theDazzler:androidbootstrap:+'
but gradle failed and shows error that library not found.
Edit: Can anyone fork it and/or publish it?
This fork isn't published in the maven central repo.
Then you can't use an import like compile com.theDazzler:androidbootstrap:+
You have to:
- clone this library locally as a module in your project
Clone the
https://github.com/theDazzler/Android-Bootstrap/tree/master/AndroidBootstrap folder in your root/module1 folder.
root:
module1
build.gradle
app
build.gradle
settings.gradle
Change your settings.gradle file in
include ':module1'
include ':app'
In your app/build.gradle file you have to add:
dependencies {
// Module Library
compile project(':module1')
}
Finally in your module1/build.gradle you have to check the level used for gradle plugin.
EDIT 31/10/2015:
You can use another way to add a dependency with a github project,using the github repo and the jitpack plugin
In this case you have to add this repo tp your build.gradle
repositories {
// ...
maven { url "https://jitpack.io" }
}
and the dependency:
dependencies {
compile 'com.github.User:Repo:Tag'
}
It can be simply done by using Jitpack.
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.User:Repo:Tag'
}
for eg: compile 'com.github.sachinvarma:JcPlayer:0.0.1'
The issue is: has that theDazzler/Android-Bootstrap been published anywhere? In any gradle/maven repo?
The usual build.gradle file has a section repositories which should reference that maven repo.
So it is possible any project using theDazzler/Android-Bootstrap should reference the repo where it is published, And with a project like gradle-git-repo-plugin, you could publish that fork on its own release section to publish it.
That task gets wrapped into a publishToGithub task that handles committing and pushing the change. Then you can run
gradle -Porg=layerhq -Prepo=gradle-releases publishToGithub
You can also run
gradle -Porg=layerhq -Prepo=gradle-releases publish
to stage a release in the local github repo and commit it manually.
Hi i had the same issue but with a different project :)
So first you should have the library code on your dev machine.
Next steps are: add a new file called settings.gradle to the root of your project if its not already there.
inside add this:
include 'AndroidBootStrap'
project('AndroidBootStrap').path = "path/to/AndroidBootstrap/AndroidBootStrapLibrary"
also add include for your root project if its not there.
Inside your build.gradle file add
compile project(':AndroidBootStrap')
to add the dependency.
How your folder Structure should look:
root
YourProject
settings.gradle
YourProjectModule
build.gradle
AndroidBootStrap
AndroidBootStrapLibrary
build.gradle
In the end the files look like this:
settings.gradle:
include 'AndroidBootStrap'
project('AndroidBootStrap').path = "../AndroidBootstrap/AndroidBootStrapLibrary"
include 'YourProjectModule'
build.gradle (YourModule):
...
dependencies {
...
compile project(':AndroidBootStrap')
}
Maybe its necessary to modify some point but i hope you get the idea!
Cheers
Neri

Categories

Resources