Upload jar to local maven repository for each build type

I am developing an Android project with Android Studio & Gradle build.

I am using Gradle ‘maven-publish’ plugin to upload jar files to my local maven repo. I would like to upload jar for each build type. So I tried the following code:

apply plugin: 'maven-publish'
  buildTypes {
    release {
     ...
    }
    debug {
     ...
    }
}
  android.applicationVariants.all { variant ->
      def buildType = variant.buildType.name
      publishing {
        repositories {
              url PATH-TO-MY-LOCAL-MAVEN-REPO
        }
          publications {
              create('myJar', MavenPublication ) {
                artifact
  "$projectDir/my.jar"
                artifactId
"myJar-$buildType" //artifactId indicates build type
                  groupId
  "com.my.app"
                version
  "2.1.1"
            }
          }
    }
      build.dependsOn(publish)
}

But Gradle complains with the following error:

ERROR: Cannot configure the 'publishing' extension after it has been accessed.

How to get rid of this error? If it is a wrong way to do things, then how can I upload my jar for each build type to local maven then?

try …

publishing {
    publications {
        android.applicationVariants.all { variant ->
            create( ... ) { .... }
        }
    }
}

I have something similar to this in my build and it works fine. However I don’t use the android plugin, so there might be something going on inside of it that is resolving the publishing configuration.

Thanks, it works but not fullfill my purpose, could you please take a look at my question here : http://forums.gradle.org/gradle/topics/customize-publish-task-to-upload-jar-to-maven-repo-per-build-variant