2016-12-29 61 views
0

如何包括不同.aar。取决于我有被包含在模块和包括在<strong>应用</strong>模块AAR模块3个.aar文件android的构建变量

目前,我有3个构建变量和特定构建变量我想包括具体的AAR。

允许假设: 我有3个AARS命名 XYZ,PQR,DEF

XYZ被包括在123构建变量 PQR要被包括在DEF被包括在789构建变量

456构建变量

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

回答

1

由于我回答我的问题,因为任何人都面临着同样的事情,那么就可以妥善解决。

首先:不能包括库项目3个AARS并据此选择它们作为我现在使用的是Android 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') 

感谢 PS:这个方法对我的作品非常好,应用不包括不需要的AAR文件

0

在你的应用的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') 
+0

但我想3 AAR将始终包含在应用程序源代码中。用这个解决方案? –

+1

没有。只有一个会被列入。原因是'xyz,'pqr'和'def'是你的构建版本,'debug','release'和'releaseUnsigned'是你的版本变体。如果你运行'./gradlew assemble',你会得到九个apk输出;每个构建变体/风味组合一个。并且每个apk只会包含一个aar(适用于构建变体/风格的那个)。 为了更好地解释它:如果你运行'./gradlew assembleXyzDebugCompile',它将生成一个(9个)构建变种/风味的apk,并且apk将只编译一个xyz.aar –

+0

谢谢@Jason ..我已经创建了3个不同的模块,包含3种不同的模块,并且包含在其中 –