为什么 libGdx 资源位于 Android 子模块中

huangapple go评论75阅读模式
英文:

Why libGdx Asset are in the android submodule

问题

我使用libGDX来创建游戏,一切正常,但在gradle构建中有一些我不太明白的地方。

其中一个问题是为什么所有的资源都在android子模块中,而不是在core子模块中。

Core是负责所有共享代码的模块,但没有将资源放在其中感觉有些奇怪。

这可能是一个简单的问题,但却使整个构建过程更加难以理解。

如果您有解决方法以避免这种情况,我愿意听取建议。我会尝试在我的一侧更改这个行为,但我在gradle配置方面的技能有限。

英文:

I use libGdx for creating a game, everything is working but there is few things i don't understand in the gradle build.

And one of them is why all the assets are in the android subModule and not in the core subModule.

Core is the module responsible for all shared code and it's weird to not have the asset in it.

This can seem an easy question but that make the whole build more difficult to understand.

If you have a solution for avoiding that, i'm open to suggestion. I will try to change this behaviour on my side but i'm not much skilled in gradle configuration

答案1

得分: 4

为什么android项目文件夹中有assets文件夹而core没有?

嗯,因为 libGDX 团队决定在 Libgdx 项目生成器中将其放在那里。你可以以不同的方式设置你的项目,但我会坚持使用 libGDX 提议的项目布局。

需要注意的是,assets 不一定始终要放在 android 项目文件夹中,这取决于你要针对哪些平台。例如,如果你不针对 Android 设备,就不会有 android 文件夹。请随意阅读更多关于LibGDX 项目结构的信息。

我如何更改 assets 文件夹的位置?

你需要编辑以下文件:

  • ios/robovm.xml
...
<resources>
  <resource>
    <!-- 修改此路径 -->
    <directory>../android/assets</directory>
  </resource>
  ...
</resources>
  • desktop/build.gradle
// ...
sourceSets.main.resources.srcDirs = ["../android/assets"]
// ...
project.ext.assetsDir = new File("../android/assets")
// ...
  • android/build.gradle
// ...
sourceSets {
    main {
        // ...
        assets.srcDirs = ['assets']
    // ...
}
// ...

这个列表可能不完整,我认为网上也没有一个详尽的列表。或许这是反对改变你的 assets 文件夹位置的另一个原因。

英文:

Why are assets in android project folder and not in core?

Well, because the libGDX team decided for the Libgdx Project Generator to put it there. You can setup your project in a different way but I would stick to the project layout proposed by libGDX.

Note that assets need not always be in android project folder, depending on what platforms you are targeting. For example, if you don't target Android devices, there will be no android folder. Feel free to read more on the Structure of LibGDX projects.

How do I change the location of the assets folder?

You have to edit:

  • ios/robovm.xml

<!-- language: xml -->
...
<resources>
<resource>
<!-- change this path -->
<directory>../android/assets</directory>
</resource>
...

  • desktop/build.gradle

<!-- language: gradle -->

// ...
sourceSets.main.resources.srcDirs = [&quot;../android/assets&quot;]
// ...
project.ext.assetsDir = new File(&quot;../android/assets&quot;)
// ...
  • android/build.gradle

<!-- language: gradle -->

// ...
sourceSets {
    main {
        // ...
        assets.srcDirs = [&#39;assets&#39;]
// ...

This list is probably not complete and I don't think there's an exhaustive one online. Perhaps that's another reason against changing the location of your assets folder.

答案2

得分: 0

我认为在Android项目中拥有 assets 目录的主要原因只是惯例。根据这个答案,原因是...

> Android 项目在其根目录下确实有一个名为 "assets" 的特定文件夹。该文件夹的名称必须严格命名为 "assets",这是Android项目强制执行的。因此,最好的做法是在其他项目中也以相同的方式布置您的文件夹层次结构,这样您就不必为各个平台进行更改。

但我认为应该有可能将 assets 目录移动到核心项目中,因为如果您创建一个没有 Android 部分的项目,它也会放置在核心项目中。

要将 assets 目录移动到核心项目,您只需要调整位于子项目内部的 build.gradle 文件,并更改以下行:

//sourceSets.main.resources.srcDirs = [&quot;../android/assets&quot;]
sourceSets.main.resources.srcDirs = [&quot;../core/assets&quot;]

//project.ext.assetsDir = new File(&quot;../android/assets&quot;);
project.ext.assetsDir = new File(&quot;../core/assets&quot;);

在我的项目中,此文件位于桌面子项目中,但其他 build.gradle 文件中可能也有类似的内容。

在Eclipse中运行您的项目时,可能需要在移动后重新链接资产文件夹。请参阅此答案以获取有关如何执行此操作的详细说明。

英文:

I think the main reason to have the assets directory in the android project is just convention. According to this answer the reason is that...

> Android projects do have a specific folder in its root named assets. The folder has to be strictly named "assets" as enforced by the Android project. Therefore, it would be a better idea to lay your folder hierarchy the same way in other projects as well so that you won't have to make changes for individual platforms.

But I think it should be possible to move the assets directory to the core project, because it is also placed in the core proejct, if you create a project without an Android part.

To move the assets directory to the core project you just need to adapt your build.gradle file, that is placed inside the sub-projects and change the following lines:

//sourceSets.main.resources.srcDirs = [&quot;../android/assets&quot;]
sourceSets.main.resources.srcDirs = [&quot;../core/assets&quot;]

//project.ext.assetsDir = new File(&quot;../android/assets&quot;);
project.ext.assetsDir = new File(&quot;../core/assets&quot;);

In my project this file was located in the desktop sub-project, but there might be some in other build.gradle files too.

To run your project in eclipse it might be neccessary to re-link the asset folder after moving it. See this answer for a detailed description on how to do this.

huangapple
  • 本文由 发表于 2020年9月6日 15:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63761911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定