Android Studio中缺少“添加为库”的选项——针对Libgdx中的Universal Tween Engine

13
我正在开发一个使用Universal Tween Engine的Libgdx项目。我已按照此页面https://github.com/libgdx/libgdx/wiki/Universal-Tween-Engine上的所有步骤将Universal Tween Engine库安装到项目中。
完成所有这些步骤后,该项目可以在我的笔记本电脑上构建和运行(Android和桌面均可),来自tween引擎的动画完美地工作。
然而,在我的桌面电脑上,每当我尝试运行桌面应用程序时,都会抛出NoClassDefFoundException,该异常在TweenAccessor类上被抛出,该类是Universal Tween Engine的一部分。应用程序编译正确,我可以通过Ctrl单击它无法找到的类,并打开该类的源代码,因此我知道IDE的某个部分至少能够找到该类。在源代码编辑器中,没有任何Library类的红色下划线错误。有趣的是,在我的桌面电脑上,我可以运行Android应用程序,它不会崩溃,并且动画完美地工作。只有桌面版本不能工作。
在尝试解决这个问题时,我遇到了很多建议切换到“项目”视图,找到jar文件,右键单击它们并选择添加为库。我以前在其他项目中也不得不这样做,它确实对我有用。
但是我的问题是,在我的台式电脑上右键单击时,上下文菜单中缺少添加为库选项:

enter image description here

我已经尝试清理项目。 我甚至完全卸载了Android Studio,并下载了一个新版本并进行了安装。 在执行此操作后仍然会得到相同的结果。
在右键单击jar文件时,“添加为库”选项是否显示取决于什么?
我在我的桌面PC上必须做什么才能正确使用Universal Tween Engine库jar?
编辑:gradle.build的相关部分。
project(":desktop") {
    apply plugin: "java"
    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        compile fileTree(dir: '../libs', include: '*.jar') // This one is not listed but I added anyway
    }
}
//...
project(":android") {
    apply plugin: "android"
    configurations { natives }
    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        compile fileTree(dir: '../libs', include: '*.jar')
    }
}
//...
project(":core") {
    apply plugin: "java"
    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile fileTree(dir: '../libs', include: '*.jar')
    }
}

我的项目结构

project_root_dir/
    android/
    core/
    desktop/
    html/
    ios/
    libs/
        tween-engine-api.jar
        tween-engine-api-sources.jar

2
你的应用程序的build.gradle文件是什么样子的?它是否有类似于“compile fileTree(dir: 'libs', include: ['*.jar'])”这样的行?你也可以通过项目结构(文件->项目结构,点击你的模块,点击依赖项选项卡,点击加号->文件依赖项)来添加依赖项。 - Vinay Dandekar
1
@VinayDandekar 我已经添加了gradle.build文件的相关部分。是的,它们包括那些行。当我查看项目结构和依赖项选项卡时,两个jar文件已经列在“core”和“android”模块下。但是没有列在桌面下。如果我尝试将它们添加到桌面模块,我无法这样做,因为它让我只浏览 桌面目录下的内容,但它们位于根目录中的libs目录中,上一级目录。 - FoamyGuy
1
你已经在/libs/文件夹中包含了所有的jar文件。不需要再添加,因为它已经被添加了。compile fileTree(dir: '../libs', include: '*.jar') - Knossos
4个回答

4
我会把这一行代码作为依赖项添加进去:

我建议只添加这一行代码:

compile files('../libs/tween-engine-api.jar')

上面的那行应该替换这行:

compile fileTree(dir: '../libs', include: '*.jar')

我能想象源代码文件可能会造成一些问题。总的来说,我会避免同时导入多个jar包,而是手动选择它们。


作为替代方法,您是否尝试了自自述文件中第6和第7点提到的方法?

在依赖部分添加以下内容:

compile "aurelienribon:tweenengine:6.3.3"
compile "aurelienribon:tweenengine:6.3.3:sources"

并将以下两个Maven存储库添加到位于项目根目录中的build.gradle文件中:

maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }

我曾经尝试按名称定位jar文件,就像您建议的那样,但这并没有解决问题。但即使在这种情况下它不是问题,我仍打算这样做。 - FoamyGuy

1
我遇到了同样的问题...我是这样解决的:只需将tween.jar文件添加到项目下的libs文件夹中,并将其添加到build.gradle中。
project(":core") {
    apply plugin: "java"


    dependencies {
        compile fileTree(dir: '../libs', include: 'tween-engine-api*.jar')
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
        compile "de.tomgrill.gdxfacebook:gdx-facebook-core:1.1.1"
        compile "de.tomgrill.gdxdialogs:gdx-dialogs-core:1.0.0"
        compile "net.dermetfan.libgdx-utils:libgdx-utils-box2d:0.13.2"
        compile "net.dermetfan.libgdx-utils:libgdx-utils:0.13.2"
        compile "org.robovm:robopods-google-mobile-ads-ios:1.6.0"
        compile "org.robovm:robopods-google-analytics-ios:1.6.0"
    }
}

你确定两台电脑上的Android Studio版本完全一致吗? - laymelek
最初,这两台电脑都安装了相同版本的Android Studio(即1.5版本)。我在桌面电脑上彻底卸载了AS,并下载了最新版本(现在是1.5.1版本),而我的笔记本电脑仍然只有1.5版本。尽管在重新安装之前它们都运行在相同版本,但在桌面电脑上无法正常工作,但在笔记本电脑上可以。 - FoamyGuy

1
如果您真的想看到“添加为库”选项。 请按照以下方式更新build.gradle中的android依赖项(通过删除“.jar”)。
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
    compile project(":core")
    compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
    compile fileTree(dir: '../libs', include: '') //removed '*.jar'
}

}

否则,您可以通过以下方法在不编辑依赖项的情况下添加您的库:
文件->项目结构->依赖项->添加->文件依赖项

我已经解决了这个问题,很快我会发布我所做的事情。我刚刚尝试像你一样更改依赖行 compile fileTree(dir: '../libs', include: ''),但我仍然没有“添加为库”选项。我让它同步 gradle,并尝试重新启动 AS,但上下文菜单中仍然没有“添加为库”选项。 - FoamyGuy
我也尝试使用文件->项目结构->依赖项->添加->文件依赖项。关于这个的问题是它似乎被锁定在模块目录内,不会让我上升一个层次,而项目就是这样构建的。我尝试将libs文件夹的副本放在模块中,并在此菜单中添加它,但没有解决我的问题。要解决这个问题,我实际上必须打开此菜单并删除其中的2个条目。 - FoamyGuy
尝试将所有的“compile fileTree(dir: '../libs', include: ''*.jar")”替换为“compile fileTree(dir: '../libs', include: '"')”。这在我的Android Studio上解决了问题。希望对你有用。 - SachinS

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接