việc xây dựng gradle không thành công trên nhiệm vụ lint


95

Tôi có một dự án Android đơn giản mà tôi đã tạo bằng Android Studio 0.4.0. Tôi sử dụng Gradle 1.9 và Gradle Android Plugin 0.7. Hôm qua, tôi đã thêm thư viện ButterKnife của Jake Wharton vào tập lệnh xây dựng lớp học của mình:

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

Khi tôi chạy ứng dụng từ Android Studio, bản dựng chạy tốt và thực thi chính xác trên thiết bị của tôi. Nhưng khi tôi thử (từ dòng lệnh) gradle buildthì bản dựng không thành công. Đây là một phần từ báo cáo xơ vải của tôi:

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.

Có thể tôi đang thiếu một cái gì đó, nhưng không thể xây dựng dự án trong thiết bị đầu cuối sẽ chặn khả năng CI cho các dự án Android.

Bất kỳ sự trợ giúp nào đều sẽ là tuyệt vời.

Câu trả lời:


143

Với 0.7.0 có hỗ trợ mở rộng cho Lint, tuy nhiên, nó không phải lúc nào cũng hoạt động bình thường. (Ví dụ: thư viện butterknife)

Giải pháp là vô hiệu hóa hủy bỏ xây dựng trên các lỗi xơ vải được tìm thấy

Tôi lấy cảm hứng từ https://android.googlesource.com/platform/tools/base/+/e6a5b9c7c1bca4da402de442315b5ff1ada819c7

(thực hiện: https://android.googlesource.com/platform/tools/base/+/e6a5b9c7c1bca4da402de442315b5ff1ada819c7/build-system/gradle/src/main/groovy/com/android/build/gradle/internal/model/DefaultAndroidProject.java )

(thảo luận: 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
  }
}

Và nếu bạn chỉ cần vô hiệu hóa quy tắc Lint cụ thể và giữ cho việc xây dựng không thành công với những người khác, hãy sử dụng điều này:

/*
 * 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
Nếu bạn muốn, bạn chỉ có thể vô hiệu hóa việc kiểm tra gói vớidisable 'InvalidPackage'
Calin

Có cách nào để thực hiện việc này từ build.gradle gốc để tôi không phải phân nhánh một loạt các mô-đun con khác nhau không?
ankushg 27/12/13

2
@AnkushGupta bạn có thể đặt nó trong thư mục gốc android{}khối dĩ nhiên
Marek Sebera

@MarekSebera Điều đó có hoạt động nếu bạn có một số thư viện Android và ứng dụng Android không? Tôi đã không làm cho nó hoạt động. Bạn có thể cho một ví dụ về cách làm?
riper

@riper có thể cần thiết phải đưa nó lintOptionsvào các thư viện, nhưng nó sẽ hoạt động nếu bạn đặt nó vào root androidblock, hãy thử tạo câu hỏi riêng biệt, nếu nó không hiệu quả với bạn bằng cách nào đó.
Marek Sebera

53

nếu abortOnError falsekhông giải quyết được vấn đề của bạn, bạn có thể thử cách này.

lintOptions {
    checkReleaseBuilds false
}

1
Cảm ơn, lưu ngày của tôi khi biên dịch với gradle-retrolambdaproject-lomboktrong phiên bản
Jason Sparc

37

Bạn có thể chọn các tùy chọn thích hợp từ đây

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

Nếu bạn thực sự muốn sử dụng lint (tốt), đó là con đường tốt nhất mà tôi nghĩ
Cicero Moura

15

Tôi đã gặp một số lỗi lint trong Android Studio chỉ xảy ra khi tôi tạo APK đã ký.

Để tránh nó, tôi đã thêm phần sau vào build.gradle

android {
    lintOptions {
        checkReleaseBuilds false
    }
}

11

Thêm các dòng này vào tệp build.gradle của bạn:

android { 
  lintOptions { 
    abortOnError false 
  }
}

Sau đó làm sạch dự án của bạn: D


8

Nếu bạn muốn tránh tùy chọn "abortInError false", hãy xem tệp build / lint-results-release-Fat.html. Đây là những lỗi được phát hiện bởi lint.

Tôi hy vọng điều này có thể giúp ai đó!


1
Trên thực tế gợi ý tốt, chỉ cần một lưu ý, không phải tất cả lỗi đều tự sửa chữa được (chẳng hạn như thiếu gói javax.*)
Marek Sebera

4

Trong Android Studio v1.2, nó cho bạn biết cách khắc phục nó:

nhập mô tả hình ảnh ở đây


3

Gặp lỗi tương tự trên AndroidStudio phiên bản 0.51

Bản dựng đang hoạt động tốt và đột nhiên, sau khi chỉ thay đổi giá trị mã phiên bản, tôi đã gặp lỗi bản dựng liên quan đến Lint.

Đã cố gắng thay đổi build.gradle, xóa bộ nhớ cache AndroidStudio và khởi động lại, nhưng không thay đổi.

Cuối cùng, tôi quay lại mã gốc (gây ra lỗi) và xóa android:debuggable="false"khỏi AndroidManifest.xml, khiến quá trình xây dựng thành công.

Tôi đã thêm lại nó và nó vẫn hoạt động ... Đừng hỏi tôi tại sao: S


3

Đối với tôi, đó là một giải pháp tồi và nhanh chóng cho vấn đề của bạn:

android { 
  lintOptions { 
    abortOnError false 
  }
}

Giải pháp tốt hơn là giải quyết vấn đề trong mã của bạn, bởi vì công cụ lint kiểm tra các tệp nguồn dự án Android của bạn để tìm các lỗi tiềm ẩn và cải tiến tối ưu hóa cho tính đúng đắn, bảo mật, hiệu suất, khả năng sử dụng, khả năng truy cập và quốc tế.

Sự cố này thường xuyên xảy ra nhất khi:

  • Bố cục bao gồm các ký hiệu chưa được giải quyết hoặc thiếu một số thuộc tính
  • Các vấn đề cấu trúc khác, chẳng hạn như sử dụng các phần tử không dùng nữa hoặc lệnh gọi API không được hỗ trợ bởi các phiên bản API mục tiêu, có thể dẫn đến mã không chạy chính xác.

Tìm lỗi của bạn Inspect Codetrong Android Studio: Cải thiện mã của bạn với Lint


1
Cảm ơn vì đã chỉ ra đối diện của một giải pháp xấu và nhanh chóng . Tôi đã phát hiện ra rất nhiều vấn đề trên báo cáo được tạo bởi lint có sẵn tại Application/build/reports/lint-results.html. Báo cáo rất chi tiết và hữu ích.
jgrocha

1

Thêm vào

android.lintOptions.abortOnError false

vào ứng dụng của bạn \ build.gradle


1
Tôi nghi ngờ OP muốn giải thích một chút về hoạt động của việc này.
sniperd
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.