I have an Android library project and I am trying to publish the AAR files to JFrog artifactory using gradle.
Once I have the AAR files and when I execute the build task, the publish is working as expected, the problem is that I am not able to do it as part of my build process if the AAR files are not there.
I want to publish the AAR files when ever a new one is available. I tried to put assembleIntegrated.finalizedBy (artifactoryPublish), but that didn’t help.
The publish task gets triggered before the AARs are generated.
mygradle.gradle -->
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
File AARFile1 = file("$buildDir/outputs/aar/my_aar_file1.aar")
File AARFile2 = file("$buildDir/outputs/aar/my_aar_file2.aar")
publishing {
publications {
AAR1(MavenPublication) {
groupId repoFolder
version libVersion
// Tell maven to prepare the generated "*.aar" file for publishing
if (AARFile1.exists()) {
artifactId libRelease
artifact(AARFile1)
} else {
println 'AAR1 files not found in' + AARFile1.absolutePath
}
}
AAR2(MavenPublication) {
groupId repoFolder
version libVersion
// Tell maven to prepare the generated "*.aar" file for publishing
if (AARFile2.exists()) {
artifactId libDebug
artifact(AARFile2)
} else {
println 'AAR2 files not found in' + AARFile2.absolutePath
}
}
}
}
artifactory {
contextUrl = "https://bintray.com/jfrog/artifactory:8080"
publish {
repository {
// The Artifactory repository key to publish to
repoKey = 'my_key'
username = 'my_username'
password = 'my_encrypt_password'
}
defaults {
// Tell the Artifactory Plugin which artifacts should be published to Artifactory.
if (AARFile1.exists() || AARFile2.exists()) {
publications('AAR1', 'AAR2')
publishArtifacts = true
// Properties to be attached to the published artifacts.
properties = ['qa.level': 'basic', 'dev.team':'Me' ]
// Publish generated POM files to Artifactory (true by default)
publishPom = true
}
}
}
}
I see the gradle tasks list as below:
executing tasks: [assembleIntegrated]
AAR1 files not found in /myfolder/.../my_lib_project/app/build/outputs/aar/my_aar_file1.aar
AAR2 files not found in /myfolder/.../my_lib_project/app/build/outputs/aar/my_aar_file2.aar
.
.
.
> Task :app:preBuild UP-TO-DATE
> Task :app:test UP-TO-DATE
> Task :app:check
> Task :app:build
> Task :app:artifactoryPublish
> Task :artifactoryDeploy
It happens, because you add files manually to the maven publication. When maven publish runs, these files are not exists. So, you should configure task dependencies manually. When you add a publication to your project, Gradle will generate some tasks with combination of your publication names, and repo names. Something like that: publish{publicationName}PublicationTo{RepositoryName}Repository. Thus, you should setup these task to depends on assembleIntegration task.
Or you can use android-maven-publish plugin, which do this work automatically.
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 have 2 library modules in my Android project.
mainlibrary
-sublibrary
The sublibrary module is being used in mainlibrary. Inside build.gradle (Module: mainlibrary), I have imported the entire sublibrary module -
implementation project(':sublibrary')
This is how I ship mainlibrary as a library using jFrog Artifactory, code present in build.gradle (Module: mainlibrary) -
publishing {
publications {
aar(MavenPublication) {
groupId 'in.mikel.reusablelibs'
version '1.0.4'
artifactId project.getName()
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
}
}
}
artifactory {
contextUrl = 'https://mikel.jfrog.io/artifactory/'
publish {
repository {
// The Artifactory repository key to publish to
repoKey = 'mikelcl-gradle-release-local'
username = "***"
password = "***"
}
defaults {
// Tell the Artifactory Plugin which artifacts should be published to Artifactory.
publications('aar')
publishArtifacts = true
// Properties to be attached to the published artifacts.
properties = ['qa.level': 'basic', 'dev.team': 'core']
// Publish generated POM files to Artifactory (true by default)
publishPom = true
}
}
}
I also want sublibrary to be packaged when publishing to Artifactory.
Do I need to write separate information in build.gradle (Module: sublibrary)? How can that be handled?
You don't have to write a separate build.gradle file. You can configure sublibrary in the root build.gradle.
For example:
project('sublibrary') {
publishing {
publications {
aar(MavenPublication) {
groupId 'in.mikel.reusablelibs'
version '1.0.4'
artifactId project.getName()
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
}
}
}
artifactoryPublish {
publications(publishing.publications.aar)
}
}
Make sure Artifactory and maven-publish plugins are applied in the sublibrary project:
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
Read more:
Example
Documentation
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am just changing from eclipse to android studio. To have the benefits, gradle is teaching us, I am trying to set up a local repository using jForgs Artifacts. Now I am facing the problem, that I want to push/publish a library I have written in Android Studio to artifacts so I can easily import it via a depency in my next android App project.
The Folder structure looks like this:
LibDeploy
build.gradle
gradle.properties
-> app
--> build
--> libs
--> src
--> main
--> java
build.gradle
-> gradle
--> wrapper
my gradle.build in the app folder look like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.0'
classpath 'com.github.dcendents:android-maven-plugin:1.0'
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3'
}
}
import java.text.SimpleDateFormat
def globalVersion = new Version(currentVersion)
// Define the artifacts coordinates
group = 'org.jfrog.example.android'
version = globalVersion
status = version.status
// Plugins
apply plugin: 'com.android.library'
apply plugin: 'artifactory'
// We need the patched maven plugin since 'install' task is overriden by 'installDebugTest', see: https://github.com/dcendents/android-maven-plugin
apply plugin: 'android-maven'
// Android
android {
compileSdkVersion 21
buildToolsVersion "21.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
}
}
configurations {
published
}
task sourceJar(type: Jar) {
from android.sourceSets.main.java
classifier "sources"
}
artifactoryPublish {
dependsOn sourceJar
}
artifacts {
published sourceJar
}
configure(install.repositories.mavenInstaller) {
pom.project {
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}
artifactory {
contextUrl = 'http://localhost:8080/artifactory'
publish {
repository {
repoKey = 'libs-snapshot-local' //The Artifactory repository key to publish to
username = artifactory_user //The publisher user name, property taken from gradle.properties
password = artifactory_password //The publisher password, property taken from gradle.properties
}
defaults {
publishConfigs('archives', 'published')
properties = ['build.status': "$it.project.status".toString()]
publishPom = true //Publish generated POM files to Artifactory (true by default)
publishIvy = false //Publish generated Ivy descriptor files to Artifactory (true by default)
}
}
resolve {
repository {
repoKey = 'libs-release' //The Artifactory (preferably virtual) repository key to resolve from
username = artifactory_user //Optional resolver user name (leave out to use anonymous resolution), property taken from gradle.properties
password = artifactory_password //The resolver password, property taken from gradle.properties
}
}
}
repositories {
jcenter()
}
// Our project dependencies
dependencies {
compile 'joda-time:joda-time:2.3'
// Backward compatibility for andoird <http://developer.android.com/tools/support-library/index.html>
//compile 'com.android.support:support-v4:19.1.+'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
}
class Version {
String thisVersion = 'default'
String status = 'default'
Version(String versionValue) {
thisVersion = versionValue
if (thisVersion.endsWith('-SNAPSHOT')) {
status = 'integration'
} else {
status = 'release'
}
}
String toString() {
thisVersion
}
}
When I am trying to push/publish to artifactory via gradlew artifactoryPublish
I am getting the error:
:app:sourceJar
FAILURE: Build failed with an exception.
* What went wrong:
Cannot convert the provided notation to a File or URI: [src/main/java].
The following types/formats are supported:
- A String or CharSequence path, e.g 'src/main/java' or '/usr/include'
- A String or CharSequence URI, e.g 'file:/usr/include'
- A File instance.
- A URI or URL instance.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
BUILD FAILED
I think the problem should be in this line in the build.gradle file
task sourceJar(type: Jar) {
from android.sourceSets.main.java
classifier "sources"
}
Does anybody have a great tut how to set up a central maven repo for our build process?
Thanks in advance!
I solved this by changing
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main
}
to
task sourcesJar(type: Jar) {
classifier = 'sources'
from 'src/main/java'
}
I am using Android Gradle plugin 0.13.2, Android Studio 0.8.11, Gradle 2.1 and maven plugin.
I would like to install multiple variants (flavour + build type) of my Android Library to local Maven repository all with one command (task).
Currently Android Gradle plugin 0.13.2 allows me to set publishNonDefault flag to publishing all variants, but as the documentation states it will publish the variants with a classifier which is not compatible with Maven Repository.
My workaround right now is to use defaultPublishConfig "myVariant" and change it for every variant I have.
apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
defaultPublishConfig "myVariant"
.
.
.
}
task installArchives(type: Upload) {
repositories.mavenInstaller {
configuration = configurations.getByName(Dependency.DEFAULT_CONFIGURATION)
pom.groupId = "com.company"
pom.artifactId = "mylibrary"
pom.version = "1.0.0-myVariant"
}
}
I would like to have a single task that would properly publish all variants to local Maven repository.
To solve this I had to create one Upload task for each variant and make them depend on each other and on a master task that starts the process.
apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
.
.
.
}
// Master task that will publish all variants
def DefaultTask masterTask = project.tasks.create("installArchives", DefaultTask)
android.libraryVariants.all { variant ->
variant.outputs.each { output ->
// Configuration defines which artifacts will be published, create one configuration for each variant output (artifact)
def Configuration variantConfiguration = project.configurations.create("${variant.name}Archives")
project.artifacts.add(variantConfiguration.name, output.packageLibrary)
// Create one Upload type task for each configuration
def Upload variantTask = project.tasks.create("${variant.name}Install", Upload)
variantTask.configuration = variantConfiguration
variantTask.repositories.mavenInstaller {
pom.groupId = "com.yourcompany"
pom.artifactId = "yourLibrary"
pom.version = "1.0.0-${variant.name}" //Give a different version for each variant
pom.packaging = "aar"
}
// Make all tasks depend on each other and on master task
masterTask.dependsOn variantTask
masterTask = variantTask
}
}
The task installArchives will publish all variants to the local Maven repository.
./gradlew installArchives