我有 3 个 .aar 文件,它们包含在一个模块中,而 aar 模块包含在 应用程序 模块中。

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

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

xyz 将包含在 123 构建变体中
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 )

应用程序项目 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 个 aars 并相应地选择它们,因为现在我使用的是 Android Studio 2.2.3

我包含 3 个 aars 的方法是。

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

设置.gradle

include ':xyzaar', ':pqraar', ':defaar',...
xyzaar 目录将包含 xyz.aar 文件

xyzaar-> build.gradle
configurations.maybeCreate("default")
artifacts.add("default", file('xyz.aar'))

与其他 2 辆车相同

应用程序-> 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 文件

10-07 19:41
查看更多