apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'

allprojects {
    repositories {
        mavenLocal()
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

buildscript {

    ext.kotlin_version = '1.5.31'

    repositories {
        mavenLocal()
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

}

def platformVersion = 16

android {

    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

        consumerProguardFiles 'consumer-rules.pro'

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                arguments "-DANDROID_PLATFORM_LEVEL=${platformVersion}",
                        '-DANDROID_TOOLCHAIN=clang',
                        '-DANDROID_STL=c++_shared'
            }
        }
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/kotlin', 'src/main/java']
        }

        // https://stackoverflow.com/questions/26244998/androidmanifest-in-androidtest-directory-being-ignored/27884436#27884436
        // https://stackoverflow.com/questions/26663539/configuring-res-srcdirs-for-androidtest-sourceset
        // https://stackoverflow.com/questions/23643189/android-gradle-src-androidtest-res-layout-mylayout-xml-not-found-in-mypackage-r
        androidTest {
            java.srcDirs = ['src/androidTest/java', 'src/androidTest/kotlin']
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
    //过滤掉libc++_shared.so，不添加到APK中。
    //当APK中已有libc++_shared.so，可以打开这段注释，减少包大小。
//    packagingOptions {
//        exclude 'lib/*/libc++_shared.so'
//    }

    // https://www.jianshu.com/p/681589927f1f
    compileOptions {
        kotlinOptions.freeCompilerArgs += ['-module-name', "GaiaX-Analyze"]
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    lintOptions {
        abortOnError false
    }

    buildTypes {
        debug {
            debuggable true
            ndk {
                abiFilters 'arm64-v8a', 'armeabi-v7a', "x86", "x86_64"
            }
        }
        release {
            debuggable false
            ndk {
                abiFilters 'armeabi-v7a', 'arm64-v8a'
            }
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    ndkVersion '23.1.7779620'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.31@jar"
    implementation 'com.alibaba:fastjson:1.2.26@jar'
    implementation 'androidx.annotation:annotation:1.0.0'

    testImplementation 'junit:junit:4.13.2'

    androidTestImplementation 'androidx.annotation:annotation:1.0.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'

}

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {

                if (project.hasProperty("type")) {
                    if (project.getProperty("type").equals("release")) {
                        from components.release
                    } else {
                        from components.debug
                    }
                } else {
                    from components.debug
                }

                if (project.hasProperty("version")) {
                    version = project.getProperty("version")
                } else {
                    version = '0.0.0'
                }
                if (project.hasProperty("group")) {
                    groupId = project.getProperty("group")
                } else {
                    groupId = 'com.github.alibaba.GaiaX'
                }

                artifactId = 'GaiaX-Analyze'
            }
        }
    }
}
