这是我第一次尝试 Android Studio。我安装了 0.8.0 并更新到 0.8.2。创建项目后,我会收到错误消息:
错误:(1, 0) 未找到 ID 为“com.android.application”的插件
C:\Users\Bob\AndroidStudioProjects\HelloAgain6\app\build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.bob.helloagain6"
minSdkVersion 15
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
和 C:\Users\Bob\AndroidStudioProjects\HelloAgain6\build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
build.gradle
被删除了 :),所以我从 git
存储库中复制了它,一切正常
更新答案(2020 年 12 月 2 日)
最新的 Gradle: 6.5
版本检查:
./gradlew -v
如何更新:
设置网址:./gradlew wrapper --gradle-version=6.5 --distribution-type=all
更新:./gradlew 包装
最新的 Android Gradle 插件:4.1.0
如果您将以下代码片段添加到 build.gradle
文件的顶部。 Gradle 将更新构建工具。
buildscript {
repositories {
google() // For Gradle 4.0+
maven { url 'https://maven.google.com' } // For Gradle < 4.0
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
在此处阅读更多信息:https://developer.android.com/studio/build/index.html 以及有关版本兼容性的信息:https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle 和 https://dl.google.com/dl/android/maven2/index.html。
原始答案
我遇到了同样的错误,您需要确保您的 Gradle 版本与您的 Android Gradle 插件兼容。
Gradle 的最新版本是 2.0,但您需要使用 1.12 才能使用 Android Gradle 插件。
如果您错过添加顶级构建文件,就会发生这种情况。
只需将 build.gradle 添加到顶层即可。
它应该看起来像这样
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.xx.y'
}
}
allprojects {
repositories {
mavenCentral()
}
}
classpath 'com.android.tools.build:gradle:3.2.1'
和 classpath 'com.google.gms:google-services:4.0.1'
就我而言,我从 GitHub 下载了项目,但 Gradle 文件丢失了。所以我只是创建了一个成功构建的新项目。然后复制粘贴 Gradle 丢失的文件。重建项目对我有用。
https://i.stack.imgur.com/2xJF1.png
根级文件:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:x.x.x'
}
}
allprojects {
repositories {
jcenter()
}
}
Gradle-wrapper.properties 文件:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-x.x-all.zip
我写这篇文章的目的不是为许多人提供解决方案,而是为一些可能犯简单错误的人写这篇文章,例如为从 SVN 导入项目指定错误的 url。它是为那些家伙准备的:)
当我从 SVN 导入项目时发生这种情况 -> Studio 自动提示打开项目 -> 它要求提供 Gradle 位置 -> D:\Software\Android\gradle-2.5 -> 然后出现错误。
不同 SVN 分支中的同一个项目适用于我在 Studio 中配置的 Gradle 插件和 Gradle。我尝试更改 Android Gradle 插件和 Gradle 以使其在错误的分支上运行,但没有任何成功。
最后,我发现这是我的以下错误:我尝试单独导入特定的应用程序,而不是导入应用程序以及依赖的库项目。我用于导入的 url 最初在末尾有应用程序项目的名称。一旦我删除它,并指定包含应用程序项目及其依赖项目的父目录,一切都很顺利:)
在与此错误消息苦苦挣扎一小时后,我发现了问题:
我不小心将根 build.gradle
重命名为 builde.gradle
中的文件名,因此 Android Studio 不再识别它。
重命名它为 build.gradle
解决了这个问题!
我仍然得到错误
Could not find com.android.tools.build:gradle:3.0.0.
问题:jcenter() 没有所需的库
解决方案:添加 google() 作为 repo
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.0.0"
}
}
在项目级别的 build.gradle 文件中,我已经替换了这一行
classpath 'com.android.tools.build:gradle:3.6.3'
有了这个
classpath 'com.google.gms:google-services:4.3.3'
添加这两行并同步后,一切都变得很好。希望这会对某人有所帮助。
我使用 IntelliJ IDEA 13.1.5 并在 build.gradle
文件中的 dependencies
中更改了 Picasso 和 Retrofit 的版本后遇到了同样的问题。我尝试使用许多解决方案,但没有结果。然后我从远程 git 克隆了我的项目(在更改依赖项版本之前我将它推送到那里)并且它工作了!之后,我刚刚关闭了当前项目并再次将旧项目从 Gradle 文件导入 IntelliJ IDEA,它也可以正常工作!所以,我认为这是 IDEA、Gradle 和 Android 插件交叉的奇怪错误。我希望这些信息对 IDEA 用户或其他任何人都有用。
转到您的成绩文件,您可以在其中看到:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
并将 classpath
更改为:
buildscript {
repositories {
jcenter()
}
dependencies {
// classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle-experimental:0.7.0-alpha1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
gradle-experimental
是“实验性的”。这与问题无关。
其他答案对我不起作用,我猜 ButterKnife 和 3.0.0 alpha5 之间发生了问题。
但是,我发现当我注释任何一个句子时,无论是 BUtterKnife 还是 3.0.0 alpha5,它都可以正常工作。
因此,您应该避免重复或冲突。
供将来参考:对我来说,这个问题完全是由于我没有以管理员身份运行 Android Studio 造成的。我将 Windows 上的快捷方式配置为始终以管理员身份运行,但在重新安装 Android Studio 后,快捷方式被替换,因此它在没有管理员权限的情况下运行。这导致了很多不透明的错误,包括这个问题中的错误。
当我不小心重命名该行时发生了这个问题
apply plugin: 'com.android.application'
文件 app/build.gradle
上的其他名称。因此,我通过将其更改为原来的方式来修复它。
这只是在我使用 Android Studio 1.3.2 时发生的,但是,由于我刚刚创建了项目,我将其删除并再次创建它。似乎Android Studio第一次没有正确创建它,甚至没有像预期的那样创建项目文件夹。
如果您在模块目录中使用 android plugin 1.2.3 运行模块任务,则会出现问题。尝试这个:
../gradlew -b ../build.gradle -c ../settings.gradle :module:xxx
确保您的两个 build.gradle 和 settings.gradle 文件位于 https://developer.android.com/studio/build/index.html 中所述的正确目录中
然后在 Visual Studio 中打开“作为现有项目”
Gradle 对此非常挑剔。
在我的顶级 build.gradle
中进行以下更改以更新到最新版本的 gradle 后,我收到此错误消息:
//classpath 'com.android.tools.build:gradle:2.3.2' old
classpath 'com.android.tools.build:gradle:2.3.3' //new
当我连接到一个敌对的工作场所代理后面时,我愚蠢地做出了改变。代理导致新版本 gradle 的 .jar
文件损坏。这可以通过检查罐子来验证它们是否尺寸异常或是否可以解压缩。
为了解决这个错误,我连接到家里的网络(不在代理后面)并从终端刷新依赖项:
./gradlew --refresh-dependencies
这导致重新下载较新版本的 gradle 并且不再出现错误。
检查拼写,我的是 'com.android.aplication'
如果项目根目录中同时存在 settings.gradle
和 settings.gradle.kts
文件(可能包含相同的模块),也可能会发生这种情况。您应该只有其中一个文件。
我有类似的问题,我做了以下事情来解决它。在应用插件行之前,我提到了 https://developer.android.com/studio/build 并复制/粘贴了以下这些行
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
}
}
[FOR FLUTTER] 转到您的构建 Gradle,然后检查您是否有三个路径
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
我以某种方式删除了 android 工具类路径并得到了错误。
模块应用程序 build.gradle 文件
apply plugin: 'com.android.application'
model{
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig.with {
applicationId "com.iamsafe"
minSdkVersion 15
targetSdkVersion 23
}
buildTypes {
debug {
minifyEnabled = false
useProguard = true
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:23.0.2'
compile files('libs/asmack-android-8-0.8.10.jar')
compile files('libs/commons-io-2.0.1.jar')
compile files('libs/httpclient-osgi-4.2.1-sources.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpmime-4.1.2.jar')
}
project build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.10'
}
}
allprojects {
repositories {
jcenter()
}
}
在这种情况下,请检查以下代码
dependencies {
classpath 'com.android.tools.build:gradle:**1.5.0**'
}
和 gradle-wrapper.properties
在您的项目目录中检查以下 disctributionUrl
:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip
如果这些彼此不兼容,那么您最终会遇到此问题。
对于 com.android.tools.build:gradle:1.5.
,您需要至少 2.8 的版本,但如果您切换到像 com.android.tools.build:gradle:2.1.0
这样的更高版本,则需要将您的 gradle 更新到 2.9 及更高版本,这可以通过将 gradle-wrapper.properties 中的 distributionUrl
更改为2.9或更高如下
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
如果您在 Windows 上工作,则必须由管理员启动 Android Studio 名称。它解决了我的问题
只需确保在创建应用程序时正确放置了 http 和 https 代理
不定期副业成功案例分享