import com.android.builder.core.DefaultManifestParser

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
    }
}

allprojects {
	repositories {
		jcenter()
		maven {
			url "https://www.jitpack.io"
		}
	}

}

apply plugin: 'com.android.application'

//specify dependens.
dependencies {
    compile 'com.github.wuzhendev:android-wgallery:1.0.0'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.2'
    compile 'com.facebook.fresco:fresco:0.14.1'
    def jarTree = fileTree(dir: 'libs', include: '*.jar')
    compile jarTree

    //可选，用于生成application类
    provided('com.tencent.tinker:tinker-android-anno:1.9.1')
    //tinker的核心库
    compile('com.tencent.tinker:tinker-android-lib:1.9.1')
}

android {
    // 避免本地缺少
    splits {
        abi {
            enable true
            reset()
            include 'armeabi'
            universalApk false
        }
    }

    //in some modules (UpdatePresentAndView.java) we still use DefaultHttpClient.java so we can do rec after that.
    useLibrary 'org.apache.http.legacy'
    compileSdkVersion 23
    buildToolsVersion "25.0.1"
    enforceUniquePackageName = false
    //used to control code quallity. example use can be found here: "http://www.kiodev.com/android-studio-lint-options/"
    lintOptions {
        abortOnError false        // true by default
        checkAllWarnings false
        checkReleaseBuilds false
        ignoreWarnings true       // false by default
        quiet true                // false by default
    }
    //means when compile to dex,some options used
    dexOptions {
        preDexLibraries = false
//        incremental false
        jumboMode = false
        //tmall magic box has max ram 1024m, so we limit it < 1024
//        javaMaxHeapSize "4g"
    }

    //use none-standard source folde.

    sourceSets {
        main {
            assets.srcDirs = ['assets']
            res.srcDirs = ['res']
            aidl.srcDirs = ['src']
            resources.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            jniLibs.srcDirs = ['libs']
            manifest.srcFile 'AndroidManifest.xml'

            java {
                srcDirs = ['src']
            }
        }
        QiuSuo {
            manifest.srcFile 'QiuSuo/AndroidManifest.xml'
            java {
                srcDirs = ['QiuSuo/java']
            }
        }
    }
    //exclude files by name.(main for multi android lib-projects)
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/ASL2.0'
    }
    //signing configs
    signingConfigs {
        //used for some market(eg Haier_market)
        commonConfig {
            storeFile file("./tool_output/wasuSigner.keystore")
            storePassword "hlwjsb"
            keyAlias "wasuSigner.keystore"
            keyPassword "hlwjsb"
        }
        //used for some market(eg Ali_market)
        BS2_0Config {
            storeFile file("./tool_output/BS2_0.keystore")
            storePassword "wasu1234"
            keyAlias "android.keystore"
            keyPassword "wasu1234"
        }
    }

    renameApks()

    //used to set build configs
    buildTypes {
        release {
            zipAlignEnabled true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            //we have multi keystore files for different market,so can ont just use commonConfig...
            //signingConfig signingConfigs.commonConfig
        }
        debug {
            signingConfig signingConfigs.commonConfig
            debuggable true
        }
    }
    //multi channel(in gradle we can use Flavors and variants)
    productFlavors {
        //签名
        def flavorSignBS30 = signingConfigs.commonConfig
        def flavorSignBS20 = signingConfigs.BS2_0Config
        //版本
//        def updateVersionCode = 4020033
//        def updateVersionName = "4.2.0.33"
//        TianShang_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_0308AC001"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_A31 {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC965"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_AiJia {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC001"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_GaoQingYuan {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC003"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_ShaFa {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC002"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_A20 {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC964"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_WuKong_Market {
//            manifestPlaceholders(["CHANNELID": "cid_0305AC003"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_LunTan_Market {
//            manifestPlaceholders(["CHANNELID": "cid_0304AC003"])
//            signingConfig flavorSignBS30
//        }
//        BaiShengTong_QuanJiaHe_Market {
//            manifestPlaceholders(["CHANNELID": "cid_0306AC003"])
//            signingConfig flavorSignBS30
//        }
//        WasuTvCs {
//            manifestPlaceholders(["CHANNELID": "cid_030213003"])
//            signingConfig flavorSignBS30
//        }
//        //这个作废，tvId是错误的
//        TCL_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_030199001"])
//            signingConfig flavorSignBS30
//        }
//        WangXunAndroid_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_0309AC003"])
//            signingConfig flavorSignBS30
//        }
//        Ali_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_2710AC003"])
//            signingConfig flavorSignBS20
//        }
//        Haier_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_030983003"])
//            signingConfig flavorSignBS30
//        }
//        HUAWEI_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_0308AC002"])
//            signingConfig flavorSignBS30
//        }
//        TV17_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_0308AC003"])
//            signingConfig flavorSignBS30
//        }
//        QiuSuo {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC164"])
//            signingConfig flavorSignBS30
//        }
//        FenSiWang {
//            manifestPlaceholders(["CHANNELID": "cid_0301ACBZ1"])
//            signingConfig flavorSignBS30
//        }
//        DuYaTech {
//            manifestPlaceholders(["CHANNELID": "cid_0301AC174"])
//            signingConfig flavorSignBS30
//        }
//        Haier_MSD6A828VX {
//            manifestPlaceholders(["CHANNELID": "cid_030183837"])
//            signingConfig flavorSignBS30
//        }
//        LeTV_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_030172003"])
//            signingConfig flavorSignBS30
//        }
//        TCL_MARKET {
//            manifestPlaceholders(["CHANNELID": "cid_030188001"])
//            signingConfig flavorSignBS30
//        }
        BenQ {
            applicationId "cn.com.wasu.tvsp"
            manifestPlaceholders(["CHANNELID": "cid_030296174"])
            signingConfig flavorSignBS30
        }
//        HaiXin_Vidda3 {
//            manifestPlaceholders(["CHANNELID": "cid_030397032"])
//            signingConfig flavorSignBS30
//        }
        //测试版本渠道，测试WasuTVCS3.0升级4.0使用
//        WasuTV_OnlyTest {
//            manifestPlaceholders(["CHANNELID": "cid_030144444"])
//            signingConfig flavorSignBS30
//        }
        XiaoShuai_MSD928 {
            applicationId "cn.com.wasu.tvsp"
            manifestPlaceholders(["CHANNELID": "cid_03016Q840"])
            signingConfig flavorSignBS30
        }
        Haier_RTD2968 {
            applicationId "cn.com.wasu.main"
            manifestPlaceholders(["CHANNELID": "cid_030383340"])
            signingConfig flavorSignBS30
        }
    }

    clean {
        delete += "output"
    }
    defaultConfig {}
}

//we should write another copyapp task depend on assembleRelease task.(this way we modifyed assemble logic)
build.doLast {
    def today = new Date().format('yyyyMMdd');
    def dir = 'output/' + today

    copy {
        from('build/outputs/apk')
        into(dir)
        include('*.apk')
        exclude("*-unaligned.apk");
        exclude("*-unsigned.apk");
    }
}

//used to modify default apk file name. we can modify apk name later .. first see default name.
def renameApks() {
    def name = "WasuTV";
    def version = readVersion();
    def today = new Date().format('yyyyMMddHHmm');
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def file = output.outputFile
            def buildTypeOfApp = variant.buildType.name
            if (!variant.buildType.name.equalsIgnoreCase("debug")) {
                //+c wangfan for BuildType.java code rec
                def outputName = name + "_" + variant.productFlavors[0].name + "_rl_" + version + "_" + today + ".apk"
//				def outputName = name + "_xxx"+ "_rl_" + version + "_" + today + ".apk"
                output.outputFile = new File(file.parent, outputName)
            } else {
                output.outputFile = new File(file.parent, "dummy");
            }
        }
    }
}

//从Manifest.xml中读取版本号
def readVersion() {
    def manifestParser = new DefaultManifestParser(android.sourceSets.main.manifest.srcFile);
    return manifestParser.getVersionName();
}
