plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'AndResGuard'
    id 'com.efs.sdk.plugin'
}

apply from: secret
apply from: andresguard

android.defaultConfig {
    secret.buildConfigField.forEach({ k, v ->
        buildConfigField("String", k, "\"${v}\"")
    })
    secret.shieldTextList.forEach({ k, v ->
        buildConfigField("String[]", k, v)
    })
}

android {
    compileSdkVersion build_versions.compile_sdk
    buildToolsVersion build_versions.build_tools

    defaultConfig {
        applicationId "com.skyd.imomoe"
        minSdkVersion build_versions.min_sdk
        targetSdkVersion build_versions.target_sdk
        versionCode version_code
        versionName version_name
        flavorDimensions "versionName"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true

        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

        ndk {
            abiFilters 'armeabi'
        }

        manifestPlaceholders = secret.manifestPlaceholders

        resConfigs "xxxhdpi", "anydpi-v26"
    }

    signingConfigs {
        release {
            keyAlias secret.sign.RELEASE_KEY_ALIAS
            keyPassword secret.sign.RELEASE_KEY_PASSWORD
            storeFile file('../key.jks')
            storePassword secret.sign.RELEASE_STORE_PASSWORD
        }
    }

    productFlavors {
        Github {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "Github"]
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "app_release_${variant.productFlavors[0].name.toLowerCase()}_${variant.versionName}.apk"
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false           // 使用keep.xml，keep住皮肤文件
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            applicationIdSuffix '.debug'    // 一台手机debug release共存
            ndk {
                abiFilters 'armeabi', 'x86', 'x86_64'
            }
        }
        release {
            signingConfig signingConfigs.release    //签名
            minifyEnabled true
            zipAlignEnabled true
            shrinkResources true       // 使用keep.xml，keep住皮肤文件
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            ndk {
                abiFilters 'armeabi'
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    packagingOptions {
        exclude 'META-INF/beans.xml'
        exclude 'META-INF/rxjava.properties'
        exclude 'META-INF/README.md'
        exclude 'META-INF/CHANGES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/eclipse.inf'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/nanohttpd/**'
        exclude 'about_files/LICENSE_CDDL.txt'
        exclude 'DebugProbesKt.bin'
        exclude 'jetty-dir.css'
        exclude 'plugin.properties'
        exclude 'org/seamless/**'
        exclude 'org/eclipse/jetty/**'
        exclude 'org/fourthline/cling/**'
        exclude 'okhttp3/internal/**'
    }

    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation deps.kotlin.kotlin_stdlib
    implementation deps.kotlin.core_ktx
    implementation deps.support.appcompat
    implementation deps.support.material
    implementation deps.support.swiperefreshlayout
    implementation deps.support.constraintlayout
    implementation deps.jsoup.jsoup
    implementation deps.lifecycle.lifecycle_livedata_ktx
    implementation deps.lifecycle.lifecycle_viewmodel_ktx
    implementation deps.lifecycle.lifecycle_runtime_ktx
    implementation deps.support.viewpager2
    implementation deps.okhttp3.okhttp
    implementation deps.shuyu.GSYVideoPlayer
    implementation deps.retrofit2.retrofit
    implementation deps.retrofit2.converter_gson
    implementation deps.getActivity.XXPermissions
    implementation deps.kotlinx.kotlinx_coroutines_android
    implementation deps.material_dialogs.core
    implementation deps.material_dialogs.input
    implementation deps.room.room_runtime
    implementation deps.room.room_ktx
    kapt deps.room.room_compiler
    implementation deps.filedownloader.library
    implementation deps.cling.cling_core
    implementation deps.cling.cling_support
    implementation deps.jetty.jetty_server
    implementation deps.jetty.jetty_servlet
    implementation deps.jetty.jetty_client
    implementation deps.nanohttpd.nanohttpd
    compileOnly files ('libs/cdi-api.jar')      // DLNACastService编译需要javax.enterprise.inject.Alternative类
//    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
    implementation deps.greenrobot.eventbus
    implementation deps.umsdk.common
    implementation deps.umsdk.asms
    implementation deps.umsdk.apm
    implementation deps.umsdk.push
    implementation deps.smart.refresh_layout_kernel
    implementation deps.smart.refresh_header_material
    implementation deps.smart.refresh_footer_ball
    implementation deps.ctiao.DanmakuFlameMaster
    implementation deps.coil_kt.coil
    implementation project(':skin')
    implementation project(':skin_blue')
    implementation project(':skin_dark')
    implementation project(':skin_lemon')
    implementation project(':skin_sweat_soybean')
}

// UM U-APM性能报告
efs {
    //是否对启动过程进程插桩的开关，如果使用自动集成监控则必须开启
    isAutoTrack = true
    //您自定义Application的类名称，必填项，如没有自定义则填写系统Application
    applicationName = "App"
    //您自定义Activity的类名称，必填项，将您所有Activity的类名按如下格式填写
    activityList = [
            "AboutActivity",
            "AnimeDetailActivity",
            "AnimeDownloadActivity",
            "ClassifyActivity",
            "CrashActivity",
            "DlnaActivity",
            "DlnaControlActivity",
            "FavoriteActivity",
            "HistoryActivity",
            "LicenseActivity",
            "MainActivity",
            "MonthAnimeActivity",
            "PlayActivity",
            "RankActivity",
            "SearchActivity",
            "SettingActivity",
            "SimplePlayActivity",
            "DetailPlayerActivity",
            "WebViewActivity",
            "SkinActivity",
            "NoticeActivity"
    ]
}

//AndResGuard资源混淆工具
andResGuard {
    // 使用mappingFile，防止需要换肤的资源id被替换
    mappingFile = file("./resource_mapping.txt")
//    mappingFile = null
    use7zip = true
    useSign = true
    // 打开这个开关，会keep住所有资源的原始路径，只混淆资源的名字
    keepRoot = false
    // 设置这个值，会把arsc name列混淆成相同的名字，减少string常量池的大小
    fixedResName = "arg"
    // 打开这个开关会合并所有哈希值相同的资源，但请不要过度依赖这个功能去除去冗余资源
    mergeDuplicatedRes = true
    whiteList = andresguard.whiteList
    compressFilePattern = [
            "*.png",
            "*.jpg",
            "*.jpeg",
            "*.gif",
            "resources.arsc"
    ]
    sevenzip {
        artifact = 'com.tencent.mm:SevenZip:1.2.20'
        //path = "/usr/local/bin/7za"
    }

    /**
     * 可选： 如果不设置则会默认覆盖assemble输出的apk
     **/
    // finalApkBackupPath = "${project.rootDir}/final.apk"

    /**
     * 可选: 指定v1签名时生成jar文件的摘要算法
     * 默认值为“SHA-1”
     **/
    // digestalg = "SHA-256"
}

//class转jar
//删除之前打出的包，默认将包打在'build/libs/'下
task deleteOldJar(type: Delete) {
    delete 'build/libs/CustomDataSource.jar'
}
//自定义数据源打包为普通的jar包操作
task makeJar(type: Jar) {
    //要打成的包的名字
    baseName 'CustomDataSource'
    //选取要打包的文件夹
    from('build\\tmp\\kotlin-classes\\GithubDebug\\com\\skyd\\imomoe\\model\\impls\\custom')
    //需要跟实际类的包名路径一样
    into('com/skyd/imomoe/model/impls/custom')
    //排除在外的文件
    exclude('BuildConfig.class', 'R.class', 'MainActivity.class', 'TestClass.class')
    //排除以R$开头的文件
    exclude { it.name.startsWith('R$') }
}
//打包~
makeJar.dependsOn(deleteOldJar)