task removeUnusedFiles {
    doLast {
        def report = file('lint-report.txt')
        def logFile = file('remove-log.txt')
        logFile.write("")
        if (!report.exists()) {
            logFile.append("No report found at " + report.absolutePath + "\n")
            return
        }
        
        int deletedCount = 0
        long deletedSize = 0L

        report.eachLine { line ->
            def matcher = line =~ /^(.*?):\s*(?:\d+:\s*)?Warning: The resource (R\.(drawable|layout|anim|raw|color)\.[a-zA-Z0-9_]+) appears to be unused/
            if (matcher.find()) {
                def filePath = matcher.group(1)
                
                if (!filePath.contains("\\values\\") && !filePath.contains("/values/")) {
                    def f = file(filePath)
                    if (f.exists()) {
                        deletedSize += f.length()
                        if (f.delete()) {
                            deletedCount++
                            logFile.append("Deleted: " + f.name + "\n")
                        } else {
                            logFile.append("Failed to delete: " + f.name + "\n")
                        }
                    } else {
                        logFile.append("File not found: " + filePath + "\n")
                    }
                }
            }
        }
        logFile.append("Total files deleted: " + deletedCount + "\n")
        logFile.append("Total space saved: " + (deletedSize / 1024) + " KB\n")
    }
}
