I am trying to use the FindBugs plugin for Gradle with an Android build.
The build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
apply plugin: 'findbugs'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
}
But when I execute the check task it says
No classes configured for FindBugs analysis.
How do I configure classes for FindBugs analysis?
This is not possible at the moment as findbugs expect Gradle's normal Java SourceSets but the android plugin uses custom ones.
There's work planned in both Gradle and in the Android plugin to allow using the default SourceSets which will enable FindBugs.
You track this issue here: https://code.google.com/p/android/issues/detail?id=55839
In newer versions of Android Studio this problem could be attributed to the fact that the location of the classes directory has changed.
The new location (as of 0.8.2) is:
build/intermediates/classes/{build_variant_name}/
for example
build/intermediates/classes/debug/
task findbugs(type: FindBugs) {
excludeFilter = file('config/findbugs/findbugs.xml')
ignoreFailures = true
classes = fileTree('build/intermediates/classes/preproduction/')
source = fileTree('src/main/java/')
classpath = files()
effort = 'max'
reports {
xml {
destination "reports/findbugs.xml"
}
}
}
This is definitely possible. At least now.
The answer can be seen on https://gist.github.com/rciovati/8461832 at the bottom.
apply plugin: 'findbugs'
// android configuration
findbugs {
sourceSets = []
ignoreFailures = true
}
task findbugs(type: FindBugs, dependsOn: assembleDebug) {
description 'Run findbugs'
group 'verification'
classes = fileTree('build/intermediates/classes/debug/')
source = fileTree('src/main/java')
classpath = files()
effort = 'max'
excludeFilter = file("../config/findbugs/exclude.xml")
reports {
xml.enabled = true
html.enabled = false
}
}
check.doLast {
project.tasks.getByName("findbugs").execute()
}
The important part here is dependsOn: assembleDebug.
Without that you will get a No classes configured for FindBugs analysis error message.
Refer to this https://stackoverflow.com/a/7743935/1159930 for the exclude file.
I was able to solving the problem
by adding find bug as separate task
task findbugs(type: FindBugs) {
ignoreFailures = true
classes = fileTree('build/classes/debug/')
source = fileTree('src/main/java/')
classpath = files()
effort = 'max'
}
this task can run using
gradle findbugs
If you are using android-test plugin you have to exclude findbugsTestDebug task when build.
gradle build -x findbugsTestDebug
I have to specify the default sourceSet for findbugs. Initially it wasnt there so I was getting the error.
findbugs {
sourceSets = [] //Default sourceSet
toolVersion = "3.0.1"
ignoreFailures = true
reportsDir = file("$project.buildDir/findbugsReports")
effort = "max"
reportLevel = "low"
}
As other people wrote, you have to set sourceSets, i.e.:
task findbugs(type: FindBugs) {
// ignoreFailures = false
ignoreFailures = true
effort = 'max'
reportLevel = 'low'
excludeFilter = file("quality/findbugs/findbugs-filter.xml")
classes = files("${project.buildDir}/intermediates/javac/debug/classes",
source "${file(getProjectDir().getPath()).getAbsolutePath()}/src"
include '**/*.java'
exclude "${project.buildDir}/generated/**/*.java"
reports {
xml.enabled = true
xml {
destination file("findbugs/report.xml")
}
/*
html.enabled = true
html {
destination file("findbugs/report.html")
}
*/
/*
text.enabled = true
text {
destination file("findbugs/report.txt")
}
*/
}
classpath = files()
}
The problem is that when you upgrade version of Android Gradle Plugin, this path changes now and then.
In our project in different times it was of following values:
"${project.buildDir}/intermediates/classes/debug"
"${project.buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes"
"${project.buildDir}/intermediates/javac/debug/classes"
So if none of mentioned above values worked out, try to find actual classes in your build tree, maybe they just changed it again.
Related
I am trying to make Android App bundles for Google Play using Android Studio. Android Studio told that I need to upgrade Gradle for that. I migrated Gradle to 7.0.0 version (and updated Android Studio itself), but I am totally new in this, so now I have issues with Maven. I got the following error:
Build file
'/Users/administrator/noughts-and-crosses/node_modules/expo-application/android/build.gradle'
line: 2
A problem occurred evaluating project ':expo-application'.
Plugin with id 'maven' not found.
I found that maven is not used anymore, so I need to migrate to maven-publish. I changed maven to maven-publish, but now I have the following issue:
Build file
'/Users/administrator/noughts-and-crosses/node_modules/expo-application/android/build.gradle'
line: 28
A problem occurred evaluating project ':expo-application'.
Could not find method uploadArchives() for arguments [build_6lgwxdh9lx7r2mkhkk2he2s1g$_run_closure4#56af98de] on project
':expo-application' of type org.gradle.api.Project.
The build.gradle file with the issue looks like that:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
group = 'host.exp.exponent'
version = '3.1.2'
// Simple helper that allows the root project to override versions declared by this library.
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
// Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
// Creating sources with comments
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
// Put the androidSources and javadoc to the artifacts
artifacts {
archives androidSourcesJar
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: mavenLocal().url)
}
}
}
android {
compileSdkVersion safeExtGet("compileSdkVersion", 30)
defaultConfig {
minSdkVersion safeExtGet("minSdkVersion", 21)
targetSdkVersion safeExtGet("targetSdkVersion", 30)
versionCode 12
versionName '3.1.2'
}
lintOptions {
abortOnError false
}
}
if (new File(rootProject.projectDir.parentFile, 'package.json').exists()) {
apply from: project(":unimodules-core").file("../unimodules-core.gradle")
} else {
throw new GradleException(
'\'unimodules-core.gradle\' was not found in the usual React Native dependency location. ' +
'This package can only be used in such projects. Are you sure you\'ve installed the dependencies properly?')
}
dependencies {
unimodule 'unimodules-core'
implementation 'com.android.installreferrer:installreferrer:1.0'
}
So, the question is: how to fix the new issue?
Update. I read the article from the comment, and now the file looks like this:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
// Simple helper that allows the root project to override versions declared by this library.
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
// Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = 'host.exp.exponent'
artifactId = 'noughts-and-crosses'
version = '3.1.2'
artifact(sourcesJar)
}
}
}
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: mavenLocal().url)
}
}
}
android {
compileSdkVersion safeExtGet("compileSdkVersion", 30)
defaultConfig {
minSdkVersion safeExtGet("minSdkVersion", 21)
targetSdkVersion safeExtGet("targetSdkVersion", 30)
versionCode 12
versionName '3.1.2'
}
lintOptions {
abortOnError false
}
}
if (new File(rootProject.projectDir.parentFile, 'package.json').exists()) {
apply from: project(":unimodules-core").file("../unimodules-core.gradle")
} else {
throw new GradleException(
'\'unimodules-core.gradle\' was not found in the usual React Native dependency location. ' +
'This package can only be used in such projects. Are you sure you\'ve installed the dependencies properly?')
}
dependencies {
unimodule 'unimodules-core'
implementation 'com.android.installreferrer:installreferrer:1.0'
}
But it didn't solve the problem.
Error message:
No files to be analyzed
My findbugs configuration is like:
tasks.create([ "type" : FindBugs, "dependsOn" : "assemble", "group": "verification", "name": "findbugs"]) {
classes = files("$projectDir.absolutePath/build/intermediates/classes")
source = fileTree('src/main/java')
classpath = files()
}
Seems like the class path have changed in AGP 3.2 from build/intermediates/classes to build/intermediates/javac.
task findbugs(type: FindBugs) {
ignoreFailures = true
classes = files("${project.rootDir}/app/build/intermediates/javac",
"${project.rootDir}/database/build/intermediates/javac",
"${project.rootDir}/dataprovider/build/intermediates/javac")
source = fileTree('app/src/main/java/')
classpath = files()
reports {
html.enabled = true
xml.enabled = false
}
}
Just change the classes path in file findbugs.gradle from classes = fileTree("$project.buildDir/intermediates/classes/dev/debug/com/android"
to classes = fileTree("$project.buildDir/intermediates/javac/debug/compileDebugJavaWithJavac/classes/android".
I wanted to generate code coverage reports on my JUnit tests in my android project so I added the JaCoCo gradle plugin. This is my project level build.gradle file:
apply plugin: 'jacoco'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta6'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
subprojects { prj ->
apply plugin: 'jacoco'
jacoco {
toolVersion '0.7.6.201602180812'
}
task jacocoReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
group = 'Reporting'
description = 'Generate Jacoco coverage reports after running tests.'
reports {
xml {
enabled = true
destination "${prj.buildDir}/reports/jacoco/jacoco.xml"
}
html {
enabled = true
destination "${prj.buildDir}/reports/jacoco"
}
}
classDirectories = fileTree(
dir: 'build/intermediates/classes/debug',
excludes: [
'**/R*.class',
'**/BuildConfig*',
'**/*$$*'
]
)
sourceDirectories = files('src/main/java')
executionData = files('build/jacoco/testDebugUnitTest.exec')
doFirst {
files('build/intermediates/classes/debug').getFiles().each { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
}
jacoco {
toolVersion '0.7.6.201602180812'
}
task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') {
group = 'Reporting'
description = 'Generates an aggregate report from all subprojects'
//noinspection GrUnresolvedAccess
dependsOn(subprojects.jacocoReport)
additionalSourceDirs = project.files(subprojects.jacocoReport.sourceDirectories)
sourceDirectories = project.files(subprojects.jacocoReport.sourceDirectories)
classDirectories = project.files(subprojects.jacocoReport.classDirectories)
executionData = project.files(subprojects.jacocoReport.executionData)
reports {
xml {
enabled = true
destination "${buildDir}/reports/jacoco/full/jacoco.xml"
}
html {
enabled = true
destination "${buildDir}/reports/jacoco/full"
}
}
doFirst {
//noinspection GroovyAssignabilityCheck
executionData = files(executionData.findAll { it.exists() })
}
}
It works great by running ./gradlew jacocoFullReport. But unfortunately coverage is not reported for the tests that are run with the RobolectricTestRunner (instructions that are obviously called in the tests are not reported as covered). Tests with no #RunWith annotation or run with MockitoJUnitTestRunner report coverage just fine.
Any help would be appreciated to fix this problem.
Update 1: I noticed that I should be using the RobolectricGradleTestRunner. But it didn't help.
It is known issue with the possible workaround - https://github.com/jacoco/jacoco/pull/288
Or downgrade jacoco version to 0.7.1.201405082137
UPDATE
The workaround is not needed anymore. You must use gradle version 2.13 and jacoco version 0.7.6.201602180812.
Update root build.gradle:
buildscript {
dependencies {
classpath 'org.jacoco:org.jacoco.core:0.7.6.201602180812'
}
}
task wrapper( type: Wrapper ) {
gradleVersion = '2.13'
}
Run ./gradlew wrapper
Update project build.gradle:
apply plugin: 'jacoco'
android {
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
}
The accepted answer is a bit dated. Here is a similar fix we just implemented. In the module (i.e. app) build.gradle add:
apply plugin: 'jacoco'
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
This does require JaCoCo 7.6+, but you are likely using it already.
Notes for Studio:
This only fixes the CLI. If you run coverage from Studio using JaCoCo, the Robolectric coverage is still not reported. The default IntelliJ Coverage Runner seems to work fine.
The test were crashing intermittently in Studio unless I added -noverify to the Android JUnit -> VM Options
I was facing the same issue but now it is resolved for me by following this link,
issue link: https://github.com/robolectric/robolectric/issues/2230
Solution for this problem is mentioned here:
https://github.com/dampcake/Robolectric-JaCoCo-Sample/commit/f9884b96ba5e456cddb3d4d2df277065bb26f1d3
I had the same issue. I changed the jacoco plugin version and added includenolocationclasses property. Here is the working jacoco.gradle file (I am using gradle wrapper 2.14.1):
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.6.201602180812"
}
android {
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
}
project.afterEvaluate {
// Grab all build types and product flavors
def buildTypes = android.buildTypes.collect { type -> type.name }
def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
println(buildTypes)
println(productFlavors)
// When no product flavors defined, use empty
if (!productFlavors) productFlavors.add('')
productFlavors.each { productFlavorName ->
buildTypes.each { buildTypeName ->
def sourceName, sourcePath
if (!productFlavorName) {
sourceName = sourcePath = "${buildTypeName}"
} else {
sourceName = "${productFlavorName}${buildTypeName.capitalize()}"
sourcePath = "${productFlavorName}/${buildTypeName}"
}
def testTaskName = "test${sourceName.capitalize()}UnitTest"
println("SourceName:${sourceName}")
println("SourcePath:${sourcePath}")
println("testTaskName:${testTaskName}")
// Create coverage task of form 'testFlavorTypeCoverage' depending on 'testFlavorTypeUnitTest'
task "${testTaskName}Coverage" (type:JacocoReport, dependsOn: "$testTaskName") {
group = "Reporting"
description = "Generate Jacoco coverage reports on the ${sourceName.capitalize()} build."
classDirectories = fileTree(
dir: "${project.buildDir}/intermediates/classes/${sourcePath}",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
def coverageSourceDirs = [
"src/main/java",
"src/$productFlavorName/java",
"src/$buildTypeName/java"
]
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
println("${project.buildDir}/jacoco/${testTaskName}.exec")
reports {
xml.enabled = true
html.enabled = true
}
}
}
}
}
This is an old issue, but for those who are still facing, it is worth mentioning that if you are setting up JaCoCo + Robolectric + Espresso - you will indeed be using includeNoLocationClasses, but still will see this error with Java9+ and probably end up here. Add the below snippet to your module build.gradle file
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
change coverage runner to jacoco in android studio
1- select app(root of the project)
2 click on menu (run --> Edit configurations --> code coverage --> choose JaCoCo).
I've juste added some flavors (or productFlavors if you want) to my project.
The fact is that when I publish the library to bintray, all flavors are uploaded (which is great), but I'm unable to use them. The plugin used is the official one here.
The uploaded aar:
androidsdk-0.0.4-fullRelease.aar
androidsdk-0.0.4-fullDebug.aar
androidsdk-0.0.4-lightRelease.aar
androidsdk-0.0.4-lightDebug.aar
As you noted, the fullRelease is named as the classifier, see doc chapter 23.4.1.3.
I am searching for a solution to choose which flavors that I want to upload.
I've already looked at bintray examples (here and here) and this, with also other examples but I'm still stuck.
Here is my current script:
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
buildscript {
repositories {
jcenter()
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 64
versionName "0.0.4"
}
publishNonDefault true
productFlavors {
full {
}
light {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
fullCompile 'com.squareup.picasso:picasso:2.5.0'
}
version = android.defaultConfig.versionName
uploadArchives {
repositories.mavenDeployer {
pom.project {
packaging 'aar'
}
}
}
////////////////////////////////
// Bintray Upload configuration
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 = "MyRepo" // repo name
userOrg = 'hugo'
name = "AndroidSDK" // Package name
websiteUrl = siteUrl
vcsUrl = gitUrl
publish = true
}
}
To import the library I'm currently using this:
compile ('com.example.lib:sdk:0.0.8:fullRelease#aar') {
transitive = true;
}
I faced the same challenge, and here's the best I could make yet:
Using mavenPublications and the gradle maven-publish plugin along the bintray plugin, you can publish any variant to mavenLocal and bintray.
Here's the publish.gradle file I apply at the end of all my project's library modules I want to publish:
def pomConfig = {
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'louiscad'
name 'Louis CAD'
email 'louis.cognault#gmail.com'
}
}
scm {
connection 'https://github.com/LouisCAD/Splitties.git'
developerConnection 'https://github.com/LouisCAD/Splitties.git'
url siteUrl
}
}
def publicationNames = []
publishing.publications {
android.libraryVariants.all { variant ->
if (variant.buildType.name == "debug") return // Prevents publishing debug library
def flavored = !variant.flavorName.isEmpty()
/**
* Translates "_" in flavor names to "-" for artifactIds, because "-" in flavor name is an
* illegal character, but is well used in artifactId names.
*/
def variantArtifactId = flavored ? variant.flavorName.replace('_', '-') : project.name
/**
* If the javadoc destinationDir wasn't changed per flavor, the libraryVariants would
* overwrite the javaDoc as all variants would write in the same directory
* before the last javadoc jar would have been built, which would cause the last javadoc
* jar to include classes from other flavors that it doesn't include.
*
* Yes, tricky.
*
* Note that "${buildDir}/docs/javadoc" is the default javadoc destinationDir.
*/
def javaDocDestDir = file("${buildDir}/docs/javadoc ${flavored ? variantArtifactId : ""}")
/**
* Includes
*/
def sourceDirs = variant.sourceSets.collect {
it.javaDirectories // Also includes kotlin sources if any.
}
def javadoc = task("${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for ${variant.name}."
source = variant.javaCompile.source // Yes, javaCompile is deprecated,
// but I didn't find any working alternative. Please, tweet #Louis_CAD if you find one.
destinationDir = javaDocDestDir
classpath += files(android.getBootClasspath().join(File.pathSeparator))
classpath += files(configurations.compile)
options.links("http://docs.oracle.com/javase/7/docs/api/");
options.links("http://d.android.com/reference/");
exclude '**/BuildConfig.java'
exclude '**/R.java'
failOnError false
}
def javadocJar = task("${variant.name}JavadocJar", type: Jar, dependsOn: javadoc) {
description "Puts Javadoc for ${variant.name} in a jar."
classifier = 'javadoc'
from javadoc.destinationDir
}
def sourcesJar = task("${variant.name}SourcesJar", type: Jar) {
description "Puts sources for ${variant.name} in a jar."
from sourceDirs
classifier = 'sources'
}
def publicationName = "splitties${variant.name.capitalize()}Library"
publicationNames.add(publicationName)
"$publicationName"(MavenPublication) {
artifactId variantArtifactId
group groupId
version libraryVersion
artifact variant.outputs[0].packageLibrary // This is the aar library
artifact sourcesJar
artifact javadocJar
pom {
packaging 'aar'
withXml {
def root = asNode()
root.appendNode("name", 'Splitties')
root.appendNode("url", siteUrl)
root.children().last() + pomConfig
def depsNode = root["dependencies"][0] ?: root.appendNode("dependencies")
def addDep = {
if (it.group == null) return // Avoid empty dependency nodes
def dependencyNode = depsNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (it.hasProperty('optional') && it.optional) {
dependencyNode.appendNode('optional', 'true')
}
}
// Add deps that everyone has
configurations.compile.allDependencies.each addDep
// Add flavor specific deps
if (flavored) {
configurations["${variant.flavorName}Compile"].allDependencies.each addDep
}
// NOTE: This library doesn't use builtTypes specific dependencies, so no need to add them.
}
}
}
}
}
group = groupId
version = libraryVersion
afterEvaluate {
bintray {
user = bintray_user
key = bintray_api_key
publications = publicationNames
override = true
pkg {
repo = 'splitties'
name = project.name
desc = libraryDesc
websiteUrl = siteUrl
issueTrackerUrl = 'https://github.com/LouisCAD/Splitties/issues'
vcsUrl = gitUrl
licenses = ['Apache-2.0']
labels = ['aar', 'android']
publicDownloadNumbers = true
githubRepo = 'LouisCAD/Splitties'
}
}
}
In order for this to work, I need to have the bintray_user and bintray_api_key properties defined. I personally just have them in my ~/.gradle/gradle.properties file like this:
bintray_user=my_bintray_user_name
bintray_api_key=my_private_bintray_api_key
I also need to define the following ext properties I used in the publish.gradle file in my root project's build.gradle file:
allprojects {
...
ext {
...
// Libraries
groupId = "xyz.louiscad.splitties"
libraryVersion = "1.2.1"
siteUrl = 'https://github.com/LouisCAD/Splitties'
gitUrl = 'https://github.com/LouisCAD/Splitties.git'
}
}
And now, I can finally use it in my android library module, where I have multiple productFlavors. Here's a snippet from a publishable library module's build.gradle file:
plugins {
id "com.jfrog.bintray" version "1.7.3" // Enables publishing to bintray
id "com.github.dcendents.android-maven" version "1.5" // Allows aar in mavenPublications
}
apply plugin: 'com.android.library'
apply plugin: 'maven-publish' // Used for mavenPublications
android {
...
defaultPublishConfig "myLibraryDebug" // Allows using this library in another
// module in this project without publishing to mavenLocal or Bintray.
// Useful for debug purposes, or for your library's sample app.
defaultConfig {
...
versionName libraryVersion
...
}
...
productFlavors {
myLibrary
myLibrary_logged // Here, the "_" will be replaced "-" in artifactId when publishing.
myOtherLibraryFlavor
}
...
}
dependencies {
...
// Timber, a log utility.
myLibrary_loggedCompile "com.jakewharton.timber:timber:${timberVersion}"; // Just an example
}
...
ext {
libraryDesc = "Delegates for kotlin on android that check UI thread"
}
apply from: '../publish.gradle' // Makes this library publishable
When you have all of this setup properly, with the name of your library instead of mine's (which you can use as an example), you can try publishing a version of your flavored library by trying to first publishing to mavenLocal.
To do so, run this command:
myLibrary $ ../gradlew publishToMavenLocal
You can then try adding mavenLocal in your app's repositories (example here) and try adding your library as a dependency (artifactId should be the flavor name, with "_" replaced with "-") and building it.
You can also check with your file explorer (use cmd+shift+G on Mac in Finder to access hidden folder) the directory ~/.m2 and look for your library.
When it's time to publish to bintray/jcenter, you just have to run this command:
myLibrary $ ../gradlew bintrayUpload
Important:
Before you publish your library to mavenLocal, Bintray or another maven repository, you'll usually want to try your library against a sample app which uses the library. This sample app, which should be another module in the same project just need to have the project dependency, which should look like this: compile project(':myLibrary'). However, since your library has multiple productFlavors, you'll want to test all of them. Unfortunately, it's currently impossible to specify which configuration you want to use from your sample app's build.gradle file (unless, you use publishNonDefault true in your library's build.gradle file, which breaks maven and bintray publications), but you can specify the default configuration (i.e. buildVariant) in your library's module as such: defaultPublishConfig "myLibraryDebug" in the android closure. You can see the available build variants for your library in the "Build Variants" tool Windows in Android Studio.
Feel free to explore my library "Splitties" here if you need an example. The flavored module is named concurrency, but I use my script for unflavored library modules too, and I tested it throughly on all the library modules in my project.
You can reach me out if you need help setting it up for you.
The setup:
buildTypes {
debug {
}
release {
}
}
publishNonDefault true
The fix:
defaultPublishConfig 'release'
// Fix for defaultPublishConfig not working as expected
// ref: https://github.com/dcendents/android-maven-gradle-plugin/issues/11
libraryVariants.all { variant ->
if( publishNonDefault && variant.name == defaultPublishConfig ) {
def bundleTask = tasks["bundle${variant.name.capitalize()}"]
artifacts {
archives(bundleTask.archivePath) {
classifier null //necessary to get rid of the suffix in the artifact
builtBy bundleTask
name name.replace('-' + variant.name, '')//necessary to get rid of the suffix from the folder name
}
}
}
}
This fix will still publish all the artifacts, but it will publish a default artifact without the flavour suffix, which is enough to make it all work.
The fix to upload only the default artifact would be this (if the bintray plugin knew what POM filters are):
install {
repositories.mavenInstaller {
/*
POM filters can be used to block artifacts from certain build variants.
However, Bintray does not respect POM filters, therefore this only works for maven deploy plugin.
Also, bintray crashes with named filters, since it always expects a /build/pom/pom-default.xml,
which does not happen with named filters.
*/
filter { artifact, file ->
// this how the default classifier is identified in case the defaultPublishConfig fix is applied
artifact.attributes.classifier == null
}
}
}
I didn't try it so I will delete the answer if it doesn't resolve the issue.
You should post a different artifact for each flavor (or build variant if you prefer).
In this way you will have in jcenter x artifacts, each of them with a pom file.
Something like:
groupId
|--library-full
|----.pom
|----.aar
|--library-light
|----.pom
|----.aar
In your top level file you can define
allprojects {
repositories {
jcenter()
}
project.ext {
groupId="xxx"
libraryName = ""
......
}
}
Then in your library module:
productFlavors {
full {
project.ext.set("libraryName", "library-full");
}
light {
project.ext.set("libraryName", "library-light");
}
}
bintray {
//...
pkg {
//...Do the same for other variables
name = project.ext.libraryName
}
}
Finally make sure to publish only the release build type (why also the debug version?)
If someone is still stuck with this problem here's what worked for me -
Let's say you want to publish the release build for your flavour1 add this to your build.gradle
android {
...
defaultPublishConfig "flavour1Release"
}
Remove publishNonDefault true if it is present in your gradle file.
Add this inside the bintray block like this
bintray {
...
archivesBaseName = 'YOUR_ARTIFACT_ID'
...
}
Then just run the bintrayUpload task as you would.
The defaultPublishConfig will have to be changed everytime you need to publish a new flavour.
It sounds like you don't want the classifier in the filename. It looks like the classifier is the same as the generated library file name. Have you tried giving them the same filename but outputting them to separate directories?
E.g. in the android scope:
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "same_name-${version}.aar"
output.outputFile = new File(outputFile.parent+"/${archivesBaseName}", fileName)
}
}
}
In my multi-module Android Studio project, I would like to create a plain java module. But in that module, I also want to be able to use certain Android API. Is this possible? If yes, how should build.gradle look like?
Thanks
jia
As long as the Android functionality that you need is in a jar instead of an aar, then you should be able to do this fairly easily as my team has a couple of artifacts like this. For Android jar artifacts in Maven Central, you just need to add the dependency:
compile 'com.google.android:android:4.1.1.4'
If the functionality is in one of the artifacts installed via the Android SDK Manager, then you can just add the dependency as above, but you'll need to add the local Android repo to pull the artifacts:
maven { url "file:///${System.env.ANDROID_HOME}/extras/android/m2repository" }
Edit
Also forgot to mention, you'll want to mark the Android artifacts as provided so that you don't get dependency clashes. You can do that by using the following:
configurations {
provided
compile.extendsFrom provided
}
dependencies {
provided('com.google.android:android:4.1.1.2')
}
Let me know if you need an example build.gradle and I will add one.
Edit 2
Below is an example build.gradle that we use for one of our projects.
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'pmd'
apply plugin: 'jacoco'
apply plugin: 'findbugs'
apply plugin: 'project-report'
apply plugin: 'jxr'
group = 'com.example'
archivesBaseName = 'project-name'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.7
configurations {
provided
compile.extendsFrom provided
}
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2/' }
maven { url "http://jcenter.bintray.com" }
}
dependencies {
classpath('net.davidecavestro:gradle-jxr-plugin:0.1')
}
}
repositories {
mavenCentral()
if (project.hasProperty("mavenLocal")) {
maven { url "${System.env.HOME}/.m2/repository" }
}
maven { url "file:///${System.env.ANDROID_HOME}/extras/android/m2repository" }
}
dependencies {
compile('com.google.code.findbugs:annotations:2.0.2')
compile('com.google.code.gson:gson:2.2.4')
compile('com.google.guava:guava:15.0')
provided('com.google.android:android:4.0.1.2')
testCompile('commons-io:commons-io:2.4')
testCompile('junit:junit:4.11')
testCompile('org.robolectric:robolectric:2.3')
testCompile('org.mockito:mockito-all:1.10.8')
}
test {
dependsOn ':assemble'
testLogging {
showExceptions = true
showStackTraces = true
exceptionFormat = "full"
events "passed", "skipped", "failed"
}
}
javadoc {
source = sourceSets.main.allJava
classpath = test.classpath
}
jacocoTestReport {
dependsOn test
description = "Generate Jacoco coverate reports after running tests."
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/reports/jacoco"
}
}
pmd.ignoreFailures = true
pmdTest.enabled = false
pmdMain.enabled = true
pmdMain {
reports {
xml.enabled = false
html.enabled = true
}
}
findbugs.ignoreFailures = true
findbugs.excludeFilter = file('./findbugs-exclude-filter.xml')
findbugsTest.enabled = false
findbugsMain.enabled = true
findbugsMain {
reports {
xml.enabled = false
html.enabled = true
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar) {
classifier = 'javadoc'
from "${projectDir}/build/docs"
}
artifacts {
archives sourcesJar
archives javadocJar
}
uploadSite.dependsOn(':check')
check.dependsOn('sourcesJar')
check.dependsOn('javadoc')
check.dependsOn('jacocoTestReport')
check.dependsOn('projectReport')
check.dependsOn('jxr')