Thursday, December 31, 2015

Configuring JJTree in Android Studio

First, follow the steps described in Configuring JavaCC in Android Studio.
Next, modify the module's build.gradle file as shown below:

android {
    // ...
    sourceSets {
        main {
            java.srcDirs = ['src/main/java',
                            file(project.buildDir.absolutePath + '/generated/jjtree'),
                            file(project.buildDir.absolutePath + '/generated/javacc')]
        }
    }
    applicationVariants.all { variant ->
        variant.javaCompile.dependsOn.add(compileJavacc)
    }
    // ...
}

compileJavacc {
    inputDirectory = file(project.buildDir.absolutePath + '/generated/jjtree')
    dependsOn.add(compileJjtree)
}

Move you *.jj file from src/main/javacc, which is JavaCC default directory, to src/main/jjtree, and change its extension to *.jjt.  When you build your project now, everything should continue to work, including all JavaCC unit tests.  In addition, you'll find new JJTree files under build/generated/jjtree.

You can also setup a task that cleans generated JavaCC and JJTree files, and make compileJavacc task depend on it:

task cleanJjdirs(type: Delete) {
    delete file(project.buildDir.absolutePath + '/generated/jjtree'),
            file(project.buildDir.absolutePath + '/generated/javacc')
}

No comments:

Post a Comment