apply plugin: 'com.android.library'

def rootPath = rootProject.getRootDir().getAbsolutePath()

def fullPackage = System.getenv("ANDROID_FULL_PACKAGE")
println("fullpackage = ${fullPackage}")

android {
    ndkVersion '23.2.8568313'

    publishNonDefault true //this line


    flavorDimensions "default"
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        externalNativeBuild {
            cmake {
                arguments '-DANDROID_TOOLCHAIN=clang',
                        "-DFFMPEG_INSTALL_DIR_ANDROID=$rootPath/../../../external/install/ffmpeg/Android/",
                        "-DEXTERN_INSTALL_DIR_ANDROID=$rootPath/../../../external/install/"
                arguments '-DANDROID_STL=c++_static'

                arguments "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            // let gradle pack the shared library into apk
            jniLibs.srcDirs = ['../../../../external/install/ffmpeg/Android']
        }
    }


    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }

    }

    productFlavors {
        corePlayer {
            ndk {
                abiFilters 'armeabi-v7a', 'arm64-v8a'
            }
        }
    }

    packagingOptions {
        if (fullPackage == "false") {
            exclude 'lib/arm64-v8a/libalivcffmpeg.so'
            exclude 'lib/armeabi-v7a/libalivcffmpeg.so'
        } else {

        }
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    if (fullPackage == "false") {
        compileOnly fileTree(dir: 'libs', include: ['*.jar'])
    } else {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    }
}

android {
    lintOptions {
        abortOnError false
    }
}


tasks.whenTaskAdded { task ->
    if (task.name.equals('assembleRelease')) {
        task.doLast {
            copy {
                into rootPath

                from('build/outputs/aar/premierlibrary-corePlayer-release.aar') {
                    into 'releaseLibs/'
                    include "premierlibrary-corePlayer-release.aar"
                    if (fullPackage == "false") {
                        rename("premierlibrary-corePlayer-release.aar", "CicadaPlayer-4.7.0-part.aar")
                    } else {
                        rename("premierlibrary-corePlayer-release.aar", "CicadaPlayer-4.7.0-full.aar")
                    }
                }
            }

        }
    }
}
