Gradle/maven newbie here. I have created a library that is currently published to bintray/jcenter as an AAR, to be used with gradle in Android Studio projects.
However, since my library doesn't really have any hard Android dependencies, and may be used in any Java SE environment, I would like to publish it also as a normal jar. Since I am not very familiar with the non-android java development community I am not sure if this means I should publish it to Maven central, or would developers typically be fine fetching my library from bintray, given that the library is actually hosted in a maven repo on bintray?
Should I simply extend my artifacts to include the jar (alongside with the aar, the javadoc and the source jar), or should I typically publish the jar separately, say to Maven central?
Getting the bintray/jcenter stuff up and running was no picnic, I found a few guides on how to do it, but none was really complete, but finally I have it working. I am however having a surprisingly difficult time finding a similar easy guide for maven publishing.
My gradle file looks like this:
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 1
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
}
}
group = <my library name>
version = <my version>
dependencies {
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
options.overview = 'src/main/overview.html'
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "maven"
name = <my library name>
websiteUrl = <my site url>
vcsUrl = <my git url>
licenses = ["Apache-2.0"]
publish = true
}
}
The com.github.dcendents.android-maven plugin offers an install task that produces the AAR, Source Jar and Javadoc Jar. I have defined my own bintray task that publishes the library to bintray. What would be the easiest way to create a task for publishing my library also as a standard jar to maven? Can I use com.github.dcendents.android-maven for this as well, or do I need a separate plugin?
Related
I need to publish my android library (aar) using Gradle to Maven local repo.
But the publication script needs to also generate the Javadocs, while ONLY including Public and Protected methods and classes.
Can't seem to find any information online, especially about the Javadocs part...
Help, I never published a library before.
Ok, after much research I found a solution, so I'm going to share it here if anyone will need this. (I don't want you to be frustrated like I was).
1) Create an android library as a new module inside your project.
2) Inside the build gradle of your library place this code:
plugins {
id 'com.android.library'
id 'maven-publish'
}
android {
nothing special here...
}
This is the code for creating the Javadocs(still inside build.gradle):
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
}
}
// excluding a specific class from being documented
exclude '**/NameOfClassToExclude.java'
title = null
options{
doclet = "com.google.doclava.Doclava"
docletpath = [file("libs/doclava-1.0.6.jar")]
noTimestamp = false
// show only Protected & Public
memberLevel = JavadocMemberLevel.PROTECTED
}
}
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
}
This is to publish the library to MavenLocal(still inside build.gradle):
afterEvaluate {
publishing{
publications{
release(MavenPublication){
groupId = "com.example.mylibrary"
artifactId = "mycoollibrary"
version = "1.0"
// Applies the component for the release build variant
from components.release
// Adds javadocs and sources as separate jars.
artifact androidSourcesJar
artifact androidJavadocsJar
}
}
}
}
Your default dependencies block:
dependencies {
your dependencies...
}
3) Now you can download the doclava doclet:
Extract the zip, copy the doclava-1.0.6.jar and paste it into your LibraryName/libs folder (can be found using the project view).
You only need doclava if you want to be able to use #hide.
With this annotation, you can exclude specific methods from your Javadocs.
4) Build and publish your library:
Find the gradle tab at the top right side of android studio, or find it from the toolbar View->Tool Windows->Gradle.
Now find your library -> tasks -> publishing -> publishReleasePublicationToMavenLocal.
5) To consume the library from another project:
Go to the settings.gradle file (of the consuming project) and add MavenLocal() as the first repository in the the dependencyResolutionManagement block.
And inside the module build gradle add your library as a dependency:
dependencies{
implementation 'com.example.mylibrary:mycoollibrary:1.0'
}
I created an Android Library in Android studio, which has some external dependencies(Retrofit, for example).
But when i tried to use this Library in an Android app, The app doesn't include the transitive dependencies(the ones included in the library).
I've already tried publishing the library to Bintray, change the 'implementation' keyword in app-gradle file to 'api'.
I've also tried setting transitive = true in my app's gradle file
When trying to build the Android app, It shows Resource Linking Failed for CardView which's used in my Library.
I faced the same problem while building a Library for Android.
I was trying to directly upload the .aar file from the android studio to the bintray. But appparantly, it was not including the pom.xml file.
So I followed this tutorial to generate the zip file.
Then create a new version at bintray and then manually upload the zip file using the UI upload option
Also, do not forget to check the explode this Archive option while uploading.
Your can then, publish the library and use it with all its transitive dependencies.
Here's an example build.gradle code:
def version = 'your.version'
def localReleaseDest = "${buildDir}/release/${version}"
uploadArchives {
repositories.mavenDeployer {
pom.groupId = 'your.package.name'
pom.artifactId = 'yourModuleName'
pom.version = 'your.version'
// Add other pom properties here if you want (developer details / licenses)
repository(url: "file://${localReleaseDest}")
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
task zipRelease(type: Zip) {
from localReleaseDest
destinationDir buildDir
archiveName "release-${version}.zip"
}
task generateRelease {
doLast {
println "Release ${version} can be found at ${localReleaseDest}/"
println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
}
}
generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)
put this code outside all blocks in your app level gradle after modifying it as needed.
Now, sync project and open terminal inside the Android studio, and execute this command:
./gradlew clean build generateRelease
this will generate a .zip file in your app/build directory which you can upload to bintray like described above
I am working on Android library development, i have already finished my library work and generated aar and jar file, but when i am trying to publish to binary at that time i am getting one error message.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':mylittlelibrary:bintrayUpload'.
> Could not create package 'abcd/maven/helloaar.example.com.mylittlelibrary': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 5.391 secs
Could not create package 'abcd/maven/helloaar.example.com.mylittlelibrary': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
2:41:59 AM: External task execution finished 'bintrayUpload'.
build.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
repositories {
mavenCentral()
}
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0-beta1'
}
group = 'helloaar.example.com.mylittlelibrary'
version = '1.0.2'
task generateSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier 'sources'
}
task generateJavaDocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath()
.join(File.pathSeparator))
}
task generateJavaDocsJar(type: Jar) {
from generateJavaDocs.destinationDir
classifier 'javadoc'
}
generateJavaDocsJar.dependsOn generateJavaDocs
bintray {
user = 'abcd'
key = '1234567890fghgfhffjfgjfjfjrtyjtkjg'
pkg {
repo = 'maven'
name = 'helloaar.example.com.mylittlelibrary'
version {
name = '1.0.2'
desc = 'My test upload'
released = new Date()
vcsTag = '1.0.2'
}
licenses = ['Apache-2.0']
vcsUrl = ''
websiteUrl = ''
}
configurations = ['archives']
}
artifacts {
archives generateJavaDocsJar
archives generateSourcesJar
}
Please kindly go through my script and suggest me some solution.
Before you publish to Bintray you must have a stable build of your code.
The hierarchy on Bintray is the following:
User --> Repo --> package --> version --> artifact
means that the Repo should be above the package by that hierarchy.
The following lines in your gradle.build are probably the main cause for the error:
pkg {
repo = 'maven'
name = 'helloaar.example.com.mylittlelibrary'
}
The hierarchy is wrong.
When using maven you should check that you comply with the maven convention, else the Maven Build can not succeed. Explanation for Maven Repositories on Bintray.
The error you had:
HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
Means that you do not have a Repository on Bintray calls maven. All the packages versions and files should be under the Bintray Repository you have created.
For more details or support issues you can use the Bintray support team which is available for all Bintray users and can help you with any problem related to any JFrog platform services such as Bintray, Artifactory, Mission control & Xray.
pkg {
repo = 'maven'
name = 'helloaar.example.com.mylittlelibrary'
version {
name = '1.0.2'
desc = 'My test upload'
released = new Date()
vcsTag = '1.0.2'
}
Could not create package 'abcd/maven/helloaar.example.com.mylittlelibrary': HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]
2:41:59 AM: External task execution finished 'bintrayUpload'.
'abcd/maven/helloaar.example.com.mylittlelibrary'
abcd : bintray user name
maven : pkg { repo = 'maven' } set by you, example 'abcd/android-lib/helloaar.example.com.mylittlelibrary' when set repo = 'android-lib'
helloaar.example.com.mylittlelibrary : pkg{ name = 'helloaar.example.com.mylittlelibrary'
-
first : check your bintray account,
second : make repository,
third : modify your pkg repo = 'input making repository name from second step' build.gradle
last : build again
i hope help for you, have a nice day
I have a library project with a module that is just for library classes and views. I've been searching over the internet how to distribute it in jCenter to use as a gradle dependency but nothing works.
While this isn't done yet, how can I use this module in others projects?
PS: I use Android Studio on Windows 10.
Many of the tutorials and directions online are out of date or are very hard to follow. I just learned how to do this myself, so I am adding what will hopefully be a quick solution for you. It includes the following points
Start with your Android library
Set up a Bintray account
Edit your project's gradle files
Upload your project to Bintray
Link it to jCenter
The library you want to share
By now you probably already have a library set up. For the sake of this example I made a new project with one demo-app application module and one my-library library module in Android Studio.
Here is what it looks like using both the Project and Android views:
Set up a Bintray account
Bintray hosts the jCenter repositories. Go to Bintray and set up a free account.
After you sign in click Add New Repository
Name the repository maven. (You can call it something else, though, if you want to group several library projects together. If you do you will also need to change the bintrayRepo name in the gradle file below.)
Chose Maven as the repository type.
You can add a description if you want. Then click Create. That's all we need to do in Bintray for now.
Edit the gradle files
I'm going to make this as cut-and-paste as possible, but don't forget to edit the necessary parts. You don't need to do anything with the demo app module's build.gradle file, only the gradle files for the project and the library.
Project build.gradle
Add the Bintray and Mavin plugins to your project build.gradle file. Here is my whole file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
// Add these lines (update them to whatever the newest version is)
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
The newest version for Bintray is here and Maven is here.
Library build.gradle
Edit everything you need to in the ext block below.
apply plugin: 'com.android.library'
// change all of these as necessary
ext {
bintrayRepo = 'maven' // this is the same as whatever you called your repository in Bintray
bintrayName = 'my-library' // your bintray package name. I am calling it the same as my library name.
publishedGroupId = 'com.example'
libraryName = 'my-library'
artifact = 'my-library' // I'm calling it the same as my library name
libraryDescription = 'An example library to make your programming life easy'
siteUrl = 'https://github.com/example/my-library'
gitUrl = 'https://github.com/example/my-library.git'
libraryVersion = '1.0.0'
developerId = 'myID' // Maven plugin uses this. I don't know if it needs to be anything special.
developerName = 'My Name'
developerEmail = 'myemail#example.com'
licenseName = 'The MIT License (MIT)'
licenseUrl = 'https://opensource.org/licenses/MIT'
allLicenses = ["MIT"]
}
// This next section is your normal gradle settings
// There is nothing special that you need to change here
// related to Bintray. Keep scrolling down.
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
// Maven section
// You shouldn't need to change anything. It just uses the
// values you set above.
apply plugin: 'com.github.dcendents.android-maven'
group = publishedGroupId // Maven Group ID for the artifact
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
groupId publishedGroupId
artifactId artifact
// Add your description here
name libraryName
description libraryDescription
url siteUrl
// Set your license
licenses {
license {
name licenseName
url licenseUrl
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
// Bintray section
// As long as you add bintray.user and bintray.apikey to the local.properties
// file, you shouldn't have to change anything here. The reason you
// don't just write them here is so that they won't be publicly visible
// in GitHub or wherever your source control is.
apply plugin: 'com.jfrog.bintray'
version = libraryVersion
if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
} else { // Java libraries
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = bintrayRepo
name = bintrayName
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = allLicenses
publish = true
publicDownloadNumbers = true
version {
desc = libraryDescription
gpg {
// optional GPG encryption. Default is false.
sign = false
//passphrase = properties.getProperty("bintray.gpg.password")
}
}
}
}
local.properties
The library build.gradle file above referenced some values in the local.properties file. We need to add those now. This file is located in the root of your project. It should be included in .gitignore. (If it isn't then add it.) The point of putting your username, api key, and encryption password here is so that it won't be publicly visible in version control.
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/home/yonghu/Android/Sdk
# Add these lines (but change the values according to your situation)
bintray.user=myusername
bintray.apikey=1f2598794a54553ba68859bb0bf4c31ff6e71746
There is a warning about not modifying this file but it seems to work well anyway. Here is how you get the values:
bintray.user: This is your Bintray username.
bintray.apikey: Go to Edit Profile in the Bintray menu and choose API Key. Copy it from here.
Upload project to Bintray
Open a terminal and go to your project's root folder. Or just use the terminal in Android Studio.
Enter the following commands
./gradlew install
./gradlew bintrayUpload
If everything is set up right it should upload your library to Bintray. If it fails then Google the solution. (I had to update my JDK the first time I tried.)
Go to your account in Bintray and you should see the library entered under your repository.
Link to jCenter
In your library in Bintray there is an Add to jCenter button.
Click it and send your request. If you are approved (which takes a day or two), then your library will be a part of jCenter and developers around the world can add your library to their projects simply by adding one line to the app build.gradle dependencies block.
dependencies {
compile 'com.example:my-library:1.0.0'
}
Congratulations!
Notes
You may want to add PGP encryption, especially if you are linking it to Maven Central. (jCenter has replaced Maven Central as the default in Android Studio, though.) See this tutorial for help with that. But also read this from Bintray.
How to add a new version
You will eventually want to add a new version to your Bintray/jCenter library. See this answer to directions on how to do it.
Further Reading
How to distribute your own Android library through jCenter and Maven Central from Android Studio
Creating and Publishing an Android Library
I'm evaluating the ability of the new gradle-based build system to reproduce our current ant-based build process and, as a gradle beginner, I failed to get checkstyle running with the android gradle plugin.
Environment:
gradle 1.6 running fine on a standard java project (checkstyle check target included)
up-to-date android SDK (22.0.1 with platform tools and build tools 17)
no eclipse, no android studio, only my lovely terminal
Symptom:
The target project is https://github.com/nibua-r/LigoTextDemo and I succeeded to build it using gradle but if I naively add apply plugin: checkstyle to my build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
apply plugin: 'checkstyle'
android {
buildToolsVersion '17'
compileSdkVersion 15
testBuildType 'debug'
defaultConfig {
versionCode = 1
versionName = '1.0'
minSdkVersion 12
targetSdkVersion 15
}
buildTypes {
debug {
packageNameSuffix = '.debug'
}
}
}
then gradle check doesn't even complain on not finding the checkstyle.xml file (at the default config/checkstyle location) and returns:
:check UP-TO-DATE
BUILD SUCCESSFUL
What's needed:
First, I just need a running checkstyle target. Then, I need to automate checkstyle running as a dependency of the compilation (but lets get the chekstyle target up and running first).
Assumption:
This may be related to the fact that (from the [user guide][1]):
The Android plugin […] uses its own sourceSets
but I'm not enough gradle-efficient to understand what I'm missing there. Please, gradle Master, enlighten me with your valuable knowledge!
I got pmd, findbugs, and checkstyle working with Gradle 1.12 android plugin 0.12.+ using the following script:
apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'
check.dependsOn 'checkstyle', 'findbugs', 'pmd'
task checkstyle(type: Checkstyle) {
configFile file("${project.rootDir}/config/quality/checkstyle/checkstyle.xml")
source 'src'
include '**/*.java'
exclude '**/gen/**'
classpath = files()
}
task findbugs(type: FindBugs) {
ignoreFailures = true
effort = "max"
reportLevel = "high"
excludeFilter = new File("${project.rootDir}/config/quality/findbugs/findbugs-filter.xml")
classes = files("$project.buildDir/intermediates/classes/")
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml {
destination "$project.buildDir/reports/findbugs/findbugs.xml"
xml.withMessages true
}
}
classpath = files()
}
task pmd(type: Pmd) {
ruleSetFiles = files("${project.rootDir}/config/quality/pmd/pmd-ruleset.xml")
ignoreFailures = true
ruleSets = ["basic", "braces", "strings"]
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = true
html.enabled = false
}
}
Running gradle build in command line will run all code quality plugins and generate xml reports in app/build/reports/ which are then ready to be viewed or parsed by CI tools.
Someone has a great answer to solve integrating PMD, findbugs and checkstyle with Gradle for Android.
Unfortunately, the only solution for now is based on ant :
http://sethrylan.org/2013/07/14/gradle-android-findbugs.html
I wish gradle will one day allow to do as much as maven for Android.
--- Update as of October 2013
With Gradle 1.8 and Android plugin for Gradle 0.6.+, you don't need this anymore. Android sourcesets and configurations are now compatible with the java plugin and all quality plugin work out of the box.
This includes pmd, findbugs, checkstyle and classycle.
--- Update
A configuration, largely inspired from the project mentioned above, is proposed in this open source project as well, plus other tools.
To get this to work with my Android project, I had to declare the task explicitly.
Here's what worked for me:
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
source 'src'
include '**/*.java'
exclude '**/gen/**'
// empty classpath
classpath = files()
}
Be mindful that the Android plugin may choose to create a task of the same name in the future or work in conjunction with the checkstyle plugin in different ways.
You can try Android Check plugin (https://github.com/noveogroup/android-check):
Checkstyle
PMD
Configuration:
buildscript {
repositories { jcenter() }
dependencies {
...
classpath 'com.noveogroup.android:check:+'
...
}
}
apply plugin: 'com.noveogroup.android.check'
You can use hardcore configuration:
check {
abortOnError true
checkstyle { config hard() }
pmd { config hard() }
}
I found by digging on the web that the Android plugin depends on java-base and not java (i.e. the sourceSets management is specific) and the checkstyle plugin rely on java. As a consequence, some gradle upstream modification are needed to get the thing done. The gradle team is working on that, as seen on twitter:
#anzix Android source sets will be soon understood by the generic code quality plugins. Work on that has already started.— Gradle Build System (#Gradleware) May 26, 2013
Take a look at the Soter Gradle plugin to semlessly add support for Findbugs, Checkstyle and PMD to Android projects.