Gradle - inherit repositories from a module - android

First of all, I explain my project setups. I use the words Project/Module as used in Android Studio.
I have a setup of my projects like following:
LibraryProject => a project that just groups my libraries, that I use in other projects, if necessary
BackupLibrary (Module)
DatabseLibrary (Module)
MainUtilityLibrary (Module)
DialogLibrary (Module)
MyProject1
App (Project)
DialogLibrary (Module, referenced from DIRFFERENT project)
My Project1 now looks like following:
1) I reference the module from the other project, that I want to use (done in settings.gradle (Project Settings) file:
include ':app'
include ':dialogLibrary '
project(':dialogLibrary ').projectDir = new File(settingsDir, '../LibraryProject/DialogLibrary ')
2) I add the foreign library module as a dependency to my app's build.gradle:
dependencies {
compile project(':dialogLibrary ')
}
My LibraryProject looks like following:
I add the repository and the dependency directly to the module's `build.gradle' (this is enough to build the module on it's own without a problem):
allprojects {
repositories {
maven { url 'https://dl.bintray.com/drummer-aidan/maven' }
}
}
dependencies {
compile 'com.afollestad:material-dialogs:0.7.5.2'
}
Problem
The setup so far does not work for MyProject1. I have to add maven { url 'https://dl.bintray.com/drummer-aidan/maven' } to MyProject1 as well, the compile project(':dialogLibrary ') is not enough. I have to add the dependency that should somehow be inherited from the LibraryModule to MyProject1 Project build.gradleagain like following:
allprojects {
repositories {
mavenCentral()
maven { url 'https://dl.bintray.com/drummer-aidan/maven' } // com.afollestad:material-dialogs
}
}
Otherwise, i can't compile my project. This way seems like doing something twice that probably can be done automatically. But how? This way, whenever I include a module from my LibraryProject, I have to check if I need an additional maven repo...

I think it has to do with the fact that Gradle has no knowledge of the LibraryProject's settings.gradle file while building MyProject1. So it includes the dialogLibrary as if it is a normal subproject of it's own. This explains the reason why the Maven repo is not know to MyProject; LibraryProject's configuration has not been loaded.
Maybe you can include the dialogLibrary through the LibraryProject? Although I did some testing and could NOT get it to work. It probably has to do with a limitation of Gradle. There is a StackOverflow post about multiple settings.gradle files here.
A work around could be to build the Modules of the LibraryProject and use them instead. For example build the DialogLibrary to a jar, upload it to a Maven repository and than in your App project just include DialogLibrary as normal dependency (don't forget to include the Maven repo).

Related

Maven publication of multi-modules android library

I am working on an Android SDK made of multiple library modules and a test app module:
mySDK
|-test-app
|-lib-core
|-lib-ui
|-...
I would like to publish it on a Maven repository, with all library modules embedded (but not the test-app).
I know how to publish a single library module using Maven Publish Plugin but can't figure out how to make a Maven publication containing multiple library modules.
How would you do that ?
Just upload each module as a standalone module.
It is not mandatory to upload all the modules at the same time, the dependencies are just described in the pom file.
It can be useful putting a base script in a single gradle file with some common settings and properties (you can read them from a gradle.properties file), something like:
rootFolder/maven_push.gradle:
apply plugin: 'maven'
//....
pom.artifactId = POM_ARTIFACT_ID
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL
//...
}
//...
and then in each module/build.gradle file add:
apply from: '../maven_push.gradle'
Each module is a library by itself, so configure each one of them (besides the app) to be packaged as AAR files and deployed to the desired repo. If you are going to use the Maven publish plugin, just apply the steps to each module(to the module's build.gradle files). A good practice is to centralize the groupId and version values, perhaps in the gradle.properties file, or in the main gradle file. Same procedure applies to other plugins, like the android-gradle-maven plugin.

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")
}

How to develop a library and an application side-by-side in Android Studio?

I'm currently developing both a library (with no activities) and an application that depends on the library. Currently, I have these as separate projects, and I can copy the generated .aar file from the library project into the application project's libs folder, and re-sync gradle. However, this is an inefficient process because I have to rebuild and manually re-copy the .aar file every time I make a change to the library project. My question is, how can I streamline this process so that my application automatically uses the library's most recently generated .aar file?
1) In your app's settings.gradle include your lib as a project:
include ':lib-project'
project(':lib-project').projectDir = new File('../path/to/lib/project/lib-project')
The path to your lib project is relative to the settings.gradle location on your filesystem
2) in your app's build.gradle add lib project as a dependency:
dependencies {
compile project(':lib-project')
...
}
how can I streamline this process so that my application automatically uses the library's most recently generated .aar file?
Option #1: Dedicated Library
Step #1: Put your app project and the library project as children of a common root directory for the overall project. For the purposes of this answer, I'll call these app/ and library/, respectively.
Step #2: In the top level (i.e., the common root directory), have a settings.gradle file that lists these modules:
include ':app', ':library'
Step #3: In the top level, have a build.gradle file that sets up the Gradle for Android plugin and any other common stuff of interest, such as:
// 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.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
(note that the above file is what you get from a native Android Studio project, created by the IDE)
Step #3: In the library/ directory, have a build.gradle file that uses the com.android.library plugin
Step #4: In the app/ directory, have a build.gradle file that has compile project(':library') in its dependencies to pull in the library
It may be that your AAR is the deliverable, not the app (e.g., the library is an open source one for community use, and the app is a demo app). In that case, you might use debugCompile in app/ to pull in the local library project for debug builds, but have releaseCompile to pull in the AAR from a published source, to confirm that you can build from the same thing that users of the AAR use.
Most of my CWAC libraries are set up this way (e.g., cwac-richedit).
Option #2: Publish the AAR Locally
You can use the maven plugin and the uploadArchives task to upload to a local Maven-style repo:
apply plugin: 'maven'
uploadArchives {
repositories.mavenDeployer {
pom.groupId = PUBLISH_GROUP_ID
pom.artifactId = PUBLISH_ARTIFACT_ID
pom.version = PUBLISH_VERSION
repository(url: LOCAL_REPO)
}
}
Here, my constants are pulled in from a gradle.properties file, and LOCAL_REPO is a file:/// URL pointing to a local repo. You can then run gradle uploadArchives to generate the AAR and push it to the local repo.
Then, your app can have a maven { url LOCAL_REPO } closure in the repositories closure, and can pull in the AAR artifact from there as if it was coming from a public repo (e.g., Maven Central).
My CWAC libraries use the uploadArchives task, but only for publishing to my local mirror of my Amazon S3-hosted Maven repo.
This approach would be if you really wanted to work off of the AAR, but wanted to do so from multiple projects. Note that you can certainly publish this to some other sort of Maven repo (e.g., a Sonatype server) for enterprise use.
Option #3: Mod a Module to Point to the Library Elsewhere
This is Pavel Dudka's approach in his answer. I haven't tried this. Off the cuff, this would be a good approach if you want to depend upon the library from multiple apps, but you're not really concerned about having an actual AAR as a thing to distribute around.
And I'm sure there are other options than these three.

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

Two android projects sharing common module in same repository using gradle

We are creating an app (actually 2) that is split up in 2 separate projects but sharing the same GIT repository (2 separate folders in git root). It is one app for handheld and one for another platform. But they should share some code like Utils, API calls etc.
Folder structure looks like this:
-GIT Root
-- Project (Project)
--- App 1 (Android application)
--- App 2 (Android application)
--- Common (Android library)
App1 and App2 should be able to reach code from common but not the other way of course.
Tried to do like above and using Gradle but it doesn't seem to work. I know that sharing the same git repo for both apps may not be the best way to handle this scenario but I'm having no options here.
Do you think that this is possible to do somehow? (Maybe I'm just not understanding modules correctly or something like that)
I'm new to Gradle which makes this even harder..
This is not too hard to do if all three projects are in the same Git repository. Here is the skeleton of how your projects should be setup:
App1 (Android application)
|_ build.gradle
|_ src
App2 (Android application)
|_ build.gradle
|_ src
Common (Android library)
|_ build.gradle
|_ src
settings.gradle
build.gradle
The top-level build.gradle file can have common gradle settings that you will use for all sub-projects
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
mavenCentral()
}
}
Make sure the settings.gradle file includes all your projects:
include ':App1'
include ':App2'
include ':Common'
Setup Common to be a Android library project. In the build.gradle file for Common add the line:
apply plugin: 'com.android.library'
Setup the other two projects to be Android applications, and include the Common project as a dependency.
apply plugin: 'com.android.application'
dependencies {
compile project(':Common')
:
:
}
You should have three git repos for this. App1, App2 and Common.
Make common a library project using gradle.
apply plugin: 'com.android.library'
You can use the android maven plugin to build the aar locally and make it available to each child app. Add classpath 'com.github.dcendents:android-maven-plugin:1.2' AND apply plugin: 'com.github.dcendents.android-maven'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
apply plugin: 'com.github.dcendents.android-maven'
Define group and version of library app
group = 'com.foo.example'
version = '1.0.0-SNAPSHOT'
Install library app locally using ./gradlew installDebug
Child Apps
Now the parent app can be included in the child app as a dependency. Add dependency to build.gradle.
compile('com.foo.example:library:1.0.0-SNAPSHOT#aar') {transitive = true}
Each time you make a change to the library app you will have to reinstall it. Then you have to resync the gradle files in your children app. Tools -> Android -> Sync Project With Gradle Files

Categories

Resources