Android AspectJ with Gradle in multimodule project - android

Is there any way to apply aspectJ pointcuts from library module to whole project? It means that I need attach library to existitng project, and it must intercept method calls from main project with all it dependencies (also including Android SDK methods)
For now I trying to do this by script in build.gradle:
android.libraryVariants.all { variant ->
variant.javaCompile.doLast {
// Find the android.jar and add to iajc classpath
def androidSdk = android.adbExe.parent + "/../platforms/" + android.compileSdkVersion + "/android.jar"
println 'Android SDK android.jar path: ' + androidSdk
def iajcClasspath;
iajcClasspath = androidSdk;
project.rootProject.allprojects.each { proj ->
if (proj.configurations.hasProperty("compile"))
iajcClasspath += ":" + proj.configurations.compile.asPath
// handle aar dependencies pulled in by gradle (Android support library and etc)
tree = fileTree(dir: "${proj.buildDir}/exploded-aar", include: '**/classes.jar')
tree.each { jarFile ->
iajcClasspath += ":" + jarFile
}
}
println 'Classpath for iajc: ' + iajcClasspath
ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
println 'ajctPath : ' + configurations.ajc.asPath.toString()
println 'aspectPath : ' + configurations.aspects.asPath.toString()
println 'inpath : ' + configurations.ajInpath.asPath.toString()
ant.iajc(
source: sourceCompatibility,
target: targetCompatibility,
fork: true,
destDir: "${project.buildDir}/classes/${variant.dirName}",
aspectPath: configurations.aspects.asPath,
inpath: configurations.ajInpath.asPath,
sourceRootCopyFilter: "**/*.java",
classpath: iajcClasspath
) {
sourceroots {
android.sourceSets.main.java.srcDirs.each {
pathelement(location: it.absolutePath)
}
// Build config file
pathelement(location: "${project.buildDir}/source/buildConfig/${variant.dirName}")
// Android resources R.***
pathelement(location: "${project.buildDir}/source/r/${variant.dirName}")
}
}
}
}
But pointcuts works only on methods, called from this module. Also, even if I move script to main build.gradle, and replace android.libraryVariants to android.applicationVariants, pointcuts aren't work on attached .jar libraries and modules, but work on gradle dependencies (such as compile 'com.googlecode.mp4parser:isoparser:1.0.1', for example).
And if there no way to do this with AspectJ, maybe there is some other way to intercept method calls in all project from project library? The most important thing is that there should not be any changes in main module code, just in library.

After read document from ant ajcTask, I finally implement with my gradle plugin GradleAndroidAspectJPlugin.
I use the iajc classpath and inpath property to specify which classes(jars) will be compile as classpath or will be re-compile from aspectj compiler.
def aopTask = project.task("compile${buildTypeName}AspectJ") {
doFirst {
project.configurations.aspectsInPath.each {
aspectsInPaths.add(it);
aspectsInPathsAbsolute.add(it.absolutePath);
}
}
doLast {
ant.taskdef(
resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
classpath: project.configurations.aspectjTaskClasspath.asPath
)
ant.iajc(
source: project.android.compileOptions.sourceCompatibility,
target: project.android.compileOptions.targetCompatibility,
fork: "true",
destDir: variant.javaCompile.destinationDir,
bootClasspath: project.android.bootClasspath.join(File.pathSeparator),
inpathDirCopyFilter: "java/**/*.class"
) {
classpath {
variant.javaCompile.classpath.each {
if (!aspectsInPathsAbsolute.contains(it)) {
pathElement(location: it)
}
}
}
inpath {
pathElement(location: copyDir)
aspectsInPaths.each {
if (!it.name.startsWith("aspectjrt")) {
pathElement(location: it)
}
}
}
}
}
}

Related

Setting up Protobuf + Kotlin in Android Studio 2023

I spend hours on just setting up Protobuf with Kotlin in Android Studio. The endgoal is just that my proto files are compiled in Kotlin and that I can use them in my project.
I have an example project here: https://github.com/Jasperav/ProtobufAndroid. It mimics my setup in the real application: an external dir containing the proto files and the android project. It contains all the code mentioned below. This is a combined effort of tutorials I found on the internet. It is probably terrible wrong. I tried https://github.com/google/protobuf-gradle-plugin, but it just looks so complicated for something simple I am doing:
Have a dir with protofiles somewhere on your filesystem
Create a new Android project on Kotlin
In the Project build.gradle, add id 'com.google.protobuf' version '0.9.2' apply false as plugin
In the Module build.gradle, add ->
This to the dependencies: implementation 'com.google.protobuf:protobuf-lite:3.21.12'
The sourceSets at the bottom inside the android bracket
The protobuf section at the bottom between the dependencies and android section.
sourceSets:
sourceSets {
main {
kotlin {
srcDirs += 'build/generated/source/proto/main/kotlin'
}
proto {
srcDir '/Users/me/androidkotlin/proto'
}
}
}
protobuf:
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.21.12'
}
plugins {
kotlinlite {
artifact = 'com.google.protobuf:protoc-gen-kotlin:3.21.12'
}
}
generateProtoTasks {
ofSourceSet("main").forEach { task ->
task.builtins {
getByName("kotlin") {
option("lite")
}
}
}
}
}
I get this error:
A problem occurred evaluating project ':app'.
> Could not find method proto() for arguments [build_cxwfo79b6zcc266x9rsqzou9f$_run_closure1$_closure8$_closure10$_closure12#203aac02] on source set main of type com.android.build.gradle.internal.api.DefaultAndroidSourceSet.
You are in a good way, but, there is some stuff missing:
The gradle code I'll share is written in Kotlin, just in case. If you can convert your grade files to Kotlin, nice, if not you have to convert them to groovy.
The first thing to check is if you have the proto folder in the right path, it should be in
root->app->src->main->proto
In the project gradle make sure to have the plugin applied
id("com.google.protobuf") version "0.8.15" apply false
In the app gradle, make sure to have the following.
import com.google.protobuf.gradle.*
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.protobuf")
}
The dependencies:
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
implementation("com.google.protobuf:protobuf-kotlin:3.21.2")
implementation("io.grpc:grpc-stub:1.52.0")
implementation("io.grpc:grpc-protobuf:1.52.0")
implementation("io.grpc:grpc-okhttp:1.52.0")
implementation("com.google.protobuf:protobuf-java-util:3.21.7")
implementation("com.google.protobuf:protobuf-kotlin:3.21.2")
implementation("io.grpc:grpc-kotlin-stub:1.3.0")
And the protobuf task:
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${rootProject.ext["protobufVersion"]}"
}
plugins {
id("java") {
artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.ext["grpcVersion"]}"
}
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.ext["grpcVersion"]}"
}
id("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:${rootProject.ext["grpcKotlinVersion"]}:jdk8#jar"
}
}
generateProtoTasks {
all().forEach {
it.plugins {
id("java") {
option("lite")
}
id("grpc") {
option("lite")
}
id("grpckt") {
option("lite")
}
}
it.builtins {
id("kotlin") {
option("lite")
}
}
}
}
}
These are the versions I'm using:
ext["grpcVersion"] = "1.47.0"
ext["grpcKotlinVersion"] = "1.3.0" // CURRENT_GRPC_KOTLIN_VERSION
ext["protobufVersion"] = "3.21.2"
ext["coroutinesVersion"] = "1.6.2"
Having that your project should generate the code based on your proto files.
For further reference, I recently build this Android App based on Kotlin + gRPC: https://github.com/wilsoncastiblanco/notes-grpc

Javadoc with shadow jar

I'm trying to create a Javadoc task in Gradle 7.2/android Gradle plugin 7.1.2 to document android libraries. One of the libraries creates a shadow jar at build time. The problem I'm having is defining the classpath of the Javadoc task.
afterEvaluate { project ->
task androidJavadocs(type: Javadoc) {
def releaseVariant = android.libraryVariants.matching { variant -> variant.name == "release" }.iterator().next()
source = android.sourceSets.main.java.source
releaseVariant.javaCompileProvider.get().classpath.files.each { v -> println "$v" }
println "---"
println project.android.getBootClasspath().each { v -> println "$v" }
println "---"
println files("$buildDir/intermediates/classes/release").files.each { v -> println "$v" }
classpath += project.files(releaseVariant.javaCompileProvider.get().classpath,
project.android.getBootClasspath().join(File.pathSeparator), files("$buildDir/intermediates/classes/release"))
title = null
options.noTimestamp(false)
options.doclet = "com.google.doclava.Doclava"
options.docletpath = configurations.doclava.files.asType(List)
}
}
For the library which uses dagger, this prints:
<snip>/RxAndroidBle/rxandroidble/build/intermediates/compile_r_class_jar/release/R.jar
/Users/nick/.gradle/caches/modules-2/files-2.1/com.jakewharton.rxrelay2/rxrelay/2.1.1/b5cb329f502caba70863749b6559f13eb25b6285/rxrelay-2.1.1.jar
/Users/nick/.gradle/caches/modules-2/files-2.1/io.reactivex.rxjava2/rxjava/2.2.17/a10e8688b858b3cc2ae5850224dd6ddd87652b83/rxjava-2.2.17.jar
/Users/nick/.gradle/caches/modules-2/files-2.1/androidx.annotation/annotation/1.1.0/e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8/annotation-1.1.0.jar
<snip>/RxAndroidBle/dagger-library-shadow/build/libs/dagger-library-shadow-1.13.0.jar
/Users/nick/.gradle/caches/modules-2/files-2.1/org.reactivestreams/reactive-streams/1.0.3/d9fb7a7926ffa635b3dcaa5049fb2bfa25b3e7d0/reactive-streams-1.0.3.jar
---
/Users/nick/Library/Android/sdk/platforms/android-31/android.jar
/Users/nick/Library/Android/sdk/build-tools/30.0.3/core-lambda-stubs.jar
---
<snip>/RxAndroidBle/rxandroidble/build/intermediates/classes/release
If I have already built the project, this works. But if I haven't, Gradle can't find dagger-library-shadow-1.13.0.jar. This error can be seen here: https://github.com/NRB-Tech/RxAndroidBle/runs/5599933013?check_suite_focus=true
Code with the problem: https://github.com/NRB-Tech/RxAndroidBle/blob/gradle-7/gradle/gradle-mvn-push.gradle#L119-L128
How can I define the classpath so it will only discover the files at build time? Or is there a way to build the shadow jar before evaluating the Javadoc task?

Gradle: how to get all the module dependencies of a project

I am trying to write a Gradle Plugin that runs Checkstyle over all the source code of a specific Android app, including all the variants and all the library modules that the app is using and generate a single report without duplicate errors. Currently I am able to run it through all the variants successfully and generate a report without duplications, but I have no idea how to get the list of modules that the app is using.
So, basically, if I have a project with this structure:
MyProject
+-MyAppFoo
+-MyAppBar
+-FeatureA
+-FeatureB
+-FeatureC
+-Core
And the app MyAppFoo has dependencies on the modules FeatureA and FeatureC and FeatureA depends on Core I would like to be able to access the project instance of FeatureA, Core and FeatureC to get their sources and classpaths.
My current code looks like this and I am applying it directly to only one app module of the project (eg. MyAppFoo):
apply plugin: 'checkstyle'
afterEvaluate {
def variants
if (project.plugins.hasPlugin('com.android.application')) {
variants = android.applicationVariants
} else if (project.plugins.hasPlugin('com.android.library')) {
variants = android.libraryVariants
} else {
return
}
def dependsOn = []
def classpath
def source
variants.all { variant ->
dependsOn << variant.javaCompiler
if (!source) {
source = variant.javaCompiler.source.filter { p ->
return p.getPath().contains("${project.projectDir}/src/main/")
}
}
source += variant.javaCompiler.source.filter { p ->
return !p.getPath().contains("${project.projectDir}/src/main/")
}
if (!classpath) {
classpath = project.fileTree(variant.javaCompiler.destinationDir)
} else {
classpath += project.fileTree(variant.javaCompiler.destinationDir)
}
}
def checkstyle = project.tasks.create "checkstyle", Checkstyle
checkstyle.group = "Verification"
checkstyle.dependsOn dependsOn
checkstyle.source source
checkstyle.classpath = classpath
checkstyle.exclude('**/BuildConfig.java')
checkstyle.exclude('**/R.java')
checkstyle.exclude('**/BR.java')
checkstyle.showViolations true
project.tasks.getByName("check").dependsOn checkstyle
}
What I would like to have is a list of projects containing only the modules that my MyAppFoo is using, this whay when I run gradle :MyAppFoo:checkstyle I want to run it on the modules MyAppFoo, FeatureA, Core and FeatureC.

Get dependencies from resolve configuration on Gradle 4.1

On previous versions of android plugin for Gradle I could, with my own task, take the path of the jar dependencies using this:
android.libraryVariants.all { variant ->
task "copyDependencies${variant.name.capitalize()}"(type: Copy) {
configurations.compile.files().each { dependency ->
from dependency.path
}
into project.projectDir.path + "/build/libs/${variant.name}"
}
}
But in the latest version of this plugin, compile pass to deprecated and they introduced api and implementation configurations, so when I try to use the previous code, gradle said that:
Resolving configuration 'api' directly is not allowed
Any suggestion for this new change introduced?
UPDATE
I have got a dependencies list and filter for configurations doing this:
android.libraryVariants.all { variant ->
task "copyDependencies${variant.name.capitalize()}"(type: Copy) {
from {
variant.getCompileConfiguration().files().each { dependency ->
configurations.api.getDependencies().each { configDep ->
if (dependency.name.contains(configDep.name)) {
from dependency.path
}
}
}
}
into project.projectDir.path + "/build/libs/${variant.name}"
}
}
But this solution still has problems, in addiction when project B is dependent on project A. Both with this task defined, Gradle doesn't build.
Finally, I found a solution for copy dependencies from a configuration:
android.libraryVariants.all { variant ->
task "copyDependencies${variant.name.capitalize()}"() {
outputs.upToDateWhen { false }
doLast {
println "Executing from in copyDependencies${variant.name.capitalize()}"
variant.getCompileConfiguration().getAllDependencies().each { dependency ->
configurations.api.getDependencies().each { configDep ->
if (dependency.name.contains(configDep.name)) {
println "Dependency detected: " + dependency.name
variant.getCompileClasspath().each { fileDependency ->
if (fileDependency.absolutePath.contains(dependency.name)) {
println fileDependency.absolutePath
copy {
from fileDependency.absolutePath
into project.projectDir.path + "/build/intermediates/bundles/${variant.name}/libs"
exclude '**/classes.jar'
}
}
}
}
}
}
}
}
}
Please see chapter 48.4 of the userguide to find all available configurations and which are to be consumed and / or resolved.

Publishing Android Library (aar) to Bintray with chosen flavors

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)
}
}
}

Categories

Resources