Skip to content

Instantly share code, notes, and snippets.

@Robyer
Last active July 31, 2024 13:18
Show Gist options
  • Save Robyer/a6578e60127418b380ca133a1291f017 to your computer and use it in GitHub Desktop.
Save Robyer/a6578e60127418b380ca133a1291f017 to your computer and use it in GitHub Desktop.
Gradle script for publishing Android library with sources and javadoc to Maven repository using maven-publish plugin.
// You can use maven-publish-helper.gradle script without changes and even share it between multiple
// modules. Just place the maven-publish-helper.gradle file in the root directory of your project,
// then apply it at the bottom of your module's build.gradle file like this:
// ...content of module's build.gradle file...
apply from: '../maven-publish-helper.gradle'
publishing {
publications {
release(MavenPublication) {
// Specify own groupId as package name of your library,
// otherwise it would just use project's name (=name of the root directory) by default.
groupId 'com.example'
// Specify custom artifactId if needed,
// otherwise it would use module's name by default.
//artifactId 'custom-artifact'
// You can specify custom version,
// otherwise it would use version from `android { defaultConfig { ... } }` by default.
//version = '1.0'
}
}
}
/**
* Maven Publish Helper
*
* Requires Android Gradle plugin 3.6.0 or higher (available since Android Studio 3.6).
* See also: https://developer.android.com/studio/build/maven-publish-plugin
*
* @Author Robert Pösel
* @Version 1.5
* @Date 3.3.2020
*/
apply plugin: 'maven-publish'
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
android.libraryVariants.all { variant ->
if (variant.name == 'release') {
owner.classpath += variant.javaCompileProvider.get().classpath
}
}
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
archiveClassifier.set('javadoc')
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// Adds javadocs and sources as separate jars.
artifact androidJavadocsJar
artifact androidSourcesJar
// You can customize attributes of the publication here or in module's build.gradle file.
//groupId = 'com.example'
//artifactId = 'custom-artifact'
version = android.defaultConfig.versionName // or just '1.0'
}
}
}
}
@Robyer
Copy link
Author

Robyer commented Jul 19, 2020

@cyb3rko Hi, you can look at this config file, I am using older version of my gist there, but the Bintray part at the bottom is what is important for you: https://github.com/adaptech-cz/Tesseract4Android/blob/d03bd5fc063d3f34cefd1d48481da38421970136/publish.gradle#L99-L136

EDIT: Oh, and also these lines too: https://github.com/adaptech-cz/Tesseract4Android/blob/d03bd5fc063d3f34cefd1d48481da38421970136/publish.gradle#L53-L58

@ashu8826
Copy link

ashu8826 commented Dec 4, 2020

@Robyer
I am getting following error on line -- from components.release
Could not get unknown property 'release' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.

I don't need javadocs so I have not added any other artifact.
my library have buildType { release {...} } and using latest Android gradle plugin

What I am doing wrong?

EDIT:
Oops: I had also defined build Flavour, forgot to add that
Below worked for me because my build flavour is named "fat"
from components.fatRelease

@Hesowcharov
Copy link

@Robyer, thanks, mate! You helped me a lot! 👍

Copy link

ghost commented Dec 31, 2020

When I do as per instructions I get Extension not initialized yet, couldn't access compileSdkVersion. error!

@Robyer
Copy link
Author

Robyer commented Dec 31, 2020

@realchandan Maybe you are using some incompatible version of Gradle plugin or other library? I am using it with stable Android Studio with Java codebase. You can try to use it in new empty project.

@ivangarza6
Copy link

@Robyer I"m having trouble keeping my JNI and native lib dependencies inside my AAR. I suspect the pom file requires me to specify then? I could really use some help!

@Robyer
Copy link
Author

Robyer commented Jan 26, 2021

@ivangarza6 I think you should ask question on StackOverflow instead, I don't have experience with specifying native lib dependencies. I'm not even sure whether Android Studio / Gradle currently supports them.

@Robyer
Copy link
Author

Robyer commented Mar 29, 2021

@Petrakeas You need to first publish the moduleB with this script as one aar. Then you need to modify the moduleA's build.gradle and reference that moduleB from repository (e.g. as implementation 'com.example:moduleB:1.0.0' instead of implementation project(':moduleB')) and publish it as second aar (moduleA). This way reference to moduleB will be saved in the moduleA's *.pom file and Gradle will automatically download this dependency when downloading moduleA from the repository.

@Petrakeas
Copy link

Petrakeas commented Mar 29, 2021

@Petrakeas You need to first publish the moduleB with this script as one aar. Then you need to modify the moduleA's build.gradle and reference that moduleB from repository (e.g. as implementation 'com.example:moduleB:1.0.0' instead of implementation project(':moduleB')) and publish it as second aar (moduleA). This way reference to moduleB will be saved in the moduleA's *.pom file and Gradle will automatically download this dependency when downloading moduleA from the repository.

Thanks Robyer. I actually deleted by question (before you answered), because I noticed that when I had both modules configured for publishing with the proposed script, the maven-publish plugin worked automatically. It seems that maven-publish plugin was smart enough to replace the dependancy with one referencing maven. It even picked-up my custom artifactId that was different to the actual module name!

@Robyer
Copy link
Author

Robyer commented Mar 29, 2021

@Petrakeas I noticed that when I had both modules configured for publishing with the proposed script, the maven-publish plugin worked automatically. It seems that maven-publish plugin was smart enough to replace the dependancy with one referencing maven. It even picked-up my custom artifactId that was different to the actual module name!

Thanks for interesting info! I didn't know that and it sounds great.

@Petrakeas
Copy link

In case it helps someone, I have made some changes that enable correct UTF-8 Javadoc generation, repeatable builds and support for Java and Android Gradle plugin. I have created a guide here.

@Robyer
Copy link
Author

Robyer commented Mar 1, 2022

With Android Gradle Plugin 7.1 it is now very simple to do this without needing any complicated scripts. AGP now also handles creating source and javadocs jar.

You don't need any separate scripts, just write everything into your build.gradle file of your module:

plugins {
    ...
    id 'maven-publish'
}
android {
    ...
    publishing {
        singleVariant("release") {
            // if you don't want sources/javadoc, remove these lines
            withSourcesJar()
            withJavadocJar()
        }
    }
}
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId 'com.example'
                artifactId 'mylibrary'
                version = android.defaultConfig.versionName // or manually '1.0'
            }
        }
    }
}

See also: https://developer.android.google.cn/studio/build/maven-publish-plugin

@nikunj-unifynd
Copy link

nikunj-unifynd commented May 5, 2023

when I publish using gradle publish task i am getting the following error:
Execution failed for task ':project-name:androidJavadocs'.

path may not be null or empty string. path=''

Pls help

@Robyer
Copy link
Author

Robyer commented May 5, 2023

@nikunj-unifynd It would be better for you to ask this question on StackOverflow, providing also additional info as content of your build.gradle file, because maybe you have some conflicting configuration there.

Or just start by creating new empty Android Studio project, and add this publishing code there. Then if it works, compare how your project where it doesn't work differs and try to find the issue.

@RahulSDeshpande
Copy link

RahulSDeshpande commented Jun 15, 2023

Thanks @Robyer

Guys FYI
Official Google Documentation:
https://developer.android.com/build/publish-library

@AndroidDeveloperLB
Copy link

I have just this:

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
            }
        }
    }
}

But how do I do this in kts file instead ?

@Robyer
Copy link
Author

Robyer commented Jul 29, 2024

But how do I do this in kts file instead ?

I don't use Kotlin DSL yet, so I don't know, sorry.

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented Jul 29, 2024

@Robyer I thought I've found how to do it for kts:

https://stackoverflow.com/a/72444502/878126

But it still didn't help me with publishing to Jitpack, so I went back to normal gradle file, and still had issues publishing there. I reported about this here:

jitpack/jitpack.io#6411

Would really appreciate it if you can help there.

@Robyer
Copy link
Author

Robyer commented Jul 29, 2024

@AndroidDeveloperLB You are missing the android { publishing { ... } } block as I mentioned in earlier comment: https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017?permalink_comment_id=4082108#gistcomment-4082108

@AndroidDeveloperLB
Copy link

AndroidDeveloperLB commented Jul 29, 2024

@Robyer But I never had this. It seems new.
Look, on these repositories it worked fine without this part:

https://github.com/AndroidDeveloperLB/CommonUtils/blob/main/library/build.gradle
https://github.com/AndroidDeveloperLB/apk-parser/blob/master/mylibrary/build.gradle

And here, even other parts weren't added and they still worked fine:

https://github.com/AndroidDeveloperLB/FastScrollerAndRecyclerViewFixes/blob/main/library/build.gradle

https://github.com/AndroidDeveloperLB/AutoFitTextView/blob/master/AutoFitTextViewLibrary/build.gradle

Why does it keep getting more and more annoying ? In the past all I had to do is having an android-library module, and the website took care of everything...

Anyway, I've tried to add it, and it still has an issue:

https://jitpack.io/com/github/AndroidDeveloperLB/ColorPicker/2237cd55a3/build.log
https://github.com/AndroidDeveloperLB/ColorPicker

@Robyer
Copy link
Author

Robyer commented Jul 29, 2024

@AndroidDeveloperLB I don't know why it worked before in your case, but right now it is supposed to be done as I said in the comment.

This is actually very simple and easy to do when compared to what I had to do in the past (again, I can't see how it could work in your case).

@Robyer
Copy link
Author

Robyer commented Jul 30, 2024

@AndroidDeveloperLB Hmm, you must probably configure it differently when you are using Kotlin - maybe that's also reason why it worked for you previously. This my gist is for Java projects and I confirm it works as I specified above, but you may need a different approach for Kotlin projects.

Maybe this will help? https://kotlinlang.org/docs/multiplatform-publish-lib.html
Otherwise use StackOverflow instead, someone will definitely help you there.

@AndroidDeveloperLB
Copy link

@Robyer It was Java based, and had gradle and not kts.
I tried kts just to see how to do it, but got back to gradle after seeing it might be the reason to the issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment