apply plugin: 'digital.wup.android-maven-publish'
apply plugin: 'com.android.library'

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath "digital.wup:android-maven-publish:3.6.2"
    }
}

android {
    compileSdkVersion 30

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
        ndk {
            abiFilters = []
            abiFilters.addAll(rootProject.ext.ABI_FILTERS.split(';').collect{it as String})
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['build/imported-lib']
        }
    }
}

// As per: https://github.com/googlesamples/android-ndk/blob/master/hello-libs/app/build.gradle
tasks.whenTaskAdded { task ->
    if (task.name == 'externalNativeBuildRelease') {
        task.dependsOn compileCodec2
    } else if (task.name == 'externalNativeBuildDebug') {
        task.dependsOn compileCodec2
    }
}

task compileCodec2 {
    doFirst {
        project.file("build/codec2_build_linux").mkdirs()
        for(String abi : rootProject.ext.ABI_FILTERS.split(";")) {
            project.file("build/codec2_build_android_" + abi).mkdirs()
            project.file("build/imported-lib/"+abi).mkdirs()
        }
    }

    doLast {
        exec {
            workingDir "$projectDir/build/codec2_build_linux"
            commandLine "/usr/bin/cmake", "$projectDir/src/codec2"
        }

        exec {
            workingDir "$projectDir/build/codec2_build_linux"
            commandLine "/usr/bin/make"
        }


        for(String abi : rootProject.ext.ABI_FILTERS.split(";")) {
            System.out.println("Handle abi " + abi)
            exec {
                workingDir "$projectDir/build/codec2_build_android_" + abi
                commandLine "cmake", "$projectDir/src/codec2",
                        "-DCMAKE_TOOLCHAIN_FILE=" + android.ndkDirectory + "/build/cmake/android.toolchain.cmake",
                        "-DUNITTEST=FALSE",
                        "-DGENERATE_CODEBOOK=$projectDir/build/codec2_build_linux/src/generate_codebook",
                        "-DANDROID_NATIVE_API_LEVEL=23", "-DANDROID_ABI="+abi,
                        "-DANDROID_STL=c++_shared"
            }

            exec {
                workingDir "$projectDir/build/codec2_build_android_" + abi
                commandLine "/usr/bin/cmake", "--build", "."
            }

            copy {
                from "$projectDir/build/codec2_build_android_" + abi +"/src/libcodec2.so"
                into "$projectDir/build/imported-lib/"+abi
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-annotations: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'
}

publishing {
    publications {
        mavenAar(MavenPublication) {
            from components.android

            groupId rootProject.group
            artifactId project.name
            version "${rootProject.version}"
        }
    }

    repositories {
        //Set this in projectdir/extrasettings.gradle
        maven {
            url rootProject.file(rootProject.ext.buildConfigProperties['repo.dir']).getAbsolutePath()
        }
    }
}
