How to generate on the fly Java source code to build with Gradle in Android Studio?

Pol

As part of an Android app that uses the NDK, I need to export some constants from the C/C++ world into Java. For obvious reasons, I'd rather have this automated. I'm using Android Studio with the NDK support.

How can I generate some .java file on the fly from a shell script that would be ran by Gradle whenever the app builds? Ideally this .java file would go in an build intermediaries directory somewhere and wouldn't have to be in the app source directory.

As an example, the shell script would generate this Java source:

public enum Type {
  FOO,
  BAR
}

from this C++ source:

enum class Type {
  FOO,
  BAR
}

For reference here's a simplified version of the Gradle file I'm starting from in Android Studio:

apply plugin: 'com.android.model.application'

model {

    compileOptions.with {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }

    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "..."
            minSdkVersion.apiLevel = 18
            targetSdkVersion.apiLevel = 23
        }

        defaultConfig.multiDexEnabled = true
    }

    android.ndk {
        platformVersion = "18"
        moduleName = "jni"
        ...
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
            ndk.with {
                CFlags.add("-Werror")
                cppFlags.add("-Werror")
            }
        }
    }

    android.productFlavors {
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
    }

}

repositories {
    ...
}

dependencies {
    ...
}

apply plugin: 'com.google.gms.google-services'
JBirdVegas

You could just write the generated file to the $buildDir/generatedJava then add that directory as a source folder. Then make the task javaCompile depend on your task that generates the source. Something like this could be used inside a build.gradle

def outputJavaFile = new File("$buildDir.absolutePath/generatedJava", 'MyEnum.java')
task generateSources(type: Exec) {
    def source = $/
package com.example;
public enum Something {
   One,
   Two
}
/$

    if (!outputJavaFile.parentFile.exists()) {
        outputJavaFile.parentFile.mkdirs()
    }
    outputJavaFile.withWriter {
        it << source
    }
}

compileJava.dependsOn outputJavaFile

sourceSets {
    main {
        java {
            srcDirs 'src/main/java', outputJavaFile.absolutePath
        }
    }
}

// if the goal is to generate the source from a script then just call the script
// inside the Exec closure
task shellScriptToGenerateSources(type: Exec) {
    commandLine 'myShellScript.sh'
}
// then make compileJava depend on the task that runs the script
compileJava.dependsOn shellScriptToGenerateSources

Edit: To apply the same logic to your updated build.gradle file might look something like this

apply plugin: 'com.android.model.application'

// define an output folder for our generated .java files
def GENERATED_JAVA_OUTPUT = "$buildDir/generatedJava"
// if the goal is to generate the source from a script then just call the script
// inside the Exec closure
task shellScriptToGenerateSources(type: Exec) {
    commandLine 'myShellScript.sh'
}
// then make compileJava depend on the task that runs the script
compileJava.dependsOn shellScriptToGenerateSources

model {

    compileOptions.with {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }

    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "..."
            minSdkVersion.apiLevel = 18
            targetSdkVersion.apiLevel = 23
        }

        defaultConfig.multiDexEnabled = true
    }

    android.ndk {
        platformVersion = "18"
        moduleName = "jni"
        ...
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
            ndk.with {
                CFlags.add("-Werror")
                cppFlags.add("-Werror")
            }
        }
    }

    android.productFlavors {
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
    }

    // let android know that our java sources shoudl also consider the generated java for the compiler
    android.sourceSet {
        main {
            java {
                srcDir(GENERATED_JAVA_OUTPUT)
            }
        }
    }
}

repositories {
    ...
}

dependencies {
    ...
}

apply plugin: 'com.google.gms.google-services'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Gradle. How to generate source code before compilation in android app

From Dev

android studio 0.4.2 + gradle + Android Source code

From Dev

Generate code for unit test build in Android Studio

From Dev

Edit code during build of gradle in android studio

From Dev

How to use Gradle to generate JavaDoc in Android Studio?

From Java

How to set -source 1.7 in Android Studio and Gradle

From Dev

android studio gradle build

From Dev

Gradle build in Android Studio

From Dev

How to modify imported source code on-the-fly?

From Dev

Import native android source code to android studio and build

From Dev

Import native android source code to android studio and build

From Dev

How to download Source code from Android Google Source With Gradle files

From Dev

Marked as an error code in Android Studio, but successfully build gradle and run on the device

From Dev

Can't get source code to build in JNI folder in Android Studio

From Dev

Android + Gradle generate code version

From Dev

With Gradle 2.12 (for Java), how can download source files if and only if 'build'?

From Java

Android Studio: how to generate signed apk using Gradle?

From Dev

Android Studio - Gradle incremental build

From Dev

Gradle build failed in Android Studio

From Dev

Android Studio Gradle fails to build

From Dev

Gradle build error in Android Studio

From Dev

Gradle Build problems in Android Studio

From Dev

Error in build gradle in Android studio

From Dev

Android Studio build error with gradle

From Dev

Android Studio / Gradle Build questions

From Dev

Gradle build fails in Android Studio

From Dev

Gradle Build Errors in Android Studio

From Dev

How to Get a PhoneGap Project to Run in Android Studio with Gradle Build System

From Java

How to set up gradle and android studio to do release build?

Related Related

  1. 1

    Gradle. How to generate source code before compilation in android app

  2. 2

    android studio 0.4.2 + gradle + Android Source code

  3. 3

    Generate code for unit test build in Android Studio

  4. 4

    Edit code during build of gradle in android studio

  5. 5

    How to use Gradle to generate JavaDoc in Android Studio?

  6. 6

    How to set -source 1.7 in Android Studio and Gradle

  7. 7

    android studio gradle build

  8. 8

    Gradle build in Android Studio

  9. 9

    How to modify imported source code on-the-fly?

  10. 10

    Import native android source code to android studio and build

  11. 11

    Import native android source code to android studio and build

  12. 12

    How to download Source code from Android Google Source With Gradle files

  13. 13

    Marked as an error code in Android Studio, but successfully build gradle and run on the device

  14. 14

    Can't get source code to build in JNI folder in Android Studio

  15. 15

    Android + Gradle generate code version

  16. 16

    With Gradle 2.12 (for Java), how can download source files if and only if 'build'?

  17. 17

    Android Studio: how to generate signed apk using Gradle?

  18. 18

    Android Studio - Gradle incremental build

  19. 19

    Gradle build failed in Android Studio

  20. 20

    Android Studio Gradle fails to build

  21. 21

    Gradle build error in Android Studio

  22. 22

    Gradle Build problems in Android Studio

  23. 23

    Error in build gradle in Android studio

  24. 24

    Android Studio build error with gradle

  25. 25

    Android Studio / Gradle Build questions

  26. 26

    Gradle build fails in Android Studio

  27. 27

    Gradle Build Errors in Android Studio

  28. 28

    How to Get a PhoneGap Project to Run in Android Studio with Gradle Build System

  29. 29

    How to set up gradle and android studio to do release build?

HotTag

Archive