Classification of artifacts in Gradle 5? - android

Problem:
Publish javadoc and sources for a gradle project. The following code works well, even on Gradle 5.1.1:
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))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
However, in Gradle 5.1.1 the following statements are deprecated:
classifier = 'sources'
...
classifier = 'javadoc'
Looking at the javadoc for the evaluated method name reveals:
Deprecated.
Use getArchiveClassifier()
Source: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/bundling/AbstractArchiveTask.html#setClassifier-java.lang.String-
This doesn't make sense to me.
What change is required for my code to continue to work and not be deprecated?

Following will not show deprecation warning:
archiveClassifier.set("sources")
archiveClassifier.set("javadoc")

Related

How to attach the Kotlin sources to android library project?

I have an android library project (.aar file, not .jar), and attaching java sources with task
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
But kotlin fource files are not showing up for users of my library. How can I add kotlin source files as well?
Use
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
And it will add kotlin sources as well.
For .jar library, you can use
task sourceJar(type: Jar) {
from sourceSets.main.allSources
}
instead of
from sourceSets.main.allJava

Obfuscated interface parameter names in gradle library

I have made a library with some interfaces (HybridMediaPlayer on github). When I import it from gradle in new project and use those interfaces I get changed parameter names such as:
player.setOnPositionDiscontinuityListener(new ExoMediaPlayer.OnPositionDiscontinuityListener() {
#Override
public void onPositionDiscontinuity(int i, int i1) {
}
});
Where the "i" is "reason" and "i1" is "currentWindowIndex".
Why it is changed in other project and how to fix that? Proguard is disabled.
I think this is because on the Maven repo only a Android Archive Library (AAR) exists and no source jar. The AAR only contains the compiled classes and in compiled classes the full variable name is not known anymore. So when your IDE implements the method it doesn't know the names anymore so it defaults to standard naming based on argument types (hence the i for integer).
If you want the correct variable names you should publish a source jar of your project as well to the Jitpack repo. This answer might provide a way to also publish a source jar next to the AAR. When a source jar is also published an IDE will use Gradle to also pull the source jar into the project and will use this when implementing methods to get argument names and such.
The solution for jitpack was adding javadoc for interfaces and this in lib gradle build file:
// build a jar with source files
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += configurations.compile
}
// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
There are two reasons why this could happen:
1. You haven't included any documentation as artifact in your AAR
If this the case then you need to add following tasks in AAR's build.gradle as follows:
task javadoc(type: Javadoc) {
description "Generates Javadoc for ${archivesBaseName}-${version} API"
group JavaBasePlugin.DOCUMENTATION_GROUP
title = "${archivesBaseName}-${version} API References"
source android.sourceSets.main.java.srcDirs, configurations.doc.collect { zipTree(it) }
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
options {
memberLevel = JavadocMemberLevel.PUBLIC
linksOffline('http://developer.android.com/reference/', "${android.sdkDirectory}/docs/reference");
}
include '<path of your public api>/*.java'
exclude '**/BuildConfig.java'
exclude '**/R.java'
exclude '**/test/**/*.java'
failOnError false
}
task androidJavadocsJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives androidJavadocsJar
}
2. You have already done step 1
If this is the case you need to inform android studio to download the javaDoc. Place following code in your target app's main build.gradle
apply plugin: 'idea'
...
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
Alternatively, you can follow this SO to do it through android studio.
Note: You need to properly document the Javadoc download in User guide of your AAR so that user of your AAR knows how to overcome the impediment

Attach sources to kotlin library project don't show up in AS

I want to attach the sources to an kotlin library project and it looks I succeeded as I have the source-jars now in here:
https://jitpack.io/com/github/walleth/kethereum/bip44/0.21/
the version before I did not export the sources - so I thought it is successful.
https://jitpack.io/com/github/walleth/kethereum/bip44/0.20/
Unfortunately AS does not show the sources. I am generating them like this:
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
apply plugin: "kotlin"
apply plugin: "jacoco"
apply plugin: "maven"
apply plugin: "com.github.ben-manes.versions"
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile 'junit:junit:4.12'
}
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 sourcesJar
archives javadocJar
}
}
Also the source-jars contain the kotlin files.
It is all in this project: https://github.com/walleth/kethereum
This is what we have for Maven Publication.
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
And then you can use artifact androidSourcesJar.

Publish Android library to Maven that depends on a local Android library project

I have a library project, libParent, that includes another library project, libChild, as a dependency. If you look at the build.gradle dependencies section, you will see something like the following:
dependencies {
...
compile project(':libChild')
}
Now, I want to publish libParent to maven as an aar file. Note that libChild has not been published, but I would like to include its sources/resources within libParent's artificat. The maven publishing section of my build.gradle file looks like the following:
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))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
Once I publish my module and include it as a dependency in my app, I get the following error:
Could not find 'libParent:libChild:unspecified'
Searched in the following locations:
https://oss.sonatype.org/content/respositories/snapshots/libParent/libChild/unspecified/libChild-unspecified.pom
I know that libChild is not getting baked into my published artifact, but how do I go about making sure that the sources/resources of libChild get included?
You can't do it.
If a library has a dependency, the pom file of the parent lib will contain this dependency.
You can't resolve this dependency including the sources/resources.
You have to publish also the child lib.

Gradle: DefaultAndroidSourceDirectorySet to File using toString() method has been deprecated

I want to update the Gradle plug-in of an Android library project. The new version is 0.10.4. The Gradle wrapper is at 1.10. The following warning appears when I run ./gradlew install on the project.
Converting class com.android.build.gradle.internal.api. \
DefaultAndroidSourceDirectorySet to File using toString() method has
been deprecated and is scheduled to be removed in Gradle 2.0.
Please use java.io.File, java.lang.String, java.net.URL, or java.net.URI instead.
I am not sure but the marked lines should the cause:
// build.gradle
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java // <----
}
task androidJavadocsJar(type: Jar) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java // <----
}
How can I rewrite the code to get rid of the warning?
android.sourceSets.main.java doesn't have the type you expect. You're passing it to something that expects a File[], but it actually has the type com.android.build.gradle.internal.api.DefaultAndroidSourceDirectorySet. If you look at the API for Android sourceSets at you'll find that there's a sourceDirs method that returns what you want. So set up your tasks like this:
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
}

Categories

Resources