Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - selective module compilation (reuse jar otherwise)

I am using a lot of modules in my project (local and online ones, >20 most of the time) and I can say that normally non of them must be checked nor recompiled. I could include them all as .jar files which would speed up build time but I would prefer following:

  • setup something where I define that gradle should build a .jar for all my modules and reuse them
  • if needed I just disable this setup and build my project (perfectly a clean build would do that)
  • I want to be able to edit my modules in the project, that's why I don't want to include them directly as .jar files.

I want faster build times but I do not want to have to build the .jar files and add them manually to my project.

Any ideas on how and if this is possible? Can I realise that via some setup or via a gradle task or similar?

like image 749
prom85 Avatar asked Nov 03 '16 06:11

prom85


2 Answers

Let me think about verification libraries, which is exist in .jar and which we should download. In other case, you can provide several type of product flavors. And after this just select Build Flavors for your work.

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')

} 

| UPDATE |

So after some workaround, I see that Gradle gathering libraries into some cache, where i can see source. But I still searching to way of correct verification libraries with project configuration.

For now i wrote script for gathering Files from Gradle cache location. And copying them into new location, where we can use build flavors,. This works really quick (less that 7 seconds for 200 libraries), but still need improvements (see above).

if I do not have time, for the next update, please, fill free to extend solution. Thanks for understanding.

// 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
        }
    }
}
like image 105
GensaGames Avatar answered Nov 16 '22 01:11

GensaGames


I think the Continuous build feature introduced from Gradle 2.5 is trying to fix the problem what you meet, though it's not exactly what you need. Pass -t or --continuous to gradlew command to use it, following is the description for -t option.

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

And quoted from the document:

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

See: https://docs.gradle.org/current/userguide/continuous_build.html

like image 40
xfdai Avatar answered Nov 16 '22 01:11

xfdai