Gradle大杂烩

45 阅读1分钟

Gradle中对gradle.properties文件的操作

在Gradle构建中,提供了 Properties 对象来支持gradle.properties文件的操作,其文件本质上也是<KEY,VALUE>键值对的处理。使用方法如下说明:


gradle.settingsEvaluated { Settings setting ->
    file("${setting.rootDir}").eachDir { dir ->
        if (dir.name.equals("sc_sdk")) { 
            dir.eachDir { subdir -> 
                if (subdir.name.equals("sc_sdk_user")) {
                    updateConfig(file("${setting.rootDir}/${dir.name}/${subdir.name}/gradle.properties"), "true")
                }
            }
        }
    }
}



def updateConfig(File file, String sourceModeSwitch) {
    if (file.exists()) {
        def props = new Properties()
        file.withInputStream {
            props.load(it) //加载进Properties中
        }
        String sourceModeSwitchArg = props.getProperty("sourceModeSwitch")
        if (sourceModeSwitch.equals(sourceModeSwitchArg)) {
            return
        }
        props.setProperty("sourceModeSwitch", sourceModeSwitch)
        props.store(file.newDataOutputStream(), "some comment") //将对象写入到文件中
    }
}

Gradle中文件解压操作

def unZipFiles(String zipPath, String descPath) {
    println("开始解压。。。")
    File zipFile = new File(zipPath)
    File descFile = new File(descPath)
    if (!descFile.exists()) {
        descFile.mkdirs()
    }
    ZipFile zip = new ZipFile(zipFile)
    Enumeration enumeration = zip.entries()
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = enumeration.nextElement()
        String zipEntryName = zipEntry.getName();
        InputStream is = zip.getInputStream(zipEntry)
        println("---999--- 文件名称 $zipEntryName")
        String outPath = descPath + zipEntryName
        File destFile = new File(outPath)
        boolean isDirectory = zipEntry.isDirectory()
        boolean isExists = descFile.exists()
        println("---999--- 文件路径 $outPath + is 文件夹 $isDirectory"  + "  是否存在  $isExists")
        if (zipEntry.isDirectory()) {
            if (!destFile.exists()) {
                destFile.mkdirs()
            }
            println("---999--- 文件夹 contine")
            continue
        }

        FileOutputStream os = new FileOutputStream(outPath)
        byte[] buff = new byte[2048]
        int size = 0
        while ((size = is.read(buff, 0, buff.length)) != -1) {
            os.write(buff, 0, size)
        }
        os.flush()
        os.close()
        is.close()
    }
    println("解压完毕。。。")
}

Gradle中资源下载并保持本地操作

def loadRemoteEngineRes(String filePath, String Url, String descFileName) {
    println("远程请求地址:$Url")
    URL url = new URL(Url)
    HttpURLConnection httpURLConnection = url.openConnection()
    httpURLConnection.setRequestMethod("GET")
    httpURLConnection.setDoInput(true)
    httpURLConnection.setDoOutput(true)
    httpURLConnection.setUseCaches(false)
    httpURLConnection.setInstanceFollowRedirects(true)
    httpURLConnection.setConnectTimeout(100000)
    println("开始下载。。。")
    httpURLConnection.connect()
    int code = httpURLConnection.getResponseCode()
    if (code == 200) {
        InputStream inputStream = httpURLConnection.getInputStream()
        BufferedInputStream bis = new BufferedInputStream(inputStream)
        FileOutputStream fos = new FileOutputStream(filePath + descFileName)
        BufferedOutputStream bos = new BufferedOutputStream(fos)
        byte[] buf = new byte[4096]
        int length = bis.read(buf)
        while (length != -1) {
            bos.write(buf, 0, length)
            length = bis.read(buf)
        }
        bos.close()
        bis.close()
        httpURLConnection.disconnect()
        println("下载完毕。。。")
    }
}