2

I am using Gradle 4.1 with Gradle-Android plugin 3.0.1 on Android Studio 3.2

I have 2 flavors 'production' and 'staging' and I am unable to build my project as a library with different build variants.

app build.gradle:

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
  ...

    productFlavors {
        production {        
        }

        staging {  
        }
    }

    defaultPublishConfig "productionRelease"
    publishNonDefault true
}

if( android.productFlavors.size() > 0 ) {
    android.libraryVariants.all { variant ->
        if( android.publishNonDefault && variant.name == android.defaultPublishConfig ) {
            def bundleTask = tasks["bundle${name.capitalize()}"]
            artifacts {
                archives(bundleTask.archivePath) {
                    classifier name.replace('-' + variant.name, '')
                    builtBy bundleTask
                    name name.replace('-' + variant.name, '')
                }
            }
        }

...

Then I run: ./gradlew clean install, errors I got is:

Execution failed for task ‘:app:install’.

Could not publish configuration ‘archives’ A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact app:aar:aar:null, trying to add MavenArtifact app:aar:aar:null.

And to get this code to compile, I need to swap android.publishNonDefault with true, otherwise I will get an error of: Cannot get the value of write-only property 'publishNonDefault'

Any suggestions or hint would be really helpful, the aim is to build the library module on jitpack, where we can import it in project with build variants. thanks!

Peter Chikov
  • 172
  • 3
  • 16
Teresa
  • 373
  • 2
  • 8
  • The error suggests that it already published a flavour with `null` classifier and it fails for the second time. I would verify that the correct parameters are being passed to the archives dsl. – jbarat Oct 08 '18 at 13:50

2 Answers2

1

After digging into this for 2 days, and emailing Jitpack support, the issue is because the lib has updated and publishNonDefault is deprecated. you just need to change your app build.gradle to:

apply plugin: 'com.github.dcendents.android-maven'
dependencies {...}

group = 'com.github.your-group'

if (android.productFlavors.size() > 0) {
    android.libraryVariants.all { variant ->
        if (variant.name.toLowerCase().contains("debug")) {
            return
        }

        def bundleTask = tasks["bundle${variant.name.capitalize()}"]

        artifacts {
            archives(bundleTask.archivePath) {
                classifier variant.flavorName
                builtBy bundleTask
                name = project.name
            }
        }
    }
}
Teresa
  • 373
  • 2
  • 8
0

problem was to create multiple flavors using jitpack so what I do is, create a variable which stores the flavor name, after that loop through the available variant, pass the flavorBuild to artifact so when u push the code to GitHub and create artifact via jitpack then jitpack create the required implementation based on your build flavor and then u can use it. You need to just change build flavor

publishing {
    publications {
        def flavorBuild ='production'
        android.libraryVariants.all { variant ->

            "maven${variant.name.capitalize()}Aar"(MavenPublication) {
                from components.findByName("android${variant.name.capitalize()}")
                groupId = 'com.examplesdk'
                artifactId = 'examplesdk'
                version "1.0.0-${variant.name}"
                   artifact "$buildDir/outputs/aar/examplesdk-${flavorBuild}-release.aar"

                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    configurations.api.allDependencies.each { dependency ->
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dependency.group)
                        dependencyNode.appendNode('artifactId', dependency.name)
                        dependencyNode.appendNode('version', dependency.version)
                    }
                }
            }
        }
    }
    repositories{
        maven {
            url "$buildDir/repo"
        }
    }
}
  • change flavorBuild value as per the need – Kartikeya Tiwari Sep 14 '21 at 12:10
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. – cursorrux Sep 14 '21 at 12:36
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 14 '21 at 12:52