简体   繁体   English

无法运行 Android 项目:Gradle 项目刷新失败

[英]Unable to Run Android Project: Gradle Project Refresh Failed

I'm running我在跑

OSX 10.9.5
javac 1.8.0_11

I was having a number of issues with Android Studio (it got stuck in a tight loop and stopped responding every time I tried to build a project), so I completely removed it (using the instructions here ) as well as my sdk folder.我在使用 Android Studio 时遇到了许多问题(每次我尝试构建项目时它都会陷入一个紧密的循环并停止响应),所以我完全删除了它(使用此处的说明)以及我的 sdk 文件夹。

I reinstalled Android Studio and then updated (1.0.2 Build #AI135.1653844 built December 18 2014) Installed the recommended SDK packages and images for each of API 19, 20, and 21, as well as the support library.我重新安装了 Android Studio,然后进行了更新(1.0.2 Build #AI135.1653844 建于 2014 年 12 月 18 日)为每个 API 19、20 和 21 以及支持库安装了推荐的 SDK 包和图像

I created a new project with all of the default settings, and was able to run the Hello World project on the Nexus 5 API 21 x86 emulator image that was in my AVD already.我使用所有默认设置创建了一个新项目,并且能够在我的 AVD 中已经存在的 Nexus 5 API 21 x86 模拟器映像上运行 Hello World 项目。

Next, I tried to check out a project that required JDK 7+ from Version Control.接下来,我尝试从 Version Control 中查看一个需要 JDK 7+ 的项目。 Some random Googling found https://github.com/wikimedia/apps-android-wikipedia.git and that also ran in the emulator with no problem.一些随机的谷歌搜索发现https://github.com/wikimedia/apps-android-wikipedia.git并且它也在模拟器中运行没有问题。

Then I imported the project I'm actually trying to build from github (I'm fairly sure it works for other people, but I'd rather not name it) and went through the same process.然后我导入了我实际上试图从 github 构建的项目(我很确定它适用于其他人,但我宁愿不命名)并经历了相同的过程。 First, the Gradle sync failed because build tools 19.1.0 weren't installed.首先,Gradle 同步失败,因为未安装构建工具 19.1.0。 It also didn't start as an Android project: I got a message about it detecting the Android Framework, and needing to be configured (a dialog box with checkboxes for "Android" and "AndroidManifest.xml").它也不是作为 Android 项目开始的:我收到一条关于它检测到 Android 框架并需要配置的消息(一个带有“Android”和“AndroidManifest.xml”复选框的对话框)。 I configured the project, installed the 19.1.0 build tools, and resynced gradle.我配置了项目,安装了 19.1.0 构建工具,并重新同步了 gradle。 The gradle build succeeded, with 9 warnings ('... cannot find annotation method...'). gradle 构建成功,有 9 个警告('...找不到注释方法...')。 I then tried to Run the project, and the gradle build failed with a bunch of然后我尝试运行该项目,并且 gradle 构建失败了

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

Following instructions here I opened File > Project Structure and checked that my JDK location was in fact pointing to 1.8 install (/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home, as /usr/libexec/java_home returns)按照此处的说明我打开文件 > 项目结构并检查我的 JDK 位置实际上指向 1.8 安装(/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home,作为 /usr/libexec/java_home 返回)

In the build.gradle for the module, I added在模块的 build.gradle 中,我添加了

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

inside the android stanza and did a sync.在 android 节中并进行了同步。 It asked me to reload the project, so I did.它要求我重新加载项目,所以我这样做了。

That had the same error, so I went into the toplevel build.那有同样的错误,所以我进入了顶层构建。 gradle, and added gradle,并添加

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

and did another sync, but this failed because the android() DSL method was not found.并进行了另一次同步,但这失败了,因为找不到 android() DSL 方法。 Using the instructions here I updated the gradle classpath from 1.0.0rc4 to just 1.0.0 (the url in the wrapper was already 2.2.1) That failed in the same way, so I took the other suggestion offered by Android Studio, to apply a gradle plugin.使用此处的说明我将 gradle 类路径从 1.0.0rc4 更新为仅 1.0.0(包装器中的 url 已经是 2.2.1)以同样的方式失败了,所以我采用了 Android Studio 提供的其他建议来申请一个gradle插件。 I noticed that the build.gradle for the module has a plugin com.android.application.我注意到模块的 build.gradle 有一个插件 com.android.application。 I apply that plugin to the toplevel build.gradle and sync.我将该插件应用到顶层 build.gradle 并同步。

Then it fails with the super helpful message 'Gradle project refresh failed.然后它失败并显示超级有用的消息“Gradle 项目刷新失败。 Error: A problem occurred configuring root project...' There was no other text, and nothing in the Gradle console.错误:配置根项目时出现问题...' 没有其他文本,Gradle 控制台中也没有任何内容。

More Googling, and I find this which suggests I should try targeting 1.7 instead, so I change the four 1_8 to 1_7.更多的谷歌搜索,我发现表明我应该尝试以 1.7 为目标,所以我将四个 1_8 更改为 1_7。 Again, no luck.再一次,没有运气。 Same problem occurred configuring root project.配置根项目时出现同样的问题。

I'm really out of ideas.我真的没有想法。 Any suggestions?有什么建议?

Put the compileOptions only in the app module build.gradle file.将 compileOptions 仅放在 app 模块 build.gradle 文件中。 If you want to use Java 1.8, use gradle-retrolamdba to convert the bytecode so it runs on Android.如果您想使用 Java 1.8,请使用gradle-retrolamdba转换字节码,使其在 Android 上运行。 If you want to use Java 1.7, make sure the SDK Location under the Project Structure settings is set to a 1.7 jdk.如果要使用 Java 1.7,请确保 Project Structure 设置下的 SDK Location 设置为 1.7 jdk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM