如何根据android构建变体包含不同的.aar

时间:2016-12-29 04:53:29

标签: android gradle android-gradle build.gradle

我有3个.aar文件,它们包含在模块中,而aar模块包含在 app 模块中。

目前我有3个构建变体,对于特定的构建变体,我想要包含特定的aar。

让我们假设: 我有3个aars命名 xyz,pqr,def

xyz将包含在123 build变体中 pqr将包含在456构建变体中 def将包含在789构建变体中

aar模块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')
}

我想根据特定的构建版本包含特定的.aar文件。

此代码提取不正确的aar文件。请建议任何解决方案

3 个答案:

答案 0 :(得分:3)

当我回答我自己的问题时,因为任何人都面临同样的事情,那么就有适当的解决方案。

首先:您不能在库项目中包含3个aars并相应地选择它们到目前为止我使用的是Android Studio 2.2.3

我包含3个aars的方法是。

我在主应用程序命名中创建了3个库模块:

settings.gradle

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

xyzaar目录将包含xyz.aar文件

xyzaar->的build.gradle

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

与其他2辆车相同

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')

由于 P.S:这种方法对我很有用,应用程序不包含不需要的aar文件

答案 1 :(得分:1)

在您的app build.gradle中添加以下内容:

apply plugin: 'com.android.application'

repositories {
    ...
    flatDir {
        dirs 'libs'
    } 
} 
...

将* .aar文件放入此libs目录,并将以下内容添加到您的应用build.gradle:

xyzDebugCompile(name: 'xyz', ext: 'aar')
xyzReleaseCompile(name: 'xyz', ext: 'aar')
xyzReleaseUnsignedCompile(name: 'xyz', ext: 'aar')

pqrDebugCompile(name: 'pqr', ext: 'aar')
pqrReleaseCompile(name: 'pqr', ext: 'aar')
pqrReleaseUnsignedCompile(name: 'pqr', ext: 'aar')

defDebugCompile(name: 'def', ext: 'aar')
defReleaseCompile(name: 'def', ext: 'aar')
defReleaseUnsignedCompile(name: 'def', ext: 'aar')

答案 2 :(得分:0)

要完成答案,如果要为结合了产品风味和构建类型的变体添加依赖项,则必须在配置块中初始化配置名称。 以下示例将 runtimeOnly 依赖项添加到您的“freeDebug”构建变体(使用本地二进制依赖项):

configurations {
    // Initializes a placeholder for the freeDebugRuntimeOnly dependency
    // configuration.
    freeDebugRuntimeOnly {}
}

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