Android gradle 构建在 lint 任务上失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20699147/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:18:23  来源:igfitidea点击:

gradle build fails on lint task

androidgradleandroid-gradle-plugin

提问by Serj Lotutovici

I have a simple android project that I created with Android Studio 0.4.0. I use Gradle 1.9 and Gradle Android Plugin 0.7. Yesterday I've added Jake Wharton's ButterKnife libraryin my gradle build script:

我有一个使用 Android Studio 0.4.0 创建的简单 android 项目。我使用 Gradle 1.9 和 Gradle Android 插件 0.7。昨天我在我的 gradle 构建脚本中添加了 Jake Wharton 的ButterKnife 库

dependencies {
            compile 'com.android.support:support-v4:19.0.0'
            compile 'com.android.support:appcompat-v7:19.0.0'

            // Butterknife
            compile 'com.jakewharton:butterknife:4.0.1'
}

When I run the application from Android Studio, the build runs fine and executes correctly on my devices. But when I try (from the command line) gradle buildthe build fails. Here is a part from my lint report:

当我从 Android Studio 运行应用程序时,构建运行良好并在我的设备上正确执行。但是当我尝试(从命令行)时gradle build,构建失败。这是我的 lint 报告的一部分:

InvalidPackage: Package not included in Android

/home/yami/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife/4.0.1/f43b36925363701633d01adb8e54df7150397a78/butterknife-4.0.1.jar: Invalid package reference in library; not included in Android: javax.annotation.processing. Referenced from butterknife.internal.InjectViewProcessor.
/home/yami/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife/4.0.1/f43b36925363701633d01adb8e54df7150397a78/butterknife-4.0.1.jar: Invalid package reference in library; not included in Android: javax.annotation.processing. Referenced from butterknife.internal.InjectViewProcessor.
/home/yami/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife/4.0.1/f43b36925363701633d01adb8e54df7150397a78/butterknife-4.0.1.jar: Invalid package reference in library; not included in Android: javax.annotation.processing. Referenced from butterknife.internal.InjectViewProcessor.
/home/yami/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife/4.0.1/f43b36925363701633d01adb8e54df7150397a78/butterknife-4.0.1.jar: Invalid package reference in library; not included in Android: javax.annotation.processing. Referenced from butterknife.internal.InjectViewProcessor.
/home/yami/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife/4.0.1/f43b36925363701633d01adb8e54df7150397a78/butterknife-4.0.1.jar: Invalid package reference in library; not included in Android: javax.annotation.processing. Referenced from butterknife.internal.InjectViewProcessor.

Maybe I'm missing something, but not to be able to build the project in the terminal blocks the possibility of CI for Android projects.

也许我遗漏了一些东西,但无法在终端块中构建项目 CI for Android 项目的可能性。

Any help would be great.

任何帮助都会很棒。

回答by Marek Sebera

With 0.7.0there comes extended support for Lint, however, it does not work always properly. (Eg. the butterknife library)

随着0.7.0有自带的扩展支持皮棉,但是,它并不总是正常工作。(例如,黄油刀库)

Solution is to disable aborting build on found lint errors

解决方案是在发现 lint 错误时禁用中止构建

I took the inspiration from https://android.googlesource.com/platform/tools/base/+/e6a5b9c7c1bca4da402de442315b5ff1ada819c7

我的灵感来自 https://android.googlesource.com/platform/tools/base/+/e6a5b9c7c1bca4da402de442315b5ff1ada819c7

(implementation: https://android.googlesource.com/platform/tools/base/+/e6a5b9c7c1bca4da402de442315b5ff1ada819c7/build-system/gradle/src/main/groovy/com/android/build/gradle/internal/model/DefaultAndroidProject.java)

(实现:https: //android.googlesource.com/platform/tools/base/+/e6a5b9c7c1bca4da402de442315b5ff1ada819c7/build-system/gradle/src/main/groovy/com/android/build/gradle/internal/model/DefaultAndroidProject.java)

(discussion: https://plus.google.com/+AndroidDevelopers/posts/ersS6fMLxw1)

(讨论:https: //plus.google.com/+AndroidDevelopers/posts/ersS6fMLxw1

android {
  // your build config
  defaultConfig { ... }
  signingConfigs { ... }
  compileOptions { ... }
  buildTypes { ... }
  // This is important, it will run lint checks but won't abort build
  lintOptions {
      abortOnError false
  }
}


And if you need to disable just particular Lint rule and keep the build failing on others, use this:

如果您只需要禁用特定的 Lint 规则并保持其他人的构建失败,请使用以下命令:

/*
 * Use only 'disable' or only 'enable', those configurations exclude each other
 */
android {
  lintOptions {
    // use this line to check all rules except those listed
    disable 'RuleToDisable', 'SecondRuleToDisable'
    // use this line to check just listed rules
    enable 'FirstRuleToCheck', 'LastRuleToCheck'
  }
}

回答by panwy

if abortOnError falsewill not resolve your problem, you can try this.

如果abortOnError false不能解决你的问题,你可以试试这个。

lintOptions {
    checkReleaseBuilds false
}

回答by Biswajit Karmakar

You can select proper options from here

您可以从这里选择合适的选项

android {
    lintOptions {
        // set to true to turn off analysis progress reporting by lint
        quiet true
        // if true, stop the gradle build if errors are found
        abortOnError false
        // if true, only report errors
        ignoreWarnings true
        // if true, emit full/absolute paths to files with errors (true by default)
        //absolutePaths true
        // if true, check all issues, including those that are off by default
        checkAllWarnings true
        // if true, treat all warnings as errors
        warningsAsErrors true
        // turn off checking the given issue id's
        disable 'TypographyFractions','TypographyQuotes'
        // turn on the given issue id's
        enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
        // check *only* the given issue id's
        check 'NewApi', 'InlinedApi'
        // if true, don't include source code lines in the error output
        noLines true
        // if true, show all locations for an error, do not truncate lists, etc.
        showAll true
        // Fallback lint configuration (default severities, etc.)
        lintConfig file("default-lint.xml")
        // if true, generate a text report of issues (false by default)
        textReport true
        // location to write the output; can be a file or 'stdout'
        textOutput 'stdout'
        // if true, generate an XML report for use by for example Jenkins
        xmlReport false
        // file to write report to (if not specified, defaults to lint-results.xml)
        xmlOutput file("lint-report.xml")
        // if true, generate an HTML report (with issue explanations, sourcecode, etc)
        htmlReport true
        // optional path to report (default will be lint-results.html in the builddir)
        htmlOutput file("lint-report.html")

        // set to true to have all release builds run lint on issues with severity=fatal
        // and abort the build (controlled by abortOnError above) if fatal issues are found
        checkReleaseBuilds true
        // Set the severity of the given issues to fatal (which means they will be
        // checked during release builds (even if the lint target is not included)
        fatal 'NewApi', 'InlineApi'
        // Set the severity of the given issues to error
        error 'Wakelock', 'TextViewEdits'
        // Set the severity of the given issues to warning
        warning 'ResourceAsColor'
        // Set the severity of the given issues to ignore (same as disabling the check)
        ignore 'TypographyQuotes'
    }
}

回答by Asaf Pinhassi

I had some lint errors in Android Studio that occurred only when I generated a signed APK.

我在 Android Studio 中有一些 lint 错误,只有在我生成签名的 APK 时才会发生。

To avoid it, I added the following to build.gradle

为了避免它,我添加了以下内容 build.gradle

android {
    lintOptions {
        checkReleaseBuilds false
    }
}

回答by Muhamet Aljobairi

Add these lines to your build.gradle file:

将这些行添加到您的 build.gradle 文件中:

android { 
  lintOptions { 
    abortOnError false 
  }
}

Then clean your project :D

然后清理你的项目:D

回答by Daniel S.

If you want avoid the "abortInError false" option, take a look at build/lint-results-release-fatal.html file. Here are the erros detected by lint.

如果您想避免“abortInError false”选项,请查看 build/lint-results-release-fatal.html 文件。以下是 lint 检测到的错误。

I hope this can help somebody!

我希望这可以帮助某人!

回答by Allen

In Android Studio v1.2, it tells you how to fix it:

在 Android Studio v1.2 中,它会告诉您如何修复它:

enter image description here

在此处输入图片说明

回答by Dmytro Melnychuk

As for me, it's a bad and quicksolution for your problem :

至于我,这是解决您问题的糟糕而快速的解决方案:

android { 
  lintOptions { 
    abortOnError false 
  }
}

Better solutionis solving problem in your code, because lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization.

更好的解决方案是解决代码中的问题,因为 lint 工具会检查您的 Android 项目源文件中是否存在潜在的错误,并针对正确性、安全性、性能、可用​​性、可访问性和国际化进行了优化改进。

This problem most frequently occurringwhen:

在以下情况下最常出现问题

  • Layout include unresolved symbols or missing some attribute
  • Other structural issues, such as use of deprecated elements or API calls that are not supported by the target API versions, might lead to code failing to run correctly.
  • 布局包括未解析的符号或缺少某些属性
  • 其他结构性问题,例如使用不推荐使用的元素或目标 API 版本不支持的 API 调用,可能会导致代码无法正确运行。

Find your bugs by Inspect Codein Android Studio: Improve Your Code with Lint

Inspect Code在 Android Studio 中查找错误:使用 Lint 改进代码

回答by Cedric Simon

Got same error on AndroidStudio version 0.51

在 AndroidStudio 0.51 版上遇到同样的错误

Build was working fine and suddenly, after only changing the version code value, I got a Lint related build error.

构建工作正常,突然间,仅更改版本代码值后,我收到了与 Lint 相关的构建错误。

Tried to change build.gradle, cleared AndroidStudio cache and restart, but no change.

尝试更改build.gradle,清除 AndroidStudio 缓存并重新启动,但没有更改。

Finally I returned to original code (causing the error), and removed android:debuggable="false"from AndroidManifest.xml, causing the build to succeed.

最后我返回到原始代码(导致错误),并android:debuggable="false"从 中删除AndroidManifest.xml,导致构建成功。

I added it again and it still works... Don't ask me why :S

我再次添加它,它仍然有效......不要问我为什么:S

回答by Mpak

Add

添加

android.lintOptions.abortOnError false

to your app\build.gradle

到您的 app\build.gradle