flatcc 24.3.25

Build-time convenience utilities for flatbuffers
Documentation
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
  compileSdk 33

  defaultConfig {
    applicationId "com.flatbuffers.app"
    minSdkVersion 26
    targetSdkVersion 33
    versionCode 1
    versionName "1.0"

    compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
    }

    sourceSets {
      main {
        java {
          srcDir '../../java/src/main/java/'
        }
      }
    }

    ndk {
      abiFilters 'arm64-v8a', 'armeabi-v7a'
    }
    
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
      cmake {
        arguments "-DFLATBUFFERS_SRC=${rootProject.projectDir}/.."
      }
    }
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }

  externalNativeBuild {
    cmake {
      path "src/main/cpp/CMakeLists.txt"
    }
  }

  task generateFbsCpp(type: Exec) {
    def inputDir = file("$projectDir/src/main/fbs")
    def outputCppDir = file("$projectDir/src/main/cpp/generated/")
    def fbsFiles = layout.files { file(inputDir).listFiles() }.filter { File f -> f.name.endsWith(".fbs") }.toList()
    ignoreExitValue(true)

    standardOutput = new ByteArrayOutputStream()
    errorOutput = new ByteArrayOutputStream()
    def commandLineArgs = ['flatc', '-o', outputCppDir, '--cpp']
    fbsFiles.forEach{
      commandLineArgs.add(it.path)
    }

    commandLine commandLineArgs

    doFirst {
      delete "$outputCppDir/"
      mkdir "$outputCppDir/"
    }

    doLast {
      if (executionResult.get().exitValue != 0) {
        throw new GradleException("flatc failed with: ${executionResult.get().toString()}")
      }
    }
  }

  task generateFbsKotlin(type: Exec) {
    def inputDir = file("$projectDir/src/main/fbs")
    def outputKotlinDir = file("$projectDir/src/main/java/generated/")
    def fbsFiles = layout.files { file(inputDir).listFiles() }.filter { File f -> f.name.endsWith(".fbs") }.toList()
    ignoreExitValue(true)

    standardOutput = new ByteArrayOutputStream()
    errorOutput = new ByteArrayOutputStream()

    setErrorOutput(errorOutput)
    setStandardOutput(standardOutput)

    def commandLineArgs = ['flatc', '-o', outputKotlinDir, '--kotlin']
    fbsFiles.forEach{
      commandLineArgs.add(it.path)
    }
    commandLine commandLineArgs

    doFirst {
      delete "$outputKotlinDir/"
      mkdir "$outputKotlinDir/"
    }
    doLast {
      if (executionResult.get().exitValue != 0) {
        throw new GradleException("flatc failed with: ${executionResult.get().toString()}")
      }
    }
  }

  afterEvaluate {
    tasks.named("preBuild") {
      dependsOn(generateFbsKotlin)
      dependsOn(generateFbsCpp)
    }
  }
  namespace 'com.flatbuffers.app'
}

dependencies {
  implementation fileTree(dir: "libs", include: ["*.jar"])
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'androidx.appcompat:appcompat:1.6.1'

  // If you using java runtime you can add its dependency as the example below
  // implementation 'com.google.flatbuffers:flatbuffers-java:$latest_version'

}