Android Studio - Java 资源文件夹是什么?

Android Studio - What is the Java Resources Folder?

我知道有两个众所周知的文件夹可以放资源。

首先是/assets文件夹,文档说:

Contains file that should be compiled into an .apk file as-is. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

第二个是/res文件夹,文档说:

Contains application resources, such as drawable files, layout files, and UI string. See Application Resources for more information.

现在我想知道,还有第三个选项可以创建资源文件夹"New > Folder > Java Resources Folder" 对我来说,我看起来像是 IntelliJ 中 Android 插件的一部分,因为它前面有一个 Android 符号 .

知道它的用途吗?我找不到任何关于它的文档。

我的第一个猜测是在您想为 JVM 测试提供资源的情况下使用它。

  • 标准java世界

    资源可以直接嵌入 "your source tree" 并与 Class 的方法 getResource 一起使用(请参阅 java 文档以获得更精确的描述)。

  • 在android世界

    不推荐这种做法(根本不起作用,因为此类资源已从生成的 APK 中删除)。您仍然可以声明 Java 资源文件夹(参见 build.gradle : sourceSets { main { resources.srcDirs = ['src/main/java/yourresourcesfolder'] } } apk 中的结果:

  • 结论

    随着资源的文件夹树被删除,使用 java 的资源文件夹在我看来变得毫无用处(在 android 项目中)。使用 android 的资产文件夹是更好的选择(也可以避免资源名称冲突,但这是另一回事 :D )

这个 post 有点旧,但我想提供一个答案来解释 Android 上的 java 资源目录的一个用例。如果这个文件夹存在于项目的选项中,那是因为可以用它来做一些事情...

为您的 Android 库输入 Service Loader, that helps to connect other services (aka libraries) into your main app, it can be used as a Gateway 以扩展特性的功能,仅公开接口或抽象 类,服务加载器提供实现而不是您的可以直接访问它们的项目。

这是一个example of how it's implemented。因此,总而言之,为了在 Android 应用程序上使用 Service Loader,您必须创建的文件夹 (META-INF/services) 和文件需要位于此 Java Resources Folder 中,否则您的提供商将获胜看不到任何实现。

如果您想查看它,可以在此处看到它在我的示例中工作:https://github.com/difereto-globant/test-library-feature/tree/1.0.9