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
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'm developing an SDK with multiple libraries and an example app to show how to use it. The libraries are uploaded in a local maven repo that the app uses to import them.
Everything works fine until I try to use different flavors for one of the libraries (and for the app).
When I have flavors, the uploadArchive task does not show any error but the library is not uploaded. All the other libraries are uploaded successfully.
I have found this: https://discuss.gradle.org/t/pom-files-for-multi-flavored-android-lib-not-published-on-uploadarchives-task/898
It is an old post but it does not have any answer.
Is it possible to upload one of the flavors of a library?
This is my archive.gradle file:
apply plugin: 'maven'
apply from: "../maven-repo.gradle"
uploadArchives {
repositories {
mavenDeployer {
def folder = "file://localhost" + myMavenRepoDir()
repository(url: folder)
pom.groupId = GROUP
pom.artifactId = POM_ARTIFACT_ID
pom.version = VERSION_NAME
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
}
}
}
}
Here, I have done qr code scan project successfully. i have used for qrcodereaderview 1.0.0 v url repository library. this is my dependency.
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.dlazaro66.qrcodereaderview:qrcodereaderview:1.0.0'
compile files('libs/ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar')
}
I have only one app module in my project.I want to upload this project as a library(.AAR) to jcenter repository.
I have tried for some jcenter uploading steps and I got Successful upload response also from bintrayupload.
For this reason, I have created Github login and created project url. but, I did not upload my project code into github. I have given empty project url only in build.gradle.
but, when I saw in bintray repository.There is no code update/version change on my bintray maven repository.
Android Studio Project convert as .aar(library) file and uploading to jcenter repository in below steps following.
1). I have changed main app module build.gradle files for three changes.
Changed library plugin,commented application id and changed manifest file launcher activity.
My app module build.gradle file:
//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
ext {
bintrayRepo = 'maven' //mavenrepo
bintrayName = 'app'
publishedGroupId = 'com.test.testsdkproj16'
libraryName = 'TestsdkProj16'
artifact = 'app'
libraryDescription = 'A simple qr code library calling your project in Android'
siteUrl = 'https://github.com/vhkrishnanv/TestsdkProj16'
gitUrl = 'https://github.com/vhkrishnanv/TestsdkProj16.git'
githubRepository= 'vhkrishnanv/TestsdkProj16'
libraryVersion = '1.0.0'
developerId = 'vhk*********6'
developerName = 'harikrish'
developerEmail = 'vhkris******#gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
allprojects {
repositories {
jcenter()
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
//applicationId "com.test.testsdkproj16"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.dlazaro66.qrcodereaderview:qrcodereaderview:1.0.0'
compile files('libs/ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar')
}
// Place it at the end of the file
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
My Manifest file:
<activity android:name=".MainActivitySDK_16">
<intent-filter>
<action android:name="com.test.testsdkproj16.MainActivitySDK_16" />
<!--comment when exporting AAR below two lines-->
<!--<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />-->
</intent-filter>
</activity>
gradle.properties file:
bintray.user=vhk*********6
bintray.apikey=***1f************************98f51***
2). Next Steps for upload my studio project to bintray.com. I have used three command for that.
-gradleW clean
BUILD SUCCESSFUL
-gradleW install
BUILD SUCCESSFUL
-gradleW bintrayupload
BUILD SUCCESSFUL
After executed the above three commands, when i saw in my bintray repository, there is no change in my repository.
My repository's screenshot
I dont know exactly what step is missing. Can anyone help to come out this error.
Overall I want to Publish my Studio2.2.2 project(converted as .aar library) to jcenter repository and need to get my project url this like. ( when i tried this url also in other new project in dependencies, getting error while sync,because, code/.aar not upload in repository)
I have one pending final steps is there. once code is uploaded, need to jcenter sync is pending in bintray repos.then only, I can able to use the above url to other New Project.
compile 'com.test.testsdkproj16:app:1.0.0'
In your question you have written, that you keep credientials to bintray account in gradle.properties:
bintray.user=vhk*********6
bintray.apikey=***1f************************98f51***
If you look into the source file from:
https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle
You will see the following lines:
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
You have put the credientials in the wrong file. They should be in local.properties
EDIT
After long discussion finally the project was published to bintray. The problem here was that the user was publishing to another organisation project. So indeed, the answer made by #gba was correct.
However, #harikrishnan was advised to download bintray.gradle and install.gradle to the project and modify them. Here is how they should look:
bintray.gradle
apply plugin: 'com.jfrog.bintray'
apply from: '../bintray.data.gradle'
version = libraryVersion
if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
task javadoc(type: Javadoc) {
options.addBooleanOption('Xdoclint:none', true)
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(new FileInputStream(file(rootProject.file('local.properties'))))
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = bintrayRepo
name = bintrayName
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
userOrg = userOrg
licenses = allLicenses
publish = true
publicDownloadNumbers = true
}
}
install.gradle
apply plugin: 'com.github.dcendents.android-maven'
apply from: '../bintray.data.gradle'
group = publishedGroupId
install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
groupId publishedGroupId
artifactId artifact
name libraryName
description libraryDescription
url siteUrl
licenses {
license {
name licenseName
url licenseUrl
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
bintray.data.gradle:
ext {
bintrayRepo = 'maven'
bintrayName = 'samplename'
publishedGroupId = 'sample.package.name'
libraryName = 'SampleName'
artifact = 'samplename'
libraryDescription = ''
siteUrl = ''
gitUrl = ''
libraryVersion = '1.0.0'
developerId = ''
developerName = 'Zagórski'
developerEmail = ''
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
app's build.gradle:
apply plugin: 'com.android.application'
(...)
apply from: '../install.gradle'
apply from: '../bintray.gradle'
main build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
A sample project that utilises those classes is here
seems like you are missing the userOrg parameter. Please look at the following threads:
HTTP/1.1 401 Unauthorized when uploading binary on bintray
Trouble Publishing Android Studio Library on jCenter with Bintray
publish my android aar to jcenter
In short, your repo is under your org and not under your user account.
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?
I've used the following standard code to upload my library to bintray.
I have two modules, a 'sample' module for testing and a 'library' module. I want to use a custom artifactId when I upload this to bintray but instead the artifactId gets changed to "library" (the module name) and I don't want this!
I know I could rename my "library" module to the desired name but I also want to keep this structure of module names.
I want something like: com.mydomain.something:CUSTOM-NAME:version.
And NOT something like: com.mydomain.something:LIBRARY:version.
ext {
bintrayRepo = 'maven'
bintrayName = 'MyLibrary'
// Maven metadata
publishedGroupId = 'com.domain.name'
libraryName = 'MyLibrary'
artifact = 'custom-name'
libraryDescription = 'description'
libraryVersion = '0.0.1'
developerId = 'someone'
developerName = 'someone'
developerEmail = 'someone#gmail.com'
}
I know all of this is possible since I've seen a couple of repositories using this module name structure and having "custom" artifactId on bintray/JCenter.
Example:
Library 1
Library 2
All you have to do is add
archivesBaseName = 'myartifactid'
to your library module gradle file. This will rename all archive outputs, so not only aar but also javadoc and sources, for example.
Using the Bintray release plugin
When using com.novoda:bintray-release, use artifactId instead of artifact in build.gradle publish configuration:
publish {
// ...
artifactId = sporkArtifactId
// ...
}
Using your second example's gradle-maven-push
See the build.gradle that applies gradle-mvn-push.gradle
The settings are defined in gradle.properties:
POM_NAME=FloatingActionButton
POM_ARTIFACT_ID=floatingactionbutton
POM_PACKAGING=aar