apply plugin: 'com.android.application'
/*
Properties props = new Properties()
props.load(new FileInputStream(file("../local.properties")))
*/

android {
    /*signingConfigs {
        release {
            keyAlias props['KEY_ALIAS']
            keyPassword props['KEY_PASSWORD']
            storeFile file(props['KEYSTORE_FILE'])
            storePassword props['KEYSTORE_PASSWORD']
        }
    }*/

    compileSdkVersion 30
    defaultConfig {
        applicationId "site.duqian.floatwindow"
        minSdkVersion 18
        targetSdkVersion 30 //如果targetSDKF大于25，第二个无需权限的demo无法显示，视情况选择实现方式
        versionCode 1
        versionName "1.0"
        //vectorDrawables.useSupportLibrary = true
    }

    /*去掉我的的签名配置*/
    buildTypes {
        release {
            //minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //signingConfig signingConfigs.release
        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //signingConfig signingConfigs.release
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    project.afterEvaluate {
        boolean hasAssembleRelease = true
        /*List<Task> tasksList = tasks.toList()
        for (Task task : tasksList) {
            String taskName = task.getName()
            if ("assembleRelease" == taskName) {
                hasAssembleRelease = true
            }*/
        if (hasAssembleRelease) {
            tasks.getByName("assembleRelease").doLast {
                System.out.println("assembleRelease finish ..................")
                handleApkBuildDone()
            }
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'

    //申请悬浮窗权限
    //implementation 'com.linchaolong.android:floatingpermissioncompat:1.0'
}

// 拷贝app打包后的文件
String handleApkBuildDone() {
    Properties properties = getAppProperties()
    String channel = properties.getProperty("duqian").toLowerCase()
    String build_date = properties.getProperty("BUILD_DATE")
    System.out.println("dq channel: " + channel + " build_date: " + build_date)

    if (channel == null || channel == "") {
        channel = "google"
    }

    String outApkName = "nono-${android.defaultConfig.versionName}-" + channel + "-" + build_date + ".apk"
    String outMapping = "mapping-${android.defaultConfig.versionName}.txt"
    File apkFile = new File("../release-app/", outApkName)
    apkFile.mkdirs()
    if (apkFile.exists()) {
        apkFile.delete()
    }

    copy {
        from 'build/outputs/apk/release/app-release.apk'
        into '../release-app/'
        rename("app-release.apk", "float-window-app-release.apk")//outApkName
    }
    copy {
        from 'build/outputs/mapping/release/mapping.txt'
        into '../release-app/'
        rename("mapping.txt", outMapping)
    }
}


static Properties getAppProperties() {
    FileInputStream fis
    try {
        fis = new FileInputStream("app/src/main/assets/duqian.properties")
        Properties prop = new Properties()
        prop.load(fis)
        return prop
    } catch (Exception e) {
    } finally {
        if (fis != null) {
            fis.close()
        }
    }
    return null
}