Skip to content

Commit

Permalink
修复windows环境下包路径不匹配导致 package语句没有替换问题 fixed liujingxing#2
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed May 25, 2022
1 parent e633e07 commit 39e92e4
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions plugin/src/main/java/com/xml/guard/model/Mapping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Mapping {
continue
}
//去除目录的直接子文件
val dirPath = rawDir.replace(".", "/")
val dirPath = rawDir.replace(".", File.separator)
val childFiles = locationProject.javaDir(dirPath).listFiles { f ->
val filename = f.name
f.isFile && (filename.endsWith(".java") || filename.endsWith(".kt"))
Expand All @@ -63,7 +63,7 @@ class Mapping {
file.insertImportXxxIfAbsent(manifestPackage)
}
val obfuscatePath = obfuscatePath(rawClassPath)
val relativePath = obfuscatePath.replace(".", "/") + file.name.getSuffix()
val relativePath = obfuscatePath.replace(".", File.separator) + file.name.getSuffix()
val newFile = locationProject.javaDir(relativePath)
if (!newFile.exists()) newFile.parentFile.mkdirs()
newFile.writeText(file.readText())
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/main/java/com/xml/guard/tasks/MoveDirTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class MoveDirTask @Inject constructor(

// 2、开始移动目录
moveFile.forEach { (oldPath, newPath) ->
val oldDir = project.javaDir(oldPath.replace(".", "/"))
val oldDir = project.javaDir(oldPath.replace(".", File.separator))
if (oldPath == manifestPackage) {
//包名目录下的直接子类移动位置,需要重新手动导入R类及BuildConfig类(如果有用到的话)
oldDir.listFiles { f -> !f.isDirectory }?.forEach { file ->
Expand All @@ -53,7 +53,7 @@ open class MoveDirTask @Inject constructor(
}
project.copy {
it.from(oldDir)
it.into(project.javaDir(newPath.replace(".", "/")))
it.into(project.javaDir(newPath.replace(".", File.separator)))
}
project.delete(oldDir)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ open class PackageChangeTask @Inject constructor(
}

//3.对旧包名下的直接子类,检测R类、BuildConfig类是否有用到,有的话,插入import语句
project.javaDir(oldPackage.replace(".", "/"))
project.javaDir(oldPackage.replace(".", File.separator))
.listFiles { f -> !f.isDirectory }
?.forEach { file ->
file.insertImportXxxIfAbsent(newPackage)
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/main/java/com/xml/guard/tasks/XmlClassGuardTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ open class XmlClassGuardTask @Inject constructor(

var replaceText = rawText
when {
rawFile.absolutePath.removeSuffix().endsWith(obfuscatePath.replace(".", "/")) -> {
rawFile.absolutePath.removeSuffix().endsWith(obfuscatePath.replace(".", File.separator)) -> {
//对于自己,替换package语句及类名即可
replaceText = replaceText
.replaceWords("package $rawPackage", "package $obfuscatePackage")
.replaceWords(rawName, obfuscateName)
}
rawFile.parent.endsWith(obfuscatePackage.replace(".", "/")) -> {
rawFile.parent.endsWith(obfuscatePackage.replace(".", File.separator)) -> {
//同一包下的类,原则上替换类名即可,但考虑到会依赖同包下类的内部类,所以也需要替换包名+类名
replaceText = replaceText.replaceWords(rawPath, obfuscatePath) //替换{包名+类名}
.replaceWords(rawName, obfuscateName)
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/java/com/xml/guard/utils/ProjectExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fun Project.isAndroidProject() =

//查找dir所在的Project,dir不存在,返回null
fun Project.findLocationProject(dir: String): Project? {
val packageName = dir.replace(".", "/")
val packageName = dir.replace(".", File.separator)
val absoluteDir = javaDir(packageName)
if (absoluteDir.exists()) {
return this
Expand Down

0 comments on commit 39e92e4

Please sign in to comment.