Skip to content

AndroidGradlePreprocessing

Igor Maznitsa edited this page Mar 23, 2016 · 13 revisions

Today usage of Gradle for Android developments becomes more and more popular so I made some investigation how to inject the Java Comment Preprocessor into such build process (JCP supports Maven and ANT out of the box). Because the preprocessor supports ANT and Gradle has very nice integration with it, then I use ANT part of the preprocessor. At First let's create 'preprocess.gradle' in the root of your Gradle based Android project, just next to the 'build.gradle' file and then we will fill the new created file by the text below

// the folder contains the result
def preprocessRoot = 'preprocessed'
// the folder contains preprocessed sources
def preprocessSrc = preprocessRoot+'/src'
// the folder contains preprocessed resources
def preprocessRes = preprocessRoot+'/res'

// the original root folder path
def originalRoot = 'src/main'
// the original java sources folder
def originalSrc = originalRoot+'/java'
// the original resource folder
def originalRes = originalRoot+'/res'

configurations {
    jcp
}

dependencies{
    jcp('com.igormaznitsa:jcp:6.0.1'){
        exclude group: 'org.apache.maven.*'
        exclude group: 'org.codehaus.*'
    }
}

android {
    sourceSets.main.java.srcDirs = [preprocessSrc]
    sourceSets.main.resources.srcDirs = [preprocessRes]
}

task preprocess << {
    println('--------------------------')

    def jcpjar = file(project.configurations.jcp.find { it.name.startsWith("jcp-") })
    println("Preprocessor JAR : "+jcpjar)
    ant.taskdef(resource: 'com/igormaznitsa/jcp/ant/antlib.xml', classpath: jcpjar)

    println('Preprocessed SRC : '+preprocessSrc)
    ant.preprocess(excluded:"none", processing:'java,xml', source: originalSrc, destination: preprocessSrc){
        global(name:'someTestGlobalVar',value:'its value')
        // cfgFile(file:'./jcpvars.cfg')
    }

    println('Preprocessed RES : '+preprocessRes)
    ant.preprocess(excluded:"none", processing:'java,xml', source: originalRes, destination: preprocessRes){
       // cfgFile(file:'./jcpvars.cfg')
    }

    println('--------------------------')
}

preBuild.dependsOn preprocess

then let's open 'build.gradle' and add one more string to call our created gradle script, for instance place the call just after `apply plugin: 'com.android.application``

apply from: 'preprocess.gradle'

the shown script will pre-process both source and resource folders, the script supports regular standard case when project has only source folder and only resource folder and for more complex cases the script should be adapted.

NB! Sources and Resources are processed by different preprocessor instances!
It means that global variables defined in sources will not be visible in resources! As a workaround you can either define global variables directly for each preprocessor task or share some config file where define global variables, in the script such file called ./jcpvars.cfg is shown in commented strings, if to uncomment them then preprocessor instances for sources and resources will load global variable definitions from the same file before start.

Clone this wiki locally