2015-06-23 19 views
8

Tôi tạo một dự án Android bằng Android Studio. Trong build.gradle của ứng dụng thêm:Thuộc tính không tồn tại lỗi trong tệp cấu hình kiểu séc

apply from: '../config/quality.gradle' 

Sau đó, tôi tạo ra các dir config với hai tập tin: quality.gradle như:

apply plugin: 'checkstyle' 

task checkstyle(type: Checkstyle) { 
    configFile file("${project.rootDir}/config/checkstyle.xml") 
    source 'src' 
    include '**/*.java' 
    exclude '**/gen/**' 
    classpath = files() 
} 

checkstyle.xml như:

<?xml version="1.0"?> 
<!DOCTYPE module PUBLIC 
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN" 
    "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> 

<module name="Checker"> 

    <module name="TreeWalker"> 

     <module name="NeedBraces"> 
      <property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/> 
      <property name="allowSingleLineStatement" value="true"/> 
     </module> 

    </module> 

</module> 

Chạy gradle checkstyle mang lại cho tôi lỗi sau:

Executing external task 'checkstyle'... 
:app:checkstyle FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':app:checkstyle'. 
> Unable to create a Checker: cannot initialize module TreeWalker - Property 'allowSingleLineStatement' in module NeedBraces does not exist, please check the documentation 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

BUILD FAILED 

Nếu tôi loại bỏ dòng:

<property name="allowSingleLineStatement" value="true"/> 

nó hoạt động. Nhưng đọc documentation, phiên bản đầu tiên cũng sẽ hoạt động.

Nó xảy ra tương tự với:

<module name="EmptyCatchBlock"> 
    <property name="exceptionVariableName" value="expected|ignore"/> 
</module> 

mà ném tôi:

* What went wrong: 
Execution failed for task ':app:checkstyle'. 
> Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock 

Tôi đang làm gì sai hoặc bằng cách nào tôi missunderstanding các tài liệu?

Trả lời

16

Tại thời điểm viết bài này, Gradle uses Checkstyle 5.9 theo mặc định. Tài sản allowSingleLineStatement chỉ là added in Checkstyle 6.5. Vì vậy, bạn sẽ có thể làm việc này bằng cách sử dụng một phiên bản Checkstyle mới hơn như thế này:

checkstyle { 
    configFile = file("${project.rootDir}/config/checkstyle.xml") 
    toolVersion = '6.7' 
} 

Thật không may, các tài liệu Checkstyle không phiên bản, vì vậy trang web luôn có các tài liệu mới nhất mà thôi, mà làm cho nó khó có thể con số thứ như thế này.

+0

Lưu ý rằng tôi phải đi trước đường dẫn 'configFile' bằng từ khóa' file', nhưng giải pháp đã hoạt động. Cảm ơn rất nhiều, Thomas. – Birei

+0

Yup, 'configFile' là một 'Tập tin'; cập nhật câu trả lời của tôi cho phù hợp. Cảm ơn! –

+0

đã thử giải pháp này, nhưng IDEA cho tôi biết rằng 'Lỗi: (12, 0) Gradle: Đã xảy ra sự cố khi đánh giá dự án ': app'. > Không thể tìm thấy phương thức toolVersion() cho đối số [6.7] trên dự án ': app'.' – pratt

Các vấn đề liên quan