導入舊項目或打開文件時,中文註解顯示為亂碼(如 涓婁笅),編輯區、Logcat 同樣受影響。
文件實際編碼(GBK/GB2312)與 IDE 預期編碼(UTF-8)不一致。
File → Settings → Editor → File Encodings
☑ Global Encoding: UTF-8
☑ Project Encoding: UTF-8
☑ Default encoding for properties files: UTF-8
☑ Transparent native-to-ascii conversion
# Linux/Mac/Git Bash
find . -name "*.java" -exec iconv -f GBK -t UTF-8 {} -o {}.utf8 \; -exec mv {}.utf8 {} \;
# Windows PowerShell
Get-ChildItem -Recurse -Filter *.java | ForEach-Object {
$content = Get-Content $_.FullName -Encoding Default
Set-Content $_.FullName -Value $content -Encoding UTF8
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
// Kotlin DSL
tasks.withType(org.gradle.api.tasks.compile.JavaCompile::class.java) {
options.encoding = "UTF-8"
}
// 在 Java 文件第一行添加
// -*- coding: UTF-8 -*-
unmappable character for encoding UTF-8 警告。一次配置,永久告別亂碼。