Skip to content

AndroidGradlePreprocessing

Igor Maznitsa edited this page Dec 31, 2023 · 13 revisions

To integrate preprocessing steps into an Android project under the Gradle build system, it is essential to incorporate specific prefixes within your build script.

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.igormaznitsa:jcp:7.1.0'
    }
}
apply plugin: 'com.igormaznitsa.jcp'

and postfix

preprocessSettings {
    def targetFolder = 'build/preprocessed/java'
    target = file(targetFolder)
    sources = ['src/main/java']
    android.sourceSets.main.java.srcDirs = [targetFolder]
}
preBuild.dependsOn preprocess

I conducted experiments using an Android project developed with the Android Plug-in within IntelliJ IDEA. I explored and modified the build.gradle file at the App module level, which was generated by the plug-in. Consequently, the entirety of my build.gradle appeared as follows in my particular case:

// download and add the preprocessor plugi-in into class path
buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.igormaznitsa:jcp:7.1.0'
    }
}
apply plugin: 'com.igormaznitsa.jcp'
// end section for preprocessor adding

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "android.it.igormaznitsa.com.jcpandroid"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

// here we should provide parameters for preprocessing
// and add hook to make preprocessing before build 
preprocessSettings {
    def targetFolder = 'build/preprocessed/java'
    target = file(targetFolder)
    sources = ['src/main/java']
    android.sourceSets.main.java.srcDirs = [targetFolder]
}
preBuild.dependsOn preprocess
Clone this wiki locally