简体   繁体   English

Android Studio 和 Gradle - 构建失败

[英]Android Studio and Gradle - build fails

I am building a small library project along wit a sample project to illustrate the use.我正在构建一个小型图书馆项目以及一个示例项目来说明用法。 I can't manage to run the sample in Android Studio.我无法在 Android Studio 中运行示例。 I have created the project from scratch.我从头开始创建了这个项目。 I am experienced with Eclipse but it's my first try at Android Studio & Gradle.我对 Eclipse 很有经验,但这是我第一次尝试 Android Studio 和 Gradle。

The error given:给出的错误:

Gradle: Execution failed for task ':demo:dexDebug'. Gradle:任务“:演示:dexDebug”的执行失败。

Running C:\\DevTools\\Android\\android-studio\\sdk\\build-tools\\android-4.2.2\\dx.bat failed.运行 C:\\DevTools\\Android\\android-studio\\sdk\\build-tools\\android-4.2.2\\dx.bat 失败。 See output查看输出

I have the following folder structure:我有以下文件夹结构:

- demo
  - build
  - libs
    - android-support-v4.jar
  - src
    - main
      - java
      - res
  - build.gradle
- library
  - build
  - libs
    - android-support-v4.jar
  - src
    - main
      - java
      - res
  - build.gradle
- build.gradle
- settings.gradle

Build.gradle at project root:项目根目录下的 Build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

Settings.gradle at project root:项目根目录下的 Settings.gradle:

include ':library', ':demo'

Build.gradle for the library module:库模块的 Build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android-library'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Build.gradle for the sample module:示例模块的 Build.gradle:

buildscript {
    repositories {
        mavenCentral() 
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':library')
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Specifying compile files('libs/android-support-v4.jar') means that every library includes support v4.指定compile files('libs/android-support-v4.jar')意味着每个库都包含支持 v4。 What you want to do is just specify that every library depends on it:您要做的只是指定每个库都依赖于它:

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
}

This will allow gradle to detect all dependencies and include this only once.这将允许 gradle 检测所有依赖项并且只包含一次。

Note: You have to first use the SDK Manager and download and install two Maven repositories: "Android Support Repository" and "Google Repository".注意:您必须首先使用 SDK Manager 并下载并安装两个 Maven 存储库:“Android Support Repository”和“Google Repository”。

I found the problem:我发现了问题:

I removed that line from the sample gradle file.我从示例 gradle 文件中删除了该行。

compile files('libs/android-support-v4.jar')

However, I have no idea why this does not work (if I have 2 or 3 external libraries that all depend on the support library, how are we supposed to do, without touching their gradle files?但是,我不知道为什么这不起作用(如果我有 2 或 3 个都依赖于支持库的外部库,我们应该怎么做,而不触及它们的 gradle 文件?

You should navigate to your libs folder in the IDE, right click on the jar and select to add the library to the project, it still needs to establish the dependency even though the jar appears to be there.您应该导航到 IDE 中的 libs 文件夹,右键单击 jar 并选择将库添加到项目中,即使 jar 似乎在那里,它仍然需要建立依赖项。 Also look at your gradle built script to make sure the dependency appears there.还要查看您的 gradle 构建脚本以确保依赖项出现在那里。 If that still doesnt work just run a gradle clean on the project.如果这仍然不起作用,只需在项目上运行 gradle clean 即可。 Intellij documentation will give you more details on what clean does. Intellij 文档将为您提供有关 clean 功能的更多详细信息。 see:看:

stackoverflow gradle build stackoverflow gradle 构建

This error could be encountered while migrating from Groovy to kotlin DSL as well and here are the steps to get rid of it:Groovy迁移到 kotlin DSL时也可能遇到此错误,以下是摆脱它的步骤:

  1. If you are still in the process of migrating please complete the migration of gradle files first, use kts syntax and then sync gradle files.如果您还在迁移中,请先完成gradle文件的迁移,使用kts语法,然后同步gradle文件。

  2. Use this dependency inside your build.gradle(app level):在 build.gradle(app level) 中使用此依赖项:

    implementation("androidx.legacy:legacy-support-v4:1.0.0")

  3. Remove id("kotlin-android-extensions") from plugins block inside build.gradle.kts (app level).从 build.gradle.kts(应用程序级别)内的插件块中删除id("kotlin-android-extensions") )。

That's it!就是这样! 3rd Point solved the issue for me but trying all the points should definitely fix the issue.第 3 点为我解决了这个问题,但尝试所有要点肯定可以解决问题。

在我的案例中替换这一行类路径“com.android.tools.build:gradle:7.0.2”

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

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