Android multi module libraries - android

How can I build all Android modules in project and expose those to maven repository without creating huge gradle script? There are no clear instructions about modularity in Android projects with artefacts generated by gradle maven plugin. It would be awesome to use built in task.

In few steps:
Create a common gradle file in root directory, such as config.gradle and put this fragment:
ext.packageName = "com.abc.example"
ext.mavenPublisher = {
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
from components.release
artifact androidSourcesJar
groupId = packageName
artifactId = findProperty('moduleName')
version = getVersion()
}
}
}
}
}
In root gradle.properties add property version=1.0.0.
In library module build.gradle add:
apply from: "${rootProject.projectDir}/config.gradle"
with mavenPublisher
Also in library module add gradle.properties with:
moduleName=name
Type in console from root project directory: gradle clean build publishToMavenLocal.
In section from components.release you can change release type to debug or another one build type. Also you can add version to each module if you want, but probably some release plugin configuration is required.
Working example at this link.

Related

How to access extra properties from project build.gradle in modules

So I'm trying to setup Maven Publish in my library which has several modules in it.
I am following this tutorial since the whole process is imo not that well documented on the Android Documentation. However, I am stuck at the point Customizing POM File Generation — Basics. I added the external info in my project build.gradle and try to add the publishing information in the modules build.gradle. When I try to sync Gradle I get the following error:
A problem occurred configuring project ':projectname'.
> Failed to notify project evaluation listener.
> Cannot get property 'pomGroupID' on extra properties extension as it does not exist
> Cannot get property 'pomGroupID' on extra properties extension as it does not exist
The code in my build.gradle files looks like this:
Project:
buildscript {
// POM information for publishing the library
ext {
pomVersion = '4.23'
pomGroupID = "com.randomname.stackoverflowquestion"
}
....
Module:
project.afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
groupId project.ext.pomGroupID
artifactId project.name
version project.ext.pomVersion
artifact(bundleReleaseAar)
}
}
}
}
Any idea on what could be the problem in my case?
The problem was very simple. Only had to change
project.ext.pomVersion
to
project.pomVersion

How to version local gradle dependencies?

My Android project contains a library that is shipped as AAR file.
There are multiple options to include local AAR files.
I can declare a file dependency:
implementation files('libs/mylib.aar')
Or I can put the AAR into another module and then use a project dependency:
implementation project(':mylibmodule')
However, I want to specify the exact version of my library:
mylib:1.0.0
Unfortunately, I do not know how to specify the version without using some remote repository.
Note that I do not want to upload the library to JitPack, MavenCentral or similar.
All I want is to specify the version of a local AAR file.
Update
The AAR file is a Zip-File with the following content:
/proguard.txt
/R.txt
/AndroidManifest.xml
/public.txt
/classes.jar
/res/values/values.xml
Note that the AndroidManifest.xml contains the version of the library.
However, I assume that gradle always expects a pom file for the versioning information.
I realized that gradle allows to specify a local Maven repository at a specific path:
repositories {
maven {
url uri("${projectDir}/mylibdir")
}
}
To use a local Maven repository, I need to build my library as a Maven artifact. To create a Maven artifact, it suffices to create a POM file in the right subfolder.
The AAR file remains unchanged since Maven does not care about the artifact format.
Creating a Maven artifact can be automated with the maven-publish plugin, e.g.:
apply plugin: 'maven-publish'
publishing {
publications {
myRelease(MavenPublication) {
groupId 'com.foo'
artifactId 'my-artifact'
version '1.0.0'
artifact("$buildDir/outputs/aar/my-artifact.aar")
}
}
repositories {
maven {
url "$buildDir/repo"
}
}
}
However, since I do not really need to use Maven, the simpler choice is to add the version to the AAR file name.
I did this with the following snippet in the build.gradle of my library:
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = "${archivesBaseName}-${variant.name}-${defaultConfig.versionName}.aar"
}
}

Kotlin multiplatform library aar wont show sources when added as Dependency in Android Studio

I'm trying to publish a Kotlin Multiplatform library including sources using the maven-publish plugin. This is generally working but when I want to have a look at the code of the dependency inside my Android Studio project I can't see the sources but instead will see decompiled Kotlin code.
I tried several approaches of adding a sources.jar to the generated aar file already. Right now I'm following this approach: https://stackoverflow.com/a/28704799.
But I still can't see the sources(gradle clean cache and restart included).
Library build.gradle:
apply plugin: 'maven-publish'
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
}
publishing {
publications {
bar(MavenPublication) {
groupId 'com.foo'
artifactId 'bar'
version '0.1'
artifact(sourceJar)
artifact("$buildDir/outputs/aar/bar-release.aar")
}
}
repositories {
maven {
url "$buildDir/repo"
}
}
}
I also tried using different sourceSets like specifying one by name since the content of android.sourceSets.main.java.srcDirs is leading to a path where no sources are inside.
The library is included in the app project like this:
dependencies {
implementation 'com.foo:bar:0.1'
}
Looking at the local maven repository I can even see the bar-release-sources.jar but it seems like it's not part of the aar?

How to publish in my maven local repository an existing aar with gradle?

I have a project that have the following structure:
projectRoot
build.gradle
module1/
build.gradle
artifact1.aar
module2/
....
My artifact1.aar is a compiled artifact and i have no access to the sources.
my module 1 gradle build file is the following:
configurations.maybeCreate("default")
artifacts.add("default", file('artifact1.aar'))
With this the code contains in the .aar is available in module 2 by simply reference a gradle project dependency.
But i want to publish the .aar in my maven local, in order that it can be accessible for other android project.
I check the maven-publish plugin and the android-maven-publish plugin but the two seems to be called after java-library plugin or com.android.library plugin.
So my question is how to publish in my maven local repository an existing aar with gradle ?
I'm using gradle in version 4.8.
I used an rxbinding aar for this example.
As you correctly mentioned, there has to be a subproject in the "publisher" project, which must contain the aar, and a build file with the following content:
// rxbinding/build.gradle
apply plugin: "maven-publish"
configurations.maybeCreate("default")
def publishArtifact = artifacts.add("default", file('rxbinding-2.1.1.aar'))
publishing {
publications {
aar(MavenPublication) {
groupId = 'my.sample.artifact'
artifactId = 'somerandomname'
version = '1.0.0'
artifact publishArtifact
}
}
}
You can apply the maven-publish plugin to any project, but then you have to define the artifacts manually, like we've just done here. Some plugins (like the java-library plugin) are integrated with the maven-publish plugin to automatically publish the default artifacts of the project.
Now run ./gradlew rxbinding:publishToMavenLocal - This should place the artifact into your local repo
Add implementation 'my.sample.artifact:somerandomname:1.0.0' to the consumer app, in any project on your machine.
It is very important to mention any aar published this way, will not bring it's dependencies, so you have to know what libs are needed to actually use the published aar.
I have also created an example project, where you can try this.
If you have maven installed, you could use the install-file goal of the maven-install-plugin.

Install library module jar to local maven repository

I am migrating from Eclipse & maven to Android Studio & Gradle build.
My project structure now in Android Studio looks like this:
MyApp
->LibModule
-src
-...
-lib_repo/
-build.gradle
->AnotherModule
...
LibModule is my library module, I want to install the build jar of LibModule to my local maven repository. What I tried in build.gradle (under LibModule/ )is:
apply plugin: 'android-library'
apply plugin: 'maven'
...
group = 'com.my.lib'
uploadArchives {
repositories {
description "Installs the artifacts to the local Maven repository."
mavenDeployer {
repository(url: "file://${System.properties['user.home']}/.m2/repository")
pom.version = '1.1.0'
pom.artifactId = 'MyLib'
}
}
}
}
I also tried:
install {
repositories.mavenInstaller {
pom.version = '1.1.0'
pom.artifactId = 'MyLib'
}
}
I got error "unsupported Gradle DSL method found 'install()'! ".
When I build it in Android Studio IDE, in gradle console, I didn't see anything happen to upload the archive to my local maven repository. I also checked the content of build/ directory, there is no poms/ folder at all. It seems it is not triggered, I followed the gradle document here. Why? What is wrong?
For installing to the local Maven repository (which is mainly useful when exchanging artifacts with local Maven builds), use the install task rather than uploadArchives. For details, check the Gradle User Guide.

Categories

Resources