2014-07-14 21 views
45

我有一個項目,其中有幾個模塊,其中一個是名爲(很差)的sdk的Android庫。當我構建項目時,它輸出一個名爲sdk.aar的AAR。如何設置Gradle的AAR輸出名稱

我一直無法在Android或Gradle文檔中找到任何可以更改AAR輸出名稱的東西。我希望它有一個像Jar任務那樣的basename +版本號,但是我不知道如何對AAR執行相同的操作,因爲它的所有配置似乎都隱藏在android.library插件中。

在此階段重命名模塊並不是一個選項,它仍然不會將版本號添加到最終的AAR。

如何更改Gradle中由com.android.library生成的AAR的名稱?

搖籃解決方案

defaultConfig { 
    minSdkVersion 9 
    targetSdkVersion 19 
    versionCode 4 
    versionName '1.3' 
    testFunctionalTest true 
    project.archivesBaseName = "Project name" 
    project.version = android.defaultConfig.versionName 
} 

回答

74

如在下面評論,另一個答覆中提到,原來這裏的答案不搖籃工作3+。每docs,類似於下面的東西應該工作:

使用變API來操縱變型輸出與 新插件打破。它仍然可以用於簡單的任務,例如在編譯時間改變APK 名,如下圖所示:

// If you use each() to iterate through the variant objects, 
// you need to start using all(). That's because each() iterates 
// through only the objects that already exist during configuration time— 
// but those object don't exist at configuration time with the new model. 
// However, all() adapts to the new model by picking up object as they are 
// added during execution. 
android.applicationVariants.all { variant -> 
    variant.outputs.all { 
     outputFileName = "${variant.name}-${variant.versionName}.apk" 
    } 
} 

OLD答:

我無法得到archivesBaseName &版本工作爲我w/Android Studio 0.8.13/Gradle 2.1。雖然我可以在我的defaultConfig中設置archivesBaseName和version,但它似乎不會影響輸出名稱。最後,添加以下libraryVariants塊到我的Android {}範圍終於爲我工作:

libraryVariants.all { variant -> 
    variant.outputs.each { output -> 
     def outputFile = output.outputFile 
     if (outputFile != null && outputFile.name.endsWith('.aar')) { 
      def fileName = "${archivesBaseName}-${version}.aar" 
      output.outputFile = new File(outputFile.parent, fileName) 
     } 
    } 
} 
+0

不爲我工作,因爲它說,有沒有這樣的財產libraryVariants – Helmisek

+1

@Helmisek嘗試用'android.libraryVariants'代替 –

+2

不再有效,與搖籃4.1:HTTPS:/ /developer.android.com/studio/build/gradle-插件-3-0-0遷移。html#variant_output – Keab42

9

除了QIX這裏回答您可以通過定期通過這種方法添加多個輸出路徑的信息字符串以及:

libraryVariants.all { variant -> 
     variant.outputs.each { output -> 
      def outputFile = output.outputFile 
      if (outputFile != null && outputFile.name.endsWith('.aar')) { 
       def fileName = "${archivesBaseName}-${version}.aar" 
       output.outputFile = new File(outputFile.parent, fileName) 
       output.outputFile = new File("/home/pepperonas/IdeaProjects/Libraries/Base/testapp/libs", fileName) 
      } 
     } 
    } 

(Upvotes屬於qix-由於可讀性,我只是寫了這個答案)。

10

與積累插件1.5.0,現在可以使用archivesBaseName在defaultConfig

+6

這適用於我:'setProperty(「archivesBaseName」,「$ {archivesBaseName} - $ versionName」)''build':gradle:1.5.0',添加到'defaultConfig {}'塊 – Frouo

7

對於Android Studio中3與搖籃4和Android插件搖籃3.0.0你必須改變的答案qix以下幾點:

libraryVariants.all { variant -> 
    variant.outputs.all { output -> 
     if (outputFile != null && outputFileName.endsWith('.aar')) { 
      outputFileName = "${archivesBaseName}-${version}.aar" 
     } 
    } 
} 
+0

Where do我們把這個? –

+0

在你的app/build.gradle裏面android {}。或者你可以把它放在你的app/build.gradle中,但是你必須將它命名爲android.libraryVariants.all {} – devz