Gradle 项目 编码GBK的不可映射字符

当从其他项目复制文件到Gradle项目时,可能会遇到GBK编码的不可映射字符问题。在Android Studio中将文件编码更改为UTF-8,或者在顶级`build.gradle`文件中添加配置可能无法解决问题。有效的解决方案是在`build.gradle`中添加特定代码片段,以确保正确处理编码。注意,使用javadoc时也需要相应设置,避免出现错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gradle 项目 编码GBK的不可映射字符

出现这种问题、一般是从别的项目中直接copy过来的文件、

如果是用AndroidStudio 去更改file encoding 为UTF-8

还有道友 在build.gradle  最外层添加代码、

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}


如果仍旧无效、在build.gradle添加如下代码是可行的


task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}

task androidJavadocsJar(type: Jar) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}


artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}


不要使用 javadocs等、也是会出现此错误


javadoc  添加这个选项是为了使它更容易从常规的makefile

Java文件生成javadocs的任务


Generates code documentation using the javadoc tool.
The source directory will be recursively scanned for Java source files to process but only those matching the inclusion rules, and not matching the exclusions rules will be passed to the javadoc tool. This allows wildcards to be used to choose between package names, reducing verbosity and management costs over time. This task, however, has no notion of "changed" files, unlike the javac task. This means all packages will be processed each time this task is run. In general, however, this task is used much less frequently.
NOTE: since javadoc calls System.exit(), javadoc cannot be run inside the same VM as Apache Ant without breaking functionality. For this reason, this task always forks the VM. This overhead is not significant since javadoc is normally a heavy application and will be called infrequently.
NOTE: the packagelist attribute allows you to specify the list of packages to document outside of the Ant file. It's a much better practice to include everything inside the build.xml file. This option was added in order to make it easier to migrate from regular makefiles, where you would use this option of javadoc. The packages listed in packagelist are not checked, so the task performs even if some packages are missing or broken. Use this option if you wish to convert from an existing makefile. Once things are running you should then switch to the regular notation.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值