> ## Content Index
> Fetch the complete content index at: https://sgpublic.xyz/llms.txt
> Use this file to discover other available public pages before exploring further.

# Symbol is declared in module X which does not export package X
- URL: https://sgpublic.xyz/p/2023/03/6aac9cb1810a4800015335f5/
- Published: 2023-03-17T03:41:12.000Z
- Updated: 2023-03-17T03:41:12.000Z
- Author: Haven Madray
- Tags: 软件开发, Java/Kotlin, #wp-post

前排提示，此方法适用于 Gradle + Kotlin 的项目。

在写 [sgpublic/XXPreference: Manage SharedPreference with annotation! (github.com)](https://github.com/sgpublic/XXPreference?ref=sgpublic.xyz) 项目的时候，我需要使用 `java.compiler` 中的内容修改 Java 语法树，于是乎便遇到了以下报错：

```
Symbol is declared in module 'jdk.compiler' which does not export package 'com.sun.tools.javac.tree'
```

网上找到的方法是在 `gradle.propertise` 中添加 `-add-exports`：

```
org.gradle.jvmargs=
 --add-exports=com.sun.tools.javac.tree=ALL-UNNAMED
 --add-exports=...
```

这个方法有效，但仅适用于纯 Java 项目，而对于 Kotlin 项目，解决方法如为，在每个使用了未导出类的 `.kt` 文件的开头添加内容：

```
@file:Suppress("JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE")
```

从而无需手动添加 `--add-opens` 或 `--add-exports` 即可成功编译。

但编译后用户使用的时候还是会报这个错误，此时可参考 `Lombok` 的解决方案，通过反射强行添加 `exports`：[LombokProcesser.java](https://github.com/projectlombok/lombok/blob/9806e5cca4b449159ad0509dafde81951b8a8523/src/core/lombok/javac/apt/LombokProcessor.java?ref=sgpublic.xyz)