import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
    buildToolsVersion PROP_BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "com.aries.ttaxx"
        minSdkVersion PROP_MIN_SDK_VERSION
        targetSdkVersion PROP_TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        ndk {
            abiFilters 'armeabi-v7a'
        }


        externalNativeBuild {
            ndkBuild {
                if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                    // skip the NDK Build step if PROP_NDK_MODE is none
                    targets 'cocos2djs'
                    arguments 'NDK_TOOLCHAIN_VERSION=clang'
                    
                    def module_paths = [project.file("${System.getenv("COCOSCREATOR_HOME")}/resources/.editors/Creator/2.4.5/resources/cocos2d-x"),
                                        project.file("${System.getenv("COCOSCREATOR_HOME")}/resources/.editors/Creator/2.4.5/resources/cocos2d-x/cocos"),
                                        project.file("${System.getenv("COCOSCREATOR_HOME")}/resources/.editors/Creator/2.4.5/resources/cocos2d-x/external")]
                    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                        arguments 'NDK_MODULE_PATH=' + module_paths.join(";")
                    }
                    else {
                        arguments 'NDK_MODULE_PATH=' + module_paths.join(':')
                    }

                    arguments '-j' + Runtime.runtime.availableProcessors()
                    abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})
                }
            }
        }
    }

    sourceSets.main {
        java.srcDirs "../src", "src"
        res.srcDirs "../res", 'res'
        jniLibs.srcDirs "../libs", 'libs'
        res.srcDirs += "libs/topon/res" //这边是全路径
        manifest.srcFile "AndroidManifest.xml"
    }

    externalNativeBuild {
        ndkBuild {
            if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                // skip the NDK Build step if PROP_NDK_MODE is none
                path "jni/Android.mk"
            }
        }
    }

    signingConfigs {

       release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                signingConfig signingConfigs.release
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=0'
                }
            }

            //FC:生成BuildConfig文件
            buildConfigField "String", "TF_CHANNEL", "${TF_CHANNEL}"
            buildConfigField "String", "JL_AID", "${JL_AID}"
            buildConfigField "String", "JL_CHANNEL", "${JL_CHANNEL}"
            buildConfigField "String", "KS_AID", "${KS_AID}"
            buildConfigField "String", "KS_ANAME", "${KS_ANAME}"
            buildConfigField "String", "KS_CHANNEL", "${KS_CHANNEL}"
            buildConfigField "String", "UM_AID", "${UM_AID}"
            buildConfigField "String", "UM_CHANNEL", "${UM_CHANNEL}"
            buildConfigField "String", "UM_SECRET", "${UM_SECRET}"
            buildConfigField "String", "WX_APP_ID", "${WX_APP_ID}"
            buildConfigField "String", "TOPON", "${TOPON}"
        }

        debug {
            debuggable true
            jniDebuggable true
            renderscriptDebuggable true
            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }

            //FC:生成BuildConfig文件
            buildConfigField "String", "TF_CHANNEL", "${TF_CHANNEL}"
            buildConfigField "String", "JL_AID", "${JL_AID}"
            buildConfigField "String", "JL_CHANNEL", "${JL_CHANNEL}"
            buildConfigField "String", "KS_AID", "${KS_AID}"
            buildConfigField "String", "KS_ANAME", "${KS_ANAME}"
            buildConfigField "String", "KS_CHANNEL", "${KS_CHANNEL}"
            buildConfigField "String", "UM_AID", "${UM_AID}"
            buildConfigField "String", "UM_CHANNEL", "${UM_CHANNEL}"
            buildConfigField "String", "UM_SECRET", "${UM_SECRET}"
            buildConfigField "String", "WX_APP_ID", "${WX_APP_ID}"
            buildConfigField "String", "TOPON", "${TOPON}"
        }

    }
}

android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/merged_assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        def sourceDir = "${buildDir}/../../../../.."

        copy {
            from "${sourceDir}"
            include "assets/**"
            include "src/**"
            include "jsb-adapter/**"
            into outputDir
        }

        copy {
            from "${sourceDir}/main.js"
            from "${sourceDir}/project.json"
            into outputDir
        }
    }
}

dependencies {
    implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    implementation fileTree(dir: "${System.getenv("COCOSCREATOR_HOME")}/resources/.editors/Creator/2.4.5/resources/cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
    implementation project(':libcocos2dx')

    //微信SDK(登录、分享等)
    api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'

    //聚合SDK
    api 'com.android.support:appcompat-v7:28.0.0' //Gradle中要补充Support v7插件的引入，否则会集成失败
    api fileTree(include: ['*.jar','*.aar'], dir: 'libs/topon')
    api fileTree(include: ['*.jar','*.aar'], dir: 'libs/topon/libs')
    api 'com.android.support:design:28.0.0'//从v5.6.2版本开始，如果有聚合快手的v2.7.2及以上版本需要添加此依赖
    implementation 'com.android.support:multidex:1.0.3' //应对接入后发布报single dex相关错误，defaultConfig中需同步设置multiDexEnabled true

    //OaidSDK
    api fileTree(include: ['*.jar','*.aar'], dir: 'libs/oaid')
}
