2016-11-03 41 views
15

我在我的項目中使用了很多模塊(本地和在線的模塊,大多數情況下> 20),我可以說通常不需要檢查或重新編譯它們。我要把它們全部作爲.jar文件,這將加快構建時間,但我寧願如下:Gradle - 選擇性模塊編譯(重複使用jar另外)

  • 設置的東西,我定義gradle這個應該建立我的所有模塊.jar,如果需要我重新使用它們
  • 只需禁用此設置並構建我的項目(完全乾淨的構建會這樣做)
  • 我希望能夠在項目中編輯我的模塊,這就是爲什麼我不想直接將它們包含爲.jar文件。

我想要更快的構建時間,但我不想構建.jar文件並將它們手動添加到我的項目中。

關於如何以及如何這是可能的任何想法?我可以通過一些設置或通過gradle任務或類似的東西意識到嗎?

+0

難道僅僅是純粹的Java庫,否則你會在Android的庫拉呢? – tynn

+0

主要是android庫... – prom85

+0

所以實際上大多是'.aar'文件? – tynn

回答

6

讓我想想驗證庫,它存在於.jar中,我們應該下載。在其他情況下,您可以提供幾種類型的產品口味。之後,您只需選擇Build Flavors即可。

productFlavors { 
     fastDebug { 
      applicationIdSuffix ".jar" 
     } 
     regularDegub { 
      applicationIdSuffix ".regular" 
     } 
     .... 
     // Other Configuration 
    } 

dependencies { 
    ... 
    // Jar Debug by adding only Jar 
    fastDegubCompile fileTree(dir: 'libs', include: '*.jar') 
    fastDegubCompile 'com.android.support:support-v4:23.1.1' 

    ... 
    // Regular Debug with downloading all libraries 
    // Including only specific from project files 
    regularDegubCompile 'com.squareup.picasso:picasso:2.5.2' 
    regularDegubCompile 'com.android.support:support-v4:23.1.1' 
    regularDegubCompile files('libs/specific1.jar', 'libs/specific2.jar') 

} 

|更新|

因此,在一些解決方法之後,我看到Gradle將庫收集到某個緩存中,在那裏我可以看到源代碼。但我仍然在尋找具有項目配置的正確驗證庫。

現在我寫了從Gradle緩存位置收集文件的腳本。並將它們複製到新的位置,在那裏我們可以使用構建口味。這種方法非常快速(200個庫的時間少於7秒),但仍需要改進(見上文)。

如果我沒有時間,請在下次更新時自由填寫以擴展解決方案。感謝您的理解。

// Task for calling from Gradle Scripts 
// ----------------------------------- 

task gatheringJarFilesTask << { 
    println("Gathering Jars Start...") 
    gatheringJarFiles(gradleCacheLocation, foundedJarsList) 
    println("------------------------------") 
    println("Gathering Jars End! Start copying!") 
    copyFiles(projectJarsLocation, foundedJarsList) 

} 


// Constants, which might be optimized too 
// ----------------------------------- 

def gradleCacheLocation = '/home/kasyangenka/.gradle/caches/modules-2/files-2.1' 
def projectJarsLocation = '/home/kasyangenka/Projects/GradleScriptsTest/app/libs' 
List<String> foundedJarsList = [] 

// Main Script Methods 
// ----------------------------------- 

def gatheringJarFiles(baseDirPath, gatheredList) { 
    new File(baseDirPath).eachFile {file -> 
     println("-> Current file: " + file.getName()) 
     if (file.isDirectory()) { 
      gatheringJarFiles(file.getAbsolutePath(), gatheredList) 
     } 
     def containsLib = (file.getName().contains(".jar") 
       || file.getName().contains(".aar")); 

     if (containsLib) { 
      println("->> Adding Jar file: " + file.getAbsolutePath()) 
      gatheredList.add(file.getAbsolutePath()) 
     } 
    } 
} 

def copyFiles (destiny, List sourceList) { 
    sourceList.each {filePath -> 
     copy { 
      from filePath 
      into destiny 
     } 
    } 
} 
+0

這將意味着,我將不得不手動創建一次瓶子並將它們放到libs文件夾中,對嗎?這是你的建議嗎?然後我可以決定通過'fastDegubCompile'使用jar或者通過'regularDegubCompile'獲取我的依賴的最新版本。 – prom85

+0

@ prom85是的,正如你所描述的那樣。 – GensaGames

+0

我希望能夠通過gradle腳本創建jar direclty,而不是僅僅使用jars ...也許你對此也有想法? – prom85

3

我覺得從搖籃2.5引入了Continuous build功能會嘗試修復你遇到什麼問題,雖然這不是正是你需要的。通過-t--continuousgradlew命令來使用它,以下是-t選項的說明。

 
-t, --continuous  Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. [incubating] 

而且引用自:

 
Gradle's incremental build support ensures that only the tasks that are actually affected by the change are executed. 

參見:https://docs.gradle.org/current/userguide/continuous_build.html

+0

我在想,android studio只支持Gradle 2.14.1,但我錯了。它使用當前的gradle插件2.2.0支持> = 2.14.1。你不知道我可以在android studio中設置gradlew參數嗎?我只知道在哪裏傳遞gradle參數... – prom85

+0

@ prom85你可以從'Preferences-> Build,Execution,Deployment-> Compiler-> Command line Options'定義它。 – xfdai

+0

不是'gradle'命令的標誌而不是'gradlew'命令? – prom85