import com.android.build.gradle.internal.tasks.LibraryJniLibsTask

apply plugin: 'com.android.library'

android {
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    compileSdk 33
    ndkVersion '25.2.9519653'
    namespace 'org.adaway.libpcap'

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 33
    }

    externalNativeBuild {
        ndkBuild {
            path 'jni/Android.mk'
        }
    }

    sonarqube {
        skipProject = true
    }
}

/**
 * Task to rename executables from hello_world to libhello_world_exec.so
 * If they look like libraries, they are packaged in the apk and deployed on the device in the lib folder!
 *
 * Help with files: https://docs.gradle.org/current/userguide/working_with_files.html
 */
def renameJniLibAsExecutable = tasks.register('renameJniLibAsExecutable', Copy) {
    def flavor = gradle.startParameter.taskRequests.toString().containsIgnoreCase("debug") ? "debug" : "release"
    def buildFolder = file("${buildDir}/intermediates/ndkBuild/${flavor}/obj/local/")

    dependsOn ":${project.name}:externalNativeBuild${flavor.substring(0, 1).toUpperCase()}${flavor.substring(1)}"

    from(buildFolder) {
        exclude "**/objs"
        exclude "**/objs-debug"
        exclude "**/*.a"
        exclude "**/*.d"
        exclude "**/*.o"
        exclude "**/*.so"
        exclude "**/*.txt"
    }

    into buildFolder

    rename '(.+)', 'lib$1_exec.so'

    eachFile {
        println "Replacing ${it.getPath()} from ${flavor} ndkBuild directory"
    }
}

tasks.withType(LibraryJniLibsTask).configureEach {
    dependsOn renameJniLibAsExecutable
}

