Java Android Studio w/gradle:包 r 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18341622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio w/gradle: Package r does not exist
提问by Rob Hawkins
Final Edit:
最终编辑:
This was kind of a long question with several edits, and I wasn't necessarily doing the right thing at each step of the way. For anyone who stumbles on this question and is having trouble converting a project to gradle in Android Studio, I'd suggest checking out this repositorymentioned in question # 16718026and trying to import that and compile and run it. Once you have something working it's easier to see what might be wrong with your own project.
这是一个经过多次编辑的长问题,我不一定在每一步都做正确的事情。对于偶然发现这个问题并且在 Android Studio 中将项目转换为 gradle 时遇到问题的任何人,我建议查看问题 #16718026 中提到的这个存储库并尝试导入它并编译和运行它。一旦你有了一些工作,就更容易看到你自己的项目可能有什么问题。
To summarize, here's what I did to convert a non-Gradle Android Studio project into a working Gradle one, using Android Studio .2.5, which auto installed Gradle 1.6 and the Gradle Plugin .5 for me.
总而言之,这是我使用 Android Studio .2.5 将非 Gradle Android Studio 项目转换为可工作的 Gradle 项目的操作,它为我自动安装了 Gradle 1.6 和 Gradle Plugin .5。
(1) Created a new blank Android Studio project with an icon and an activity. NOTE: I didn't realize this at first because I was learning Android and Gradle at the same time, but Android Studio (in this version anyway) actually sets you up with a multi-project setup, which was confusing for me. Multi-Project means there's another folder above your project in the hierarchy, giving a location to store settings for all projects. AS did notcreate a build.gradle file in the subproject folder (in my case, FrontlineSMS), despite a video guide indicating it would. So I had to create that subproject build.gradle file and also create a very simple settings.gradle (see Update 2below.)
(1) 创建一个新的空白 Android Studio 项目,其中包含一个图标和一个活动。注意:一开始我没有意识到这一点,因为我同时学习 Android 和 Gradle,但是 Android Studio(无论如何在这个版本中)实际上为您设置了多项目设置,这让我感到困惑。多项目意味着在层次结构中的项目上方有另一个文件夹,提供一个位置来存储所有项目的设置。AS也没有,尽管视频指南创建的子项目文件夹中的文件的build.gradle(在我的情况,FrontlineSMS),说明会。因此,我必须创建该子项目 build.gradle 文件并创建一个非常简单的 settings.gradle(请参阅下面的更新 2。)
(2) Copied src/, res/, .git/, lib/, .gitignore, and AndroidManifest.xml to the root of the new project.
(2) 将src/、res/、.git/、lib/、.gitignore、AndroidManifest.xml复制到新建项目的根目录下。
(3) Used git mv
to move these items to the appropriate places according to conventions in the Gradle Plugin User Guide, replacing or removing some existing files or folders in the blank project where necessary.
(3) 用于git mv
根据Gradle Plugin User Guide中的约定将这些项目移动到合适的位置,必要时替换或删除空白项目中的一些现有文件或文件夹。
(4) Created a build.gradle
in my subproject folder and gave it some basic settings (see below or in the question linked above)
(4)build.gradle
在我的子项目文件夹中创建了一个并给了它一些基本设置(见下文或在上面链接的问题中)
(5) Imported the project as described in Update 4
(5) 按照更新 4 中所述导入项目
Original question:
原问题:
I'm a novice Android developer trying to convert an Android Studio project that does not currently use Gradle (and compiles and runs just fine) to one that uses Gradle on OS X. I'm stuck on a Resource error, which I think may have to do with either my project structure, settings in the IDE, settings in the build.gradle files, or some combination of those.
我是一名 Android 新手开发人员,试图将当前不使用 Gradle(并且编译和运行良好)的 Android Studio 项目转换为在 OS X 上使用 Gradle 的项目。我遇到了资源错误,我认为这可能与我的项目结构、IDE 中的设置、build.gradle 文件中的设置或这些的某种组合有关。
The way I went about doing the conversion was to first create a new project in Android Studio, copy over files to appropriate locations, then add some settings in the IDE and build.gradle files. My resulting project structure is,
我进行转换的方法是首先在 Android Studio 中创建一个新项目,将文件复制到适当的位置,然后在 IDE 和 build.gradle 文件中添加一些设置。我得到的项目结构是,
├── frontlinesms-for-android
├── build.gradle
├── gradle
├── gradlew
├── gradle.bat
├── settings.gradle (added in update 2)
├── local.properties
├── lib
└── lib1.jar
└── lib2.jar
└── ...
├── frontlinesms-for-android.iml
├── FrontlineSMS
└── build.gradle
└── src
├── main
│?? └── res
│?? └── AndroidManifest.xml
│?? └── java
│?? └── net
│?? └── frontlinesms
│?? └── android
│?? ├── FrontlineSMS.java
│?? ├── ...
Most notably, maybe, is I put the res/ folder at the same level as java/ and AndroidManifest.xml. I'm trying to follow the conventions mentioned in the Gradle Plugin User Guide so I don't have to customize too much in the build.gradle files.
最值得注意的是,也许我将 res/ 文件夹与 java/ 和 AndroidManifest.xml 放在同一级别。我正在尝试遵循 Gradle 插件用户指南中提到的约定,因此我不必在 build.gradle 文件中进行太多自定义。
I also marked the folder FrontlineSMS/src/main/java/net as source because if I don't then nothing attempts to compile. I created a new build.gradle file in FrontlineSMS/build.gradle because it did not exist when I made the new project, even though an instructional video on the Gradle website said it would.
我还将文件夹 FrontlineSMS/src/main/java/net 标记为源,因为如果我不这样做,则不会尝试编译。我在 FrontlineSMS/build.gradle 中创建了一个新的 build.gradle 文件,因为当我创建新项目时它并不存在,尽管 Gradle 网站上的一个教学视频说它会存在。
In the IDE I also set the dependencies for the external libraries I have in /lib.
在 IDE 中,我还为 /lib 中的外部库设置了依赖项。
When I try to compile via the IDE I get many Package r does not exist
errors, indicating the auto generated R class is not getting generated or recognized. This is defined in the project as,
当我尝试通过 IDE 进行编译时,出现许多Package r does not exist
错误,表明未生成或识别自动生成的 R 类。这在项目中定义为,
import net.frontlinesms.android.R;
in several files. What can I do to make Android Studio / Gradle recognize the resources I have defined under src/main/res?
在几个文件中。我该怎么做才能让 Android Studio/Gradle 识别我在 src/main/res 下定义的资源?
Reading up on others' questions, I notice that some solutions to this error involved marking a "gen/" folder as a source root or "cleaning" the project. I tried creating a gen/ folder in the root directory and marking it as source but that didn't work. And, rebuilding the project just gives me the same errors.
阅读其他人的问题,我注意到此错误的一些解决方案涉及将“gen/”文件夹标记为源根目录或“清理”项目。我尝试在根目录中创建一个 gen/ 文件夹并将其标记为源,但这没有用。而且,重建项目只会给我同样的错误。
My root build.gradle file has the following,
我的根 build.gradle 文件具有以下内容,
dependencies {
compile project(':FrontlineSMS')
}
And the one under FrontlineSMS/build.gradle has,
FrontlineSMS/build.gradle 下的那个有,
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile file('../lib/acra-3.1.1.jar')
compile file('../lib/activation.jar')
compile file('../lib/additionnal.jar')
compile file('../lib/annotations.jar')
compile file('../lib/libGoogleAnalytics.jar')
compile file('../lib/mail.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
When I try to build from the command line using ./gradlew clean
I get this error,
当我尝试使用命令行构建时,出现./gradlew clean
此错误,
(removed error)
In addition to my previous question, I'd appreciate any tips you can lend as to how this project should be structured. This is not meant to be a multi-project setup. Thanks for your help.
除了我之前的问题,我很感激您可以提供有关如何构建该项目的任何提示。这并不意味着是一个多项目设置。谢谢你的帮助。
Update 1
更新 1
I was able to generate an APK from the command line. My main problem there is I was calling gradle clean
from the wrong directory. Calling it from FrontlineSMS worked. I'm guessing Android Studio set me up with a multi-project setup when I just had one project, and I hadn't entered all the settings for that, ie, I hadn't created a settings.gradle file.
我能够从命令行生成一个 APK。我的主要问题是我gradle clean
从错误的目录中调用。从 FrontlineSMS 调用它有效。我猜当我只有一个项目时,Android Studio 为我设置了一个多项目设置,而我没有为此输入所有设置,即我没有创建 settings.gradle 文件。
Update 2
更新 2
Created the following settings.gradle file, which allows me to run gradle clean && gradle build
from the root directory (where settings.gradle resides):
创建了以下 settings.gradle 文件,它允许我gradle clean && gradle build
从根目录(settings.gradle 所在的位置)运行:
include ':FrontlineSMS'
Note: the version of gradle I'm using here is the one installed by homebrew, 1.6. If I use the gradlew wrapper that was installed it says ,
注意:我在这里使用的 gradle 版本是自制软件安装的版本,1.6。如果我使用已安装的 gradlew 包装器,它会说,
Gradle version 1.6 is required. Current version is 1.7
Searching the web for this error seems to indicate that Android doesn't support 1.7 at this point, only 1.6 (and maybe it's always a version or two behind), yet this is the version that was installed by Android Studio when I created a new project!
在网上搜索此错误似乎表明 Android 目前不支持 1.7,仅支持 1.6(也许它总是落后一两个版本),但这是我创建新版本时由 Android Studio 安装的版本项目!
Update 3(edit: this is basically all wrong, don't follow this)
更新 3(编辑:这基本上都是错误的,不要遵循这个)
Trying to build the project from Android Studio yields the same error as above regarding 1.6 being required. I tried reimporting the project, though it said it could not find any source files. I guess this means either (1) I have the wrong version of gradle and need to fix it and maybe (2) I'll need to define source files in the build.gradle file after all, even though I thought I was following the conventions from the guide.
尝试从 Android Studio 构建项目会产生与上面关于需要 1.6 相同的错误。我尝试重新导入该项目,但它说找不到任何源文件。我想这意味着要么 (1) 我的 gradle 版本错误,需要修复它,也许 (2) 毕竟我需要在 build.gradle 文件中定义源文件,即使我认为我正在遵循指南中的约定。
I tried copying gradle-wrapper-1.6.jar
to replace gradle/wrapper/gradle-wrapper.jar
but that just gave a class or method not found error when I ran it via ./gradlew clean
.
我尝试复制gradle-wrapper-1.6.jar
以替换,gradle/wrapper/gradle-wrapper.jar
但是当我通过./gradlew clean
.
Attempted solution: In gradle/wrapper/gradle-wrapper.properties
there is a line distributionUrl=
and I changed this to point to the 1.6 version,
尝试的解决方案:在gradle/wrapper/gradle-wrapper.properties
有一行中distributionUrl=
,我将其更改为指向 1.6 版本,
distributionUrl=http\://services.gradle.org/distributions/gradle-1.6-bin.zip
Then the next time I ran ./gradlew clean
it downloaded a new version of of gradle into my home directory ~/.gradle
(which up until now I didn't realize existed) and now when I type ./gradlew --version
it says it is on 1.6, and everything builds via the command line using ./gradlew clean && ./gradlew build
, resulting in apks. So it seems to work from the command line, but I also want this to work from the IDE
然后下次我运行./gradlew clean
它时,将新版本的 gradle 下载到我的主目录中~/.gradle
(直到现在我还没有意识到存在),现在当我输入./gradlew --version
它时说它在 1.6 上,并且所有内容都通过命令行使用./gradlew clean && ./gradlew build
,导致 apks。所以它似乎从命令行工作,但我也希望它从 IDE 工作
In the IDE it now says
在 IDE 中,它现在说
Project is using an old version of the Android Gradle plug-in. The minimum supported version is 0.5.0
So I update the build.gradle
file to say
所以我更新build.gradle
文件说
classpath 'com.android.tools.build:gradle:0.5'
And reopen the IDE, which tells me,
然后重新打开 IDE,它告诉我,
Could not find com.android.tools.build:gradle:0.5.
Are Android Studio gradle plugin and gradle versions tied together? If so what do I want, gradle plugin .4 + gradle 1.6 or gradle plugin .5 and gradle 1.7? And how do I properly install these?
Android Studio gradle 插件和 gradle 版本是否绑定在一起?如果是这样,我想要什么,gradle plugin .4 + gradle 1.6 或 gradle plugin .5 和 gradle 1.7?我如何正确安装这些?
Update 4
更新 4
I finally got this to compile via both the command line andthe IDE. The trick was to import the project using Gradle after having created the basic Gradle files. Thanks Ethan! During the Import Project process there's a step where you choose "Create project from existing sources" or "Import project from external model". I'd been choosing the former when I should've chosen the latter and picked Gradle. After that I selected "use gradle-wrapper (recommended)" and checked "use auto-import" and clicked Finish. It warned me about a couple things I had to change, such as using the latest gradle plugin, for which I made this change in my build.gradle
file,
我终于通过命令行和IDE编译了它。诀窍是在创建基本 Gradle 文件后使用 Gradle 导入项目。谢谢伊森!在导入项目过程中有一个步骤,您可以选择“从现有源创建项目”或“从外部模型导入项目”。当我应该选择后者并选择 Gradle 时,我一直选择前者。之后,我选择了“使用 gradle-wrapper(推荐)”并选中“使用自动导入”并单击完成。它警告我必须更改的几件事,例如使用最新的 gradle 插件,为此我在我的build.gradle
文件中进行了更改,
classpath 'com.android.tools.build:gradle:0.5.+'
which is suggested in the known issues for the android gradle plugin.
这是在 android gradle 插件的已知问题中建议的。
采纳答案by Ethan
It looks like you are missing your settings.gradle file from the project root directory.
看起来您缺少项目根目录中的 settings.gradle 文件。
You are going to need the settings.gradle file to access the subproject that is located inside your source repo.
您将需要 settings.gradle 文件来访问位于源代码库中的子项目。
When you import the project be sure to select Gradle as the source!
导入项目时一定要选择 Gradle 作为源!
回答by robotoaster
samples (with multi module project and multi lib projects) zipped.
样本(带有多模块项目和多库项目)压缩。
http://tools.android.com/tech-docs/new-build-system/
http://tools.android.com/tech-docs/new-build-system/
scroll to the bottom.
滚动到底部。
regarding
关于
Could not find com.android.tools.build:gradle:0.5.
this jar is not in maven central, it comes with Android Studio 0.2.+ Usually happens if you upgrade Android Studio from pre 0.2 which doesn't have internal m2 repo. Install fresh Android studio and it should work. Same story with support library.
这个 jar 不在 maven 中心,它随 Android Studio 0.2.+ 一起提供。如果您从没有内部 m2 存储库的 0.2 版之前升级 Android Studio,通常会发生这种情况。安装新的 Android Studio,它应该可以工作。与支持库相同的故事。
回答by tmx
Actually a couple of points:
其实有几点:
Go to file -> settings -> gradle and browse to:
转到文件 -> 设置 -> gradle 并浏览到:
select Use local gradle distribution and browse to:
选择使用本地 gradle 发行版并浏览到:
c:\users\.gradle/wrapper/dists/gradle-1.6-bin/72srdo3a5eb3bic159kar72vok/gradle-1.6
c:\users\.gradle/wrapper/dists/gradle-1.6-bin/72srdo3a5eb3bic159kar72vok/gradle-1.6
This works, but you have to clear all the previous stuff out, by going to:
这有效,但您必须通过以下方式清除所有以前的内容:
File -> Invalidate caches / restart
文件 -> 使缓存无效/重新启动
Then you should come back in, it will re-import (might prompt you to again configure gradle if the above wasn't exactly correct i think)
然后你应该回来,它会重新导入(如果我认为上述内容不完全正确,可能会提示你再次配置 gradle)
Now it should work...you don't have to manually build the build.gradle files.
现在它应该可以工作了……您不必手动构建 build.gradle 文件。
the settings.gradle under the top level project actually.
实际上是顶级项目下的 settings.gradle 。
Finally if project structure isn't showing you much (mine only showed me platform settings and didn't have all the other selections in most guides) you can right-click on your top-level project eg HelloWorldProject and then select Module Settings and this might get you where you need.
最后,如果项目结构没有向您展示太多(我的只向我展示了平台设置,并且在大多数指南中没有所有其他选择),您可以右键单击您的顶级项目,例如 HelloWorldProject,然后选择模块设置和这个可能会带你到你需要的地方。
Some screenshots for the above may be helpful - looks like they are changing layouts etc and hiding stuff so the official guides aren't always correct:
上面的一些屏幕截图可能会有所帮助 - 看起来他们正在更改布局等并隐藏内容,因此官方指南并不总是正确的:
Lame can't post you images...
跛脚不能给你发图片...