Android Studio Gradle 找不到方法 compile()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20311659/
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 Gradle Could not find method compile()
提问by Marc Rasmussen
The past two days i have tried to find a solution to this problem but with no luck.
过去两天我试图找到解决这个问题的方法,但没有运气。
I am trying to include GSON
lib. Into my android project.
我正在尝试包含GSON
lib。进入我的android项目。
Here is a picture of my folder structure:
这是我的文件夹结构的图片:
Now in my build.gradle i have the following:
现在在我的 build.gradle 中,我有以下内容:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
dependencies {
compile files('libs/gson-2.2.4.jar')
}
i have also right clicked gson-2.2.4.jar and added it as a lib.
我还右键单击了 gson-2.2.4.jar 并将其添加为 lib。
When i compile i get the following error:
当我编译时,出现以下错误:
Gradle: A problem occurred evaluating root project 'notebox-android'.
> Could not find method compile() for arguments [directory 'libs'] on root project 'notebox-android'.
Can anyone help me with this?
谁能帮我这个?
please tell me if you need more information!
如果您需要更多信息,请告诉我!
回答by VJ Vélan Solutions
The answer to this question might help you to import your gson library correctly and avoid any compile/link issues: Android Studio - Importing external Library/Jar. Another related link: Android Studio: Add jar as library?with accepted answer. Both answers say the same thing. gradlew clean
after you add/import the jar file and edit the build.gradle file.
这个问题的答案可能会帮助您正确导入 gson 库并避免任何编译/链接问题:Android Studio - Importing external Library/Jar。另一个相关链接:Android Studio:将 jar 添加为库?有公认的答案。两个答案都说同样的话。gradlew clean
添加/导入 jar 文件并编辑 build.gradle 文件后。
HTH.
哈。
Edit: There are two build.gradle files. You should be editing the build.gradle that's under your project folder and not the one under your project-root folder.
编辑:有两个 build.gradle 文件。您应该编辑项目文件夹下的 build.gradle,而不是项目根文件夹下的那个。
回答by Bruce Long
I had this same problem when I added the v7 appcompat repository.
当我添加 v7 appcompat 存储库时,我遇到了同样的问题。
Initially I thought that Android Studio 0.5.4 had not updated the build.gradle file, but per Marc's warning above I found that I was editing the wrong build.gradle file: I edited the one in the project folder, and not the one in the app folder. Theone in the app folder is automatically updated with the compile() directive for the appcompat repository and support library when they are added with the SDK Manager.
最初我认为 Android Studio 0.5.4 没有更新 build.gradle 文件,但是根据上面 Marc 的警告,我发现我正在编辑错误的 build.gradle 文件:我在项目文件夹中编辑了一个,而不是在应用程序文件夹。当 appcompat 存储库和支持库与 SDK 管理器一起添加时,app 文件夹中的一个会自动更新为 compile() 指令。
So the problem turned out to be that I had neglected to adjust the minSdkVersion and targetSdkVersion settings in the build.gradle file in the app folder:
所以问题原来是我忽略了调整 app 文件夹中 build.gradle 文件中的 minSdkVersion 和 targetSdkVersion 设置:
I had to change it from:
我不得不改变它:
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
to:
到:
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
versionCode 1
versionName "1.0"
}
To match the back compatibility with Android 2.1 (i.e. v7 appcompat).
匹配与Android 2.1(即v7 appcompat)的后向兼容性。
Another tip (if it even qualifies as one):
另一个提示(如果它甚至符合条件):
I am adding v2.1 backwards compatible support for the action bar, and it turns out that in 0.5.4 (which I doubt is different to 0.5.3 with respect to this issue) you need to import ActionBarActivity into the main activity .java class file:
我正在为操作栏添加 v2.1 向后兼容支持,结果证明在 0.5.4 中(我怀疑在此问题上与 0.5.3 不同)您需要将 ActionBarActivity 导入主活动 .java类文件:
import android.support.v7.app.ActionBarActivity;
and not:
并不是:
import android.support.v7.app.ActionBar; //Android Studio 0.5.4 IDE lists this as unused.
as the beginner project at http://developer.android.com/training/basics/actionbar/setting-up.htmlindicates.
正如http://developer.android.com/training/basics/actionbar/setting-up.html 上的初学者项目所示。
The build worked when I made the above two adjustments.
当我进行上述两项调整时,构建工作。