Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include different .aar depending on the android build variant

I am having 3 .aar files which are included in a module and the aar module in included in a app module.

Currently I have 3 build variants and for specific build variant I want to include specific aar.

Lets Suppose: I have 3 aars naming xyz, pqr, def

xyz to be included in 123 build variant pqr to be included in 456 build variant def to be included in 789 build variant

aar module build.gradle

File xyzFile= file("xyz.aar")
File pqrFile = file("pqr.aar")
File defFile = file("def.aar")


configurations.maybeCreate("xyz")
   artifacts.add("xyz", xyzFile)
configurations.maybeCreate("pqr")
 artifacts.add("pqr", pqrFile )
configurations.maybeCreate("def")
    artifacts.add("def", defFile )

app project build.gradle

apply plugin: 'com.android.application'

configurations {
    xyzDebugCompile
    xyzReleaseCompile
    xyzReleaseUnsignedCompile

    pqrDebugCompile
    pqrReleaseCompile
    pqrReleaseUnsignedCompile

    defDebugCompile
    defReleaseCompile
    defReleaseUnsignedCompile

}

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
   =

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        applicationId "cm.ez.eia"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName '0.9.2'
        multiDexEnabled true
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'FA-Comm' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }

        }
        release {
            debuggable false
            jniDebuggable false
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'FA-Comm' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }

            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        releaseUnsigned {
            debuggable false
            jniDebuggable false
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'FA-Comm' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "1g" //specify the heap size for the dex process
        preDexLibraries = false //delete the already predexed libraries
    }


    productFlavors {
        xyz {
            applicationId 'com.fieldaware.communicator'
            minSdkVersion 21
            targetSdkVersion 24
        }


        def {
            applicationId 'com.fieldaware.communicator'
            minSdkVersion 21
            targetSdkVersion 24
        }
        pqr {
            applicationId 'com.fieldaware.communicator'
            minSdkVersion 21
            targetSdkVersion 24
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':android-common-master')
    compile 'com.android.support:appcompat-v7:24.2.1'

    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.google.android.gms:play-services-ads:9.4.0'
    compile 'com.google.android.gms:play-services-auth:9.4.0'
    compile 'com.google.android.gms:play-services-gcm:9.4.0'



    //Aar Configs
    xyzDebugCompile project(path:':sdk',configuration: 'xyz')
    xyzReleaseCompile project(path:':sdk',configuration: 'xyz')
    xyzReleaseUnsignedCompile project(path:':sdk',configuration: 'xyz')


    pqrDebugCompile project(path: ':sdk', configuration: 'pqr')
    pqrReleaseCompile project(path: ':sdk', configuration: 'pqr')
    pqrReleaseUnsignedCompile project(path: ':sdk', configuration: 'pqr')

    defDebugCompile project(path: ':sdk', configuration: 'def')
    defReleaseCompile project(path: ':sdk', configuration: 'def')
    defReleaseUnsignedCompile project(path: ':sdk', configuration: 'def')
}

I want to include the specific .aar file according to the specific build variant.

This code is fetching incorrect aar files. Please suggest any solution

like image 419
Mohammed Rampurawala Avatar asked Mar 11 '23 07:03

Mohammed Rampurawala


1 Answers

As I am answering my own question because anyone faces same thing then there is proper solution available.

First of all: You cannot include 3 aars in a library project and choose them accordingly as of now I am using Android Studio 2.2.3

My approach to include 3 aars was.

I have created 3 library modules in my main application naming:

settings.gradle

include ':xyzaar', ':pqraar', ':defaar',...

xyzaar directory will include xyz.aar file

xyzaar-> build.gradle

configurations.maybeCreate("default")
artifacts.add("default", file('xyz.aar'))

Same as for other 2 cars

app-> build.gradle

xyzDebugCompile project(path: ':xyzaar')
xyzReleaseCompile project(path: ':xyzaar')
xyzReleaseUnsignedCompile project(path: ':xyzaar')

pqrDebugCompile project(path: ':pqraar')
pqrReleaseCompile project(path: ':pqraar')
pqrReleaseUnsignedCompile project(path: ':pqraar')

defDebugCompile project(path: ':defaar')
defReleaseCompile project(path: ':defaar')
defReleaseUnsignedCompile project(path: ':defaar')

Thanks P.S: This approach works for me very good and app doesn't include unwanted aar files

like image 189
Mohammed Rampurawala Avatar answered Apr 26 '23 18:04

Mohammed Rampurawala