apply plugin: 'com.android.application'

android {
    compileSdkVersion 33
    buildToolsVersion '26.0.1'


    defaultConfig {
        applicationId "com.excelliance.lbui"
        minSdkVersion 8
        targetSdkVersion 26
        versionCode 280402
        versionName "28.04.02"

        manifestPlaceholders = [
                "screenOrientation": 'landscape' //横屏，竖屏修改portrait
        ]
    }

    signingConfigs {
        MySignConfig {
            storeFile file('lbui.jks')
            keyAlias 'lbuikey'
            keyPassword 'lbuikey'
            storePassword 'lbuikey'
            v2SigningEnabled false
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    buildTypes {
        debug {
            minifyEnabled false
            signingConfig signingConfigs.MySignConfig
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.MySignConfig
            if (rootProject.name == 'lbsdk') {
                writeVersionInfo()
            }
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if ('release' == variant.buildType.name) {
                output.outputFileName =  "lbui.jar"
            }else{
                output.outputFileName = "dontUseItOnlyForTest.apk"
            }
        }
    }

}
def writeVersionInfo() {
    project.mkdir('src/main/assets/')
    project.file('src/main/assets/lbuilogid').withPrintWriter { os->
        os.println(releaseTime())
        os.println("V"+android.defaultConfig.versionName)
        os.println(getGitVersion())
    }
}
def releaseTime() {
    return new Date().format("yyyyMMdd HHmm")
}

def getGitDir() {
    // 从配置文件中读取 git.dir 目录
    def properties = new Properties()
    def local = new File(rootDir, "local.properties")
    if (!local.isFile()) {
        return null
    }
    properties.load(new FileInputStream(local))
    def gitDir = properties.getProperty("git.dir")
    return gitDir
}

def getGitVersion() {
    def gitDir = getGitDir()
    def cmd = 'git'
    if (null != gitDir) {
        cmd = new File(gitDir, cmd)
    }

    def version = String.format('%s rev-parse HEAD', cmd).execute().text.trim()
    if (null == version || "" == version) {
        // 兼容部分 mac 环境下无法获取 git 版本信息
        List<String> commands = new ArrayList<>()
        commands.add(cmd)
        commands.add(String.format("--git-dir=%s", new File(rootDir, ".git")))
        commands.add('rev-parse')
        commands.add('HEAD')
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(commands)
        Process p = builder.command().execute()
        p.waitFor()
        version = p.text.trim()
    }
    return version
}

dependencies {
//    implementation project(':lblibrary') //测试的时候可以用
//    implementation fileTree(include: ['*.jar'], dir: 'libs')
    debugImplementation fileTree(include: ['*.jar'], dir: 'libs')
    compileOnly fileTree(include: ['*.jar'], dir: 'libs')
}

task buildUI() {
    doLast {
        copy {
            from "${buildDir}/outputs/apk/release/lbui.jar"
            into project(':lebian').file('src/main/assets/')
        }
    }
}

buildUI.dependsOn(build)
