Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据android构建变体包含不同的.aar_Android_Gradle_Android Gradle Plugin_Build.gradle - Fatal编程技术网

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

如何根据android构建变体包含不同的.aar,android,gradle,android-gradle-plugin,build.gradle,Android,Gradle,Android Gradle Plugin,Build.gradle,我有3个.aar文件,它们包含在一个模块中,aar模块包含在一个应用程序模块中 目前我有3个构建变体,对于特定的构建变体,我希望包括特定的aar 让我们假设: 我有3个AAR xyz,pqr,def 将包含在123构建变体中的xyz pqr将包含在456版本中 789版本变型中包含def aar模块构建。渐变 File xyzFile= file("xyz.aar") File pqrFile = file("pqr.aar") File defFile = file("def.aar")

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

目前我有3个构建变体,对于特定的构建变体,我希望包括特定的aar

让我们假设: 我有3个AAR xyz,pqr,def

将包含在123构建变体中的xyz pqr将包含在456版本中 789版本变型中包含def

aar模块构建。渐变

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 )
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')
}
应用程序项目构建。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 )
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文件。请在您的应用程序构建中建议任何解决方案。请添加以下内容:

apply plugin: 'com.android.application'

repositories {
    ...
    flatDir {
        dirs 'libs'
    } 
} 
...
将*.aar文件放入此libs目录,并将以下内容添加到app 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')

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

首先:您不能在一个库项目中包含3个AAR,并且从现在起,我使用的是Android Studio 2.2.3

我加入3个AAR的方法是

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

格雷德尔酒店

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

xyzaar->build.gradle

configurations.maybeCreate("default")
artifacts.add("default", file('xyz.aar'))
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')
与其他2辆车相同

应用程序->build.gradle

configurations.maybeCreate("default")
artifacts.add("default", file('xyz.aar'))
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')
谢谢
注意:这种方法对我来说非常有效,并且应用程序不包含不需要的aar文件。要完成回答,如果您想为结合了产品风格和构建类型的变体添加依赖项,那么您必须在配置块中初始化配置名称。 以下示例将runtimeOnly依赖项添加到“freeDebug”构建变量(使用本地二进制依赖项):


但我想3 aar将始终包含在应用程序源代码中。用这个解决方案?不。只包括一个aar。原因是“xyz”、“pqr”和“def”是您的构建风格,“debug”、“release”和“releaseUnsigned”是您的构建变体。如果运行“/gradlew assembly”,将输出九个apk;每个构建变体/风味组合一个。每个apk将只包含一个aar(适用于该构建变体/风格的aar)。为了更好地解释它:如果运行“/gradlew assembleXyzDebugCompile”,它将为该(九个)构建变量/风格生成apk,并且该apk将仅使用一个xyz.aarThank@Jason.进行编译。。我用3个不同的AAR创建了3个独立的模块,并相应地包含在flavor中“首先:你不能在一个库项目中包含3个AAR并相应地选择它们”与3.X.X中的内容相同:(我想我可以不创建单独的模块就这样做,但事实并非如此