【问题标题】:gradle build fails on lint taskgradle build 在 lint 任务上失败
【发布时间】:2014-01-09 01:14:18
【问题描述】:

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

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'
}

当我从 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.

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

任何帮助都会很棒。

【问题讨论】:

    标签: android gradle android-gradle-plugin


    【解决方案1】:

    如果你设置abortOnError false,你会看到一个警告:

    无法访问“abortOnError”:它在“LintOptions”中是私有的

    感谢erluxman,您只需在lintOptions {} 块内的空白处按Ctrl+Space 即可查看属性并写入:

    lintOptions {
        // if true, stop the gradle build if errors are found
        isAbortOnError = false
        // if true, show all locations for an error, do not truncate lists, etc.
        isShowAll = true
    }
    

    【讨论】:

      【解决方案2】:

      添加

      android.lintOptions.abortOnError false
      

      到你的应用\build.gradle

      【讨论】:

      • 我怀疑 OP 想对它的工作原理做一点解释。
      【解决方案3】:

      我在 Android Studio 中遇到了一些 lint 错误,这些错误仅在我生成签名的 APK 时发生。

      为了避免这种情况,我将以下内容添加到build.gradle

      android {
          lintOptions {
              checkReleaseBuilds false
          }
      }
      

      【讨论】:

        【解决方案4】:

        对于我来说,这是一个快速解决您的问题的方法:

        android { 
          lintOptions { 
            abortOnError false 
          }
        }
        

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

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

        • 布局包含未解析的符号或缺少某些属性
        • 其他结构问题,例如使用不推荐使用的元素或目标 API 版本不支持的 API 调用,可能会导致代码无法正确运行。

        在 Android Studio 中通过Inspect Code 查找您的错误:Improve Your Code with Lint

        【讨论】:

        • 感谢您指出与又快又坏的解决方案相反的一面。我在 lint 生成的报告中发现了很多问题,可以在Application/build/reports/lint-results.html 找到。该报告非常详细且很有帮助。
        【解决方案5】:

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

        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'
            }
        }
        

        【讨论】:

        • 如果你真的想用lint(好),那是我认为最好的路径
        【解决方案6】:

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

        android { 
          lintOptions { 
            abortOnError false 
          }
        }
        

        然后清理你的项目:D

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            【解决方案8】:

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

            lintOptions {
                checkReleaseBuilds false
            }
            

            【讨论】:

            • 谢谢,在发布时使用 gradle-retrolambdaproject-lombok 编译时节省了我的时间
            【解决方案9】:

            0.7.0 提供了对 Lint 的扩展支持,但是,它并不总是能正常工作。 (例如,butterknife 库)

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

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

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

            (讨论: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
              }
            }
            

            如果您只需要禁用特定的 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'
              }
            }
            

            【讨论】:

            • 如果你想你只能用disable 'InvalidPackage'禁用包检查
            • 有没有办法从根 build.gradle 做到这一点,这样我就不必分叉一堆不同的子模块?
            • @AnkushGupta 你可以把它放在root android{} 当然
            • @MarekSebera 如果您有许多 Android 库和一个 Android 应用程序,这是否可行?我没有让它工作。你能举个例子吗?
            • @riper 可能有必要将 lintOptions 放入库中,但如果您将其放入根 android 块中,它应该可以工作,如果它不起作用,请尝试创建单独的问题以某种方式为您服务。
            【解决方案10】:

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

            我希望这可以帮助别人!

            【讨论】:

            • 确实不错的建议,只是一个注释,并不是所有的错误都可以手动纠正(比如缺少包javax.*
            【解决方案11】:

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

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

            尝试更改build.gradle,清除AndroidStudio缓存并重启,但没有任何变化。

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

            我又添加了一遍,它仍然有效...不要问我为什么:S

            【讨论】:

              猜你喜欢
              • 2014-06-24
              • 2020-03-16
              • 2021-08-07
              • 2023-03-15
              • 1970-01-01
              • 1970-01-01
              • 2014-10-25
              • 2016-01-19
              • 2021-08-01
              相关资源
              最近更新 更多