In Android, compiling with Gradle, how to share code between projects? - android

What is the preferred way to share some code (E.g. a Utils class) between two projects when building two apps using Gradle to build?
Can I do this without creating extra jar files? I just want my code to sit outside the app projects, be imported/compiled into both app projects. Or is this simply not possible?
I'm familiar with the approach that uses jars or Android library projects, but both seem a bit unwieldy.

My favorite way of doing this is by keeping it in a local Maven repo. The repo can even live in your SCM so it's the same across workspaces.
Create a new Android Studio project and then set it as a maven project your build.gradle config:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
apply plugin: 'maven'
repositories {
mavenCentral()
}
configurations {
archives {
extendsFrom configurations.default
}
}
group = 'com.mypackage.mylibrary'
version = '1.0.0'
uploadArchives {
configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: uri("relative/path/to/localrepo"))
pom.project {
artifactId 'mylibrary'
name 'My Library'
packaging 'aar'
}
}
}
}
android {
// copy old android config here
}
You'll need to deploy the library before you can use it. Do this by using the uploadArchives task [./gradlew uploadArchives]
Now you should be able to use this library in any project by doing this:
repositories {
maven { url 'relative/path/to/localrepo' }
}
dependencies {
compile ('com.mypackage.mylibrary:1.0.0')
}
When you make changes to your library, you'll have to re-deploy (uploadArchives) with a new version, then update the dependency reference in whatever project needs the new version.

Related

How we can link two interdependent projects in Android studio 3.6?

I have a sdk and a client app. The client app is using the sdk. When I build the sdk, it generates multiple aar files which I can add it in libs folder in my client project.
Now if I modify the sdk, every time I need to build the sdk code using gradlew and then add the aar in client code. This is becoming a lengthy process.
My top level build.gradle file looks like below:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.2'
classpath 'com.google.gms:google-services:3.1.1'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
Is there a way to link the sdk code with client code. So the sdk changes will be automatically picked by the client code. I am using Android Studio 3.6.
Thanks,
Arindam.
you can use the maven, or maven-publish to publish to your own maven repositories, it can be local. the config like that in build.gradle
uploadArchives {
repositories {
configuration = configurations.archives
mavenDeployer {
// the local repo address
repository(url: uri('../repo'))
pom.project {
version '1.0.1'
artifactId 'artifactId'
groupId 'groupId'
packaging 'aar'
description 'version 1.0.1'
}
}
}
}
then add this repository in project build.gradle
maven {
url "file://Users.../yourProject/repo/"
}
after that, you can use that as the third library. after you change it, you only need to execute it, then build the current project.
if you also want to look at the source code about SDK in the current project.
you can change to use maven-publish plugin.

Gradle cannot resolve local aar dependency

I wanted to integrate my own library to an Android application.
Before I push it on remote maven repo I want to check the integration with an application locally. So I've pushed it to my mavenLocal().
Maven was installed via brew install maven
And there is my problem - Gradle cannot resolve dependency to my library.
Error I get is just ERROR: Failed to resolve: com.op.rlgen:0.0.1
In library I use maven-publish plugin:
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'com.op'
artifactId 'rlgen'
version '0.0.1'
artifact("$buildDir/outputs/aar/rlgen-release.aar")
pom.withXml {
// generating pom logic here
}
}
}
// For `publish` task use mavenLocal
repositories {
mavenLocal()
}
}
to publish a library on my maven I use:
./gradlew :rlgen:publishToMavenLocal
In following path i have 2 files, one with aar extension, the second one with pom:
~/.m2/repository/com/op/rfgen
As I thought the problem is caused because of quite complicated scripts in my main app, I created a sandbox, clear Android Application.
In sandbox's gradle I have:
// project level
allprojects {
repositories {
mavenLocal()
google()
jcenter()
// also tried with
maven {
url "~/.m2/repository"
}
}
}
// app level
dependencies {
compile('com.op.rfgen:0.0.1#aar) {
transitive = true
}
}
I check if mavenLocal points to proper folder and it seems ok:
// in gradle
repositories {
println(mavenLocal().url) // file:/Users/op_user/.m2/repository/
}
And with this configuration I cannot add dependency to that aar because of:
ERROR: Failed to resolve: com.op.rlgen:0.0.1:
Affected Modules: app
Thanks for your help!
The problem was that line:
compile('com.op.rfgen:0.0.1#aar). Should be... compile('com.op:rfgen:0.0.1#aar)... :D

Nested aar dependencies not working

To simplify things I'm going to call the libraries I'm using library_a.aar and library_b.aar.
Here is the scenario that I'm facing.
library_a is build and pushed to maven_repository and no problems here.
library_b depends on library_a and added to library_b as follows:
repositories {
maven {
credentials {
username USERNAME
password PASSWORD
}
url "https://api.bitbucket.org/1.0/repositories/COMPANY/maven_repository/raw/releases"
}
}
dependencies {
...
compile 'package:library_a:1.0'
...
}
library_b is built with no errors and uploaded to the maven_repository.
Now my application depends on library_b which I need to add by providing (as above) the repository along with the credentials.
The first issue that I'm facing is that in order to compile library_b in my project it needs to be compiled in the following way:
dependencies {
...
compile 'package:library_b:1.0#aar'
...
}
I have to add the #aar otherwise gradle won't recognize it, but I didn't have to do that with library_a.
The second issue is that when I build the app I get warnings that it can't find references to classes available in library_a. What am I missing over here? I did try to add transitive=true to library_a
dependencies {
...
compile ('package:library_a:1.0') {
transitive = true;
}
...
}
but absolutely nothing works. I did check the pom file and it includes the proper dependencies.
Could it be that what I'm doing is not supported by gradle and I have to compile both library_a and library_b in my app?
Sounds like your maven repo only contains the aar files but no or wrong pom files.
Are you using the following gradle plugin and the uploadArchives gradle task in order to upload the aars to your maven repository?
https://github.com/dcendents/android-maven-gradle-plugin
try this gradle snippet for your aars:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android { ... }
dependencies { ... }
uploadArchives {
repositories {
mavenDeployer {
repository(url: "https://api.bitbucket.org/1.0/repositories/COMPANY/maven_repository/raw/releases") {
authentication(userName: USERNAME, password: PASSWORD)
}
}
}
}
Then use the uploadArchives gradle task

Gradle: How to publish a Android library to local repository

I have a library and a Android app using Gradle and Android Studio. I can include the library directly in the project as following
compile project(':library')
Because I don't want to mesh up with library source code, I want to publish the library into local repository so that I can use as
compile 'com.mygroup:library:1.0'
Any advise?
I just found a solution. In the build.gradle of the library project, add this
apply plugin: 'maven'
group = 'com.mygroup'
version = '1.0'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://[your local maven path here]")
// or repository(url: mavenLocal().getUrl())
}
}
}
In the project folder, type following command
gradle uploadArchives
Read Publishing artifacts for more information
For an Android Library you should use the Android Gradle-Maven plugin https://github.com/dcendents/android-maven-gradle-plugin:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
Then to publish to your local repository run:
./gradlew install
which will install it in $HOME/.m2/repository. In your app or other project you can then include it by adding mavenLocal() to your repositories.
Alternatively, if your library is on GitHub then you can simply include it in other projects using JitPack. Then you don't have to run the above command and can just use what's been pushed.
Publish de library on your local maven repository and then on your gradle use
repositories {
mavenLocal()
}
If you have other repositories listed, make sure your mavenLocal appears first.
Docs: section 51.6.4 on https://gradle.org/docs/current/userguide/dependency_management.html
I prefer adding the java sources and the javadoc to the maven repository. The following script publishes your android library to a maven repository using the android maven plugin. It creates the .aar, javadoc.jar, sources.jar and .pom and updates the maven-metadata.xml after uploading the files to the maven repository. I also put the script on GitHub.
apply plugin: 'com.android.library'
apply plugin: 'maven'
//Your android configuration
android {
//...
}
//maven repository info
group = 'com.example'
version = '1.0.0'
ext {
//Specify your maven repository url here
repositoryUrl = 'ftp://your.maven.repository.com/maven2'
//Or you can use 'file:\\\\C:\\Temp' or 'maven-temp' for a local maven repository
}
//Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
//If you want to deploy to an ftp server
dependencies {
deployerJars "org.apache.maven.wagon:wagon-ftp:2.2"
}
// custom tasks for creating source/javadoc jars
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
destinationDir = file("../javadoc/")
failOnError false
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
//Creating sources with comments
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
//Put the androidSources and javadoc to the artifacts
artifacts {
archives androidSourcesJar
archives javadocJar
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: repositoryUrl) {
//if your repository needs authentication
authentication(userName: "username", password: "password")
}
}
}
}
Call it with
./gradlew uploadArchives
In settings.gradle add
include 'riz.com.mylibrary'
project(':riz.com.mylibrary').projectDir = new File('C:\\Users\\Rizwan Asif\\AndroidStudioProjects\\mylibrary')
Then in build.gradle in the dependencies add
compile project(':riz.com.mylibrary')

Building android project with gradle

Im trying to build a simple android app using gradle build tools. but im getting an error like this
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (java.lang.String) values: [org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT]
Possible solutions: module(java.lang.Object)
ang here's a simple configuration of build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
compile 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
processResource {
expand (project.properties)
}
task configureDebug << {
jar.classifier = "debug"
}
task configureRelease << {
proguard.enabled = true
}
When applying a plugin you want tell you build script to use it in its classpath. It is not required for compilation so just change the configuration compile to classpath. More more information see 51.5.1. Using your plugin in another project in the Gradle user guide.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
EDIT: At the moment the plugin does not support r20 of the Android SDK. For more information see this issue.
Make sure you are writing the dependency block on your application build.gradle "YourProjectName->yourprojectname->build.gradle" in android studio hierarchy .
Use android gradle tools
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}

Categories

Resources