4

I have three build variants - staging, debug and production in android library. I need to publish the aar file on maven but the constraint is that if currently selected build variant is debug or staging then only debugAar should publish and if release is selected then releaseAar should publish. By default gradle only publishes releaseAar.

Currently everytime I want debug variant I need to change "artifact(bundleReleaseAar)" to "artifact(bundleDebugAar)"

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'dexguard'

def libraryGroupId = '*.*'
def libraryArtifactId = 'xyz'
def libraryVersion = projectxyzVersion

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        flatDir { dirs 'lib' }
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath ':dexguard:'
    }
}

android {
    compileSdkVersion projectCompileSdkVersion


    defaultConfig {
        minSdkVersion projectMinSdkVersion
        targetSdkVersion projectTargetSdkVersion
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
        debug {
            /*ext.enableHanselLocalization = false
            ext.enableHansel = false*/
            debuggable true
            buildConfigField ('String', 'BUILD_TYPE_STRING', '"debug"')

            //todo only for testing, remove while making final aar
            proguardFile getDefaultDexGuardFile('dexguard-library-debug.pro')
            proguardFile 'dexguard-project.txt'
        }
        staging {
            debuggable true
            buildConfigField ('String', 'BUILD_TYPE_STRING', '"staging"')
            /*ext.enableHanselLocalization = false
            ext.enableHansel = false*/
        }
        release {
            /*ext.enableHanselLocalization = false
            ext.enableHansel = false*/
            buildConfigField ('String', 'BUILD_TYPE_STRING', '"release"')
//             minifyEnabled true
//            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            proguardFile getDefaultDexGuardFile('dexguard-library-release.pro')
            proguardFile 'dexguard-project.txt'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "com.android.support:appcompat-v7:${projectSupportLibraryVersion}"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation ("com.google.android.gms:play-services-safetynet:${projectSafetyNetVersion}") {
        exclude group: "androidx.core"
    }
    implementation "com.android.volley:volley:${projectVolleyVersion}"
    implementation "com.google.code.gson:gson:${projectGsonVersion}"
}

publishing {
    publications {
        aar(MavenPublication) {
            groupId libraryGroupId
            version libraryVersion
            artifactId libraryArtifactId

            artifact(bundleReleaseAar)

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                def deps = configurations.implementation.allDependencies + configurations.compile.allDependencies + configurations.api.allDependencies
                deps.each {
                    if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null && !"unspecified".equals(it.version)) {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version',it.version)
                    }
                }
            }
        }
    }
}

artifactory {
    contextUrl = 'https://artifactory.paytm.in/'
    publish {
        repository {
            repoKey = "xyz-android"

            username = xyz_artifactory_username
            password = xyz_artifactory_password
        }
        defaults {
            publications('aar')
            publishArtifacts = true

            properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
            publishPom = true

        }
    }
}
Jaggs
  • 75
  • 2
  • 6
  • this isn't really an answer to your question, but i've done a workaround for this where you only have one module and pass through any build variant related variables through to the module from the app, instead of having different modules – a_local_nobody Jan 23 '20 at 11:34
  • so, one module, different app variants – a_local_nobody Jan 23 '20 at 11:35

0 Answers0