clean_project
Clear project directories to prepare for rebuilding by removing unnecessary files and optionally creating backups. Designed for use with Android application reverse engineering via the Apktool MCP Server.
Instructions
Clean a project directory to prepare for rebuilding.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| backup | No | ||
| project_dir | Yes |
Implementation Reference
- apktool_mcp_server.py:1221-1321 (handler)The handler function for the 'clean_project' tool. It cleans build artifacts from an APKTool project directory (build, dist, temp dirs and temp/log files), optionally backing them up first. Validates input path, computes sizes freed, and returns detailed results.@mcp.tool() async def clean_project(project_dir: str, backup: bool = True) -> Dict: """ Clean a project directory to prepare for rebuilding with enhanced backup support. Args: project_dir: Path to the APKTool project directory backup: Whether to create a backup of build directories before cleaning Returns: Dictionary with operation results and cleanup details """ # Input validation path_validation = ValidationUtils.validate_path(project_dir, must_exist=True) if not path_validation["valid"]: return {"success": False, "error": path_validation["error"]} try: dirs_to_clean = ["build", "dist", "temp"] files_to_clean = ["*.tmp", "*.log"] cleaned_dirs = [] cleaned_files = [] backed_up = [] # Clean directories for dir_name in dirs_to_clean: dir_path = os.path.join(project_dir, dir_name) if os.path.exists(dir_path): if backup: # Create backup backup_path = f"{dir_path}_backup_{int(time.time())}" shutil.copytree(dir_path, backup_path) backed_up.append({ "original": dir_path, "backup": backup_path, "type": "directory" }) # Calculate size before removal dir_size = 0 file_count = 0 for root, dirs, files in os.walk(dir_path): for file in files: file_path = os.path.join(root, file) try: dir_size += os.path.getsize(file_path) file_count += 1 except: pass # Remove directory shutil.rmtree(dir_path) cleaned_dirs.append({ "path": dir_path, "size_freed": dir_size, "files_removed": file_count }) # Clean specific files import glob for pattern in files_to_clean: pattern_path = os.path.join(project_dir, pattern) for file_path in glob.glob(pattern_path): if os.path.isfile(file_path): file_size = os.path.getsize(file_path) if backup: backup_path = f"{file_path}.bak.{int(time.time())}" shutil.copy2(file_path, backup_path) backed_up.append({ "original": file_path, "backup": backup_path, "type": "file" }) os.remove(file_path) cleaned_files.append({ "path": file_path, "size": file_size }) total_size_freed = sum(d["size_freed"] for d in cleaned_dirs) + sum(f["size"] for f in cleaned_files) total_files_removed = sum(d["files_removed"] for d in cleaned_dirs) + len(cleaned_files) return { "success": True, "cleaned_directories": cleaned_dirs, "cleaned_files": cleaned_files, "backed_up_items": backed_up, "total_size_freed": total_size_freed, "total_files_removed": total_files_removed, "backup_created": len(backed_up) > 0 } except Exception as e: logger.error(f"Error cleaning project: {str(e)}") return { "success": False, "error": f"Failed to clean project: {str(e)}" }