gradle构建失败在皮棉任务上


95

我有一个使用Android Studio 0.4.0创建的简单android项目。我使用Gradle 1.9和Gradle Android Plugin 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'
}

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

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项目的可能性。

任何帮助都会很棒。

Answers:


143

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

解决方案是禁用在发现的皮棉错误上中止构建

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

19
如果需要,您只能使用disable 'InvalidPackage'
Calin

有没有一种方法可以从root build.gradle根目录执行此操作,这样我就不必派生许多不同的子模块了?
13年

2
@AnkushGupta android{}当然可以放到根目录中
Marek Sebera 2013年

@MarekSebera如果您有许多Android库和一个Android应用程序,那行得通吗?我没有得到它的工作。您能举个例子吗?
2014年

@riper可能有必要将它们放入lintOptions库中,但是如果将其放入根android块中应该可以使用,尝试创建一个单独的问题,如果它对您不起作用。
Marek Sebera 2014年

53

如果abortOnError false无法解决您的问题,则可以尝试此操作。

lintOptions {
    checkReleaseBuilds false
}

1
谢谢,保存了我的编译gradle-retrolambdaproject-lombok发布时的一天
Jason Sparc

37

您可以从此处选择适当的选项

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

如果你真的想使用皮棉(好),这是我认为最好的路径
西塞罗·莫拉

15

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

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

android {
    lintOptions {
        checkReleaseBuilds false
    }
}


8

如果要避免使用“ abortInError false”选项,请查看build / lint-results-release-fatal.html文件。这是皮棉检测到的错误。

我希望这可以帮助某人!


1
确实是个好建议,只是一个提示,并非所有错误都可以手动纠正(例如缺少包裹javax.*
Marek Sebera 2014年

4

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

在此处输入图片说明


3

在AndroidStudio版本0.51上遇到相同的错误

构建工作正常,突然之间,仅更改版本代码值后,出现了Lint相关的构建错误。

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

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

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


3

对于我来说,这是解决您问题的快速而又不好的解决方案:

android { 
  lintOptions { 
    abortOnError false 
  }
}

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

在以下情况下最常发生问题

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

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


1
感谢您指出不良和快速解决方案的反面。我发现在lint生成的报告中存在很多问题,网址Application/build/reports/lint-results.html。该报告非常详细且有帮助。
jgrocha

1

android.lintOptions.abortOnError false

到您的app \ build.gradle


1
我怀疑OP需要对此作一些解释。
sniperd
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.