【问题标题】:How to include different .aar depending on the android build variant如何根据 android 构建变体包含不同的 .aar
【发布时间】:2023-04-03 17:37:02
【问题描述】:

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

目前我有 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 文件。请提出任何解决方案

【问题讨论】:

    标签: android gradle android-gradle-plugin build.gradle


    【解决方案1】:

    当我回答自己的问题时,因为任何人都面临同样的问题,所以有适当的解决方案可用。

    首先:您不能在库项目中包含 3 个 aar 并相应地选择它们,因为我现在使用的是 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 文件

    【讨论】:

      【解决方案2】:

      在您的应用 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')
      

      【讨论】:

      • 但我猜 3 aar 将始终包含在应用程序源代码中。用这个解决方案?
      • 不。仅包含一个 aar。原因是“xyz”、“pqr”和“def”是您的构建风格,“debug”、“release”和“releaseUnsigned”是您的构建变体。如果你运行 './gradlew assemble' 你会得到九个 apk 的输出;每个构建变体/风味组合一个。并且每个 apk 将仅包含一个 aar(适合该构建变体/风味的 aar)。为了帮助更好地解释它:如果您运行“./gradlew assembleXyzDebugCompile”,它将为该(九个)构建变体/风味生成 apk,并且该 apk 将仅使用一个 xyz.aar 进行编译
      • 谢谢@Jason.. 我已经创建了一个具有 3 个不同 aar 的 3 个独立模块,并相应地包含在风味中
      • “首先:您不能在库项目中包含 3 个 aar 并相应地选择它们”在 ​​AS 3.X.X 中也是如此 :( 我想我可以在不创建单独模块的情况下做到这一点,但事实并非如此案例
      【解决方案3】:

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

      configurations {
          // Initializes a placeholder for the freeDebugRuntimeOnly dependency
          // configuration.
          freeDebugRuntimeOnly {}
      }
      
      dependencies {
          freeDebugRuntimeOnly fileTree(dir: 'libs', include: ['*.jar'])
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 2015-02-24
        • 1970-01-01
        • 2019-02-19
        • 1970-01-01
        相关资源
        最近更新 更多