一键处理 Android Studio3 导入Android Studio2项目

作为一个积极进取的Android开发者,时长需要从github、gitee上clone代码,看一下。毕竟研究别人的代码,是成长最快的方式嘛(PS.有大神带那是另一回事了)。这个Android Studio3升级了好久了,不过呢?github上的好多都是使用的Android Studio2进行上传的。clone下来项目后,别的先不干,需要修改好几个文件,需要一分钟左右吧!

我就想呀,做手机APP都是为别人服务,为啥自己不能研究研究一下,方便自己的开发呢?
最初的设想是:研究gradle,将这几个地方都进行替换掉。稍微看了一下,gradle好难呀?不就是修改几个文件吗?非要使用gradle吗?最近刚学习了kotlin,使用kotlin读写文件,联系一下也很好呀!…
下面上完整的代码:

import java.io.*

/**
 * 对文件中部分字段修改后的,返回正确的整个文件的内容
 */
fun correctContent(filePath: String, replaceKey: String, rightValue: String, decollator: String = " "): List<String> {
    val srcFile: File = File(filePath)
    val contentList = ArrayList<String>()
    for (item in srcFile.readLines()) {
        if (item.contains(replaceKey)) {
            contentList.add(replaceKey + decollator + rightValue)
            continue
        }
        contentList.add(item)
    }
    return contentList

}

/**
 * 这里是进行key的替换 eg. compile -> implemention
 * propertyParent dependencies
 */
fun correctContent(filePath: String, kkMap: Map<String, String>, propertyParent: String): List<String> {
    var reachModifedPalce = false
    val srcFile: File = File(filePath)
    val contentList = ArrayList<String>()
    for (item in srcFile.readLines()) {
        if (!reachModifedPalce && (item.indexOf(propertyParent) >= 0)) {
            reachModifedPalce = true
        }
        if (!reachModifedPalce) {
            contentList.add(item)
            continue
        }
        var rightRowMsg = item
        var keySet = kkMap.keys.iterator()
        while (keySet.hasNext()) {
            var keyStr = keySet.next()
            if (item.contains(keyStr)) {
                val nextChar = item[item.indexOf(keyStr) + keyStr.length]
                //compiler 等 不需要进行拆分
                if ((nextChar >= 'A' && nextChar <= 'Z') || (nextChar >= 'a' && nextChar <= 'z')) {
                    break;
                }
                val srcValStr = item.substring(item.indexOf(keyStr) + keyStr.length)
                rightRowMsg = item.substring(0, item.indexOf(keyStr)) + kkMap[keyStr] + srcValStr
                break
            }
        }
        //ignore }
        contentList.add(rightRowMsg)
    }
    return contentList

}

/**
 * 将正确的文件内容写入源文件
 */
fun writeFile(filePath: String, rightContent: List<String>) {
    val thisFile = File(filePath)
    try {
        if (!thisFile.parentFile.exists()) {
            thisFile.parentFile.mkdirs()
        }
        val fw = FileWriter(filePath, false)
        for (item in rightContent) {
            fw.write(item)
            fw.write("\n")
        }
        fw.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

/**
 * 将调用整体封装一下
 */
fun replaceFileContent(filePath: String, fieldStr: String, rightValStr: String, decollator: String = " ") {
    val rightContent = correctContent(filePath, fieldStr, rightValStr, decollator)
    writeFile(filePath, rightContent)
}

/**
 * 将调用整体封装一下
 */

fun replaceFileContent(filePath: String, kkMap: Map<String, String>, propertyParent: String) {
    val rightContent = correctContent(filePath, kkMap, propertyParent)
    writeFile(filePath, rightContent)
}

fun main(args: Array<String>) {
    //1.replace module build.gradle  classpath property.
    run {
        val filePath: String = "./build.gradle"
        val fieldStr = "classpath"
        val rightValStr = "'com.android.tools.build:gradle:3.2.0'"
        replaceFileContent(filePath, fieldStr, rightValStr)
    }
    //2.replace app build.gradle  classpath property.
    run {
        val filePath: String = "./app/build.gradle"
        val fieldStr = "buildToolsVersion"
        val rightValStr = "\"28.0.3\""
        replaceFileContent(filePath, fieldStr, "$rightValStr")
    }
    //3.replace gradle gradle-wrapper.properties  distributionUrl property.
    run {
        val filePath: String = "./gradle/wrapper/gradle-wrapper.properties"
        val fieldStr = "distributionUrl"
        val rightValStr = "https\\://services.gradle.org/distributions/gradle-4.6-all.zip"
        val decollator = "="
        replaceFileContent(filePath, fieldStr, rightValStr, decollator)
    }
    //4.replace app build.gradle  dependencies .compile -> implementation
    run {
        val filePath: String = "./app/build.gradle"
        val keyMap = HashMap<String, String>()
        keyMap["compile"] = "implementation"  
        keyMap["Compile"] = "Implementation"
        val propertyParent = "dependencies"
        replaceFileContent(filePath, keyMap, propertyParent)
    }
}

这是一个kotlin项目,怎么运行呀?

参考地址: https://blog.csdn.net/liangjiu2009/article/details/72518558

1.下载kotlin编译器,解压将 其中的bin 添加到系统变量PATH;
2.通过kotlin -version 查看一下版本
3.完成一个Hello World.
hello.kt

fun main(args: Array) {
println(“Hello, World!”)
}

$ kotlinc hello.kt -include-runtime -d hello.jar

$ java -jar hello.jar

就这样…

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值