buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.ajoberstar:gradle-git:0.12+'
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

dependencies {
    implementation project(':ABVJE_UI_Android')
    implementation 'com.android.support:multidex:1.0.3'
}

android {
    compileSdkVersion 26
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 23
        multiDexEnabled true

        // from gradle.properties
        applicationId app_custompackagename
        versionCode app_versioncode as int;
        versionName version_name

        buildConfigField("boolean", "isStoreProduct", "${isStoreProduct}")
        manifestPlaceholders = [HOME_APP: "android.intent.category.DEFAULT"]
    }

    dexOptions {
        preDexLibraries = true
        jumboMode = true
        javaMaxHeapSize "2g"
    }
    signingConfigs {
        release {
            storeFile file(keystore)
            storePassword keystorepassword
            keyAlias keyalias
            keyPassword keyaliaspassword
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['res']
            aidl.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
    lintOptions {
        quiet true
        checkReleaseBuilds false
        abortOnError false
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'values/attrs.xml'
        exclude 'values/dimens.xml'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
    }

    flavorDimensions "default" // OK
    productFlavors {

        if (isStoreProduct.toBoolean()) {
            armv7 {
                versionCode defaultConfig.versionCode
                ndk {
                    abiFilters "armeabi-v7a", "x86", ""
                }
            }

            armv8 {
                versionCode defaultConfig.versionCode + 1
                ndk {
                    abiFilters "arm64-v8a", ""
                }
            }

            x86 {
                versionCode defaultConfig.versionCode + 2
                ndk {
                    abiFilters "x86", ""
                }
            }

            x86_64 {
                versionCode defaultConfig.versionCode + 3
                ndk {
                    abiFilters "x86_64", ""
                }
            }

            // 容量が大きいため、コメントアウト
//            x86_armv7 {
//                ndk {
//                    abiFilters "x86", "armeabi-v7a", ""
//                }
//            }
        } else {
            armv7 {
                versionCode defaultConfig.versionCode
                ndk {
                    abiFilters "armeabi-v7a", "x86", ""
                }
            }
            armv8 {
                versionCode defaultConfig.versionCode + 1
                ndk {
                    abiFilters "arm64-v8a", ""
                }
            }
        }
    }
}

import org.ajoberstar.grgit.*
task gitInfo << {
    description 'update githash.txt with current hash'
    def grgit = Grgit.open(project.file('..'))
    def gitfile=file('assets/githash.txt')
    gitfile.write grgit.head().id
}
preBuild.dependsOn gitInfo

task convertToPackage << {
    description 'convert to package for google-services.json'
    def appModuleRootFolder = '.'
    def googleServicesJson = 'google-services.json'
    copy {
        from "fcm"
        include "$googleServicesJson"
        into "$appModuleRootFolder"
        expand PACKAGE_NAME : app_custompackagename
    }
}
preBuild.dependsOn convertToPackage

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
        options.compilerArgs << "-Xlint:unchecked" <<  "-Xlint:deprecation"
    }
}