Cannot configure the ‘publishing’ extension after it has been accessed

i saw several posts regarding this error involving the’new’ maven-publish plugin we use instead of the old ‘maven’ plugin and for android the extra plugin to generate the pom file.

everything worked fine until i decided i need to put my versionCode and versionName inside a properties file and i gradle is activated with ./gradlew -PincreaseVersion bintrayUpload i incease the version in the property file, i do that BEFORE evaluation since i’m using getVersionName() and getVersionCode() methods inside the ‘android’ block.

moving the maven-publish ‘apply plugin’ to right before the publication {} clause OR changing publication to publication.publishing {} will work but will cause NullPointerException in the sample app project that is built as part of the project (firebase-jobdispatcher-android)

my lib gradle file looks like this:

apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.android.library'
//apply from: 'coverage.gradle'


//Properties versionProperties = readVersionProperties()
//updateProperties(versionProperties)

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1//getVersionCode()
        versionName "aaa"//getVersionName()
    }

    buildTypes {
        debug {
            testCoverageEnabled true
        }
        release {
            minifyEnabled false
        }
    }
}


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
}

dependencies {
    testCompile "junit:junit:4.12"
    testCompile "org.robolectric:robolectric:3.0"
    testCompile "org.mockito:mockito-core:1.+"

    compile "com.android.support:support-v4:23.3.0"
}

publishing.publications {
        aar(MavenPublication) {
            groupId = 'org.c0d3scr1b3r'
            artifactId "${project.name}"
            version = android.defaultConfig.versionName
            artifact "${project.buildDir}/outputs/aar/${project.name}-release.aar"
//            artifact androidJavadocsJar
            artifact androidSourcesJar
            pom.withXml {
                def root = asNode()
                def license = root.appendNode('licenses').appendNode('license')
                license.appendNode('name', 'The Apache Software License, Version 2.0')
                license.appendNode('url', 'http://www.apache.org/licenses/LICENSE-2.0.txt')
                license.appendNode('distribution', 'repo')
                def scm = root.appendNode('scm')
                scm.appendNode('connection', 'https://github.com/eyal-rounds/firebase-jobdispatcher-android.git')
                scm.appendNode('url', 'https://github.com/eyal-rounds/firebase-jobdispatcher-android')
                root.appendNode('developers').appendNode('developer').appendNode('name', 'c0d3sc1b3r')
                def dependencies = root.appendNode('dependencies')
                configurations.getByName("_releaseCompile").
                        getResolvedConfiguration().getFirstLevelModuleDependencies().each {
                    def dependency = dependencies.appendNode('dependency')
                    dependency.appendNode('groupId', it.moduleGroup)
                    dependency.appendNode('artifactId', it.moduleName)
                    dependency.appendNode('version', it.moduleVersion)
                }
            }
        }
}


bintray {
    user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
    key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')
    if( user == null ){
        println("no user defined!!!")
    }
    if( key == null ){
        println("no key defined!!!")
    }
    publications = ['aar']
    publish = true
    pkg {
        repo = 'maven'
        name = 'firebase-jobdispatcher-android'
        licenses = ['Apache-2.0']
        vcsUrl = 'https://github.com/eyal-rounds/firebase-jobdispatcher-android.git'
        publicDownloadNumbers = true

        version {
            name = android.defaultConfig.versionName
            desc = 'firebase jobDispatcher for android'
        }
    }
}

def int getVersionCode(Properties props){
    String codeAsString = props.get("version.code")
    return codeAsString as int
}

def String getVersionName(Properties props){
    return props.get("version.name")
}

def Properties readVersionProperties(){
    Properties properties = new Properties()
    properties.load(new FileInputStream("version.properties"))
    return properties
}

def void updateProperties(Properties props){
    if( 'updateVersion' in project.properties ){
        updateVersionName(props)
        updateVersionCode(props)
        prop.store(new FileOutputStream("version.properties"),"")
    }
}

def void updateVersionName(Properties props){
    String asString = props["version.name"]
    def asList = asString.split("\\.")
    int idx = asList.size() - 1
    boolean done = false
    int carry = 0
    while( ! done && idx >= 0 ){
        int asNumber = asList[idx] as int
        int temp = asNumber + 1 + carry
        asNumber = temp % 10
        carry = temp / 10
        done = carry == 0
        asList[idx] = (asNumber as String)
        idx++
    }
    props["version.name"] = asList.join("\\.")
}

def void updateVersionCode(Properties props){
    int versionCode = props["version.code"] as int
    props["version.code"] = (versionCode + 1) as String
}

this file works! but i i try to remove the comment from getVersionName() and getVersionCode() i get either NullPointerException OR the error that is in the title.

i work with gradle 2.14.1, oracle java jdk 8 and linux mint 17.3 cinamon. I run the gradle from the command line.

Any idea how to solve this, looks like an inner gradle conflict of some kind, race condition of whom is initialized first.

my bad.
i’m not passing properties object to the methods in the android clause.
shame that not even the stacktrace reveals it…