gradle - 如何将 checkstyle 添加到 libGDX 项目?
问题描述
我有以下libGDX
项目:https ://github.com/Glusk2/sproouts
我尝试在根文件中添加checkstyle
整个项目,如下所示:build.gradle
// ...
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "checkstyle"
// ...
}
// ...
并添加config/checkstyle/checkstyle.xml
到根项目中。
但它不起作用。运行后出现此错误./gradlew build connectedCheck
:
:android:compileJava
<path-to-project>\sprouts\android\src\com\github\glusk2\sprouts\AndroidLauncher.java:3: error: package android.os does not exist
import android.os.Bundle;
^
<path-to-project>\sprouts\android\src\com\github\glusk2\sprouts\AndroidLauncher.java:9: error: cannot access Activity
public class AndroidLauncher extends AndroidApplication {
^
class file for android.app.Activity not found
<path-to-project>\sprouts\android\src\com\github\glusk2\sprouts\AndroidLauncher.java:11: error: cannot find symbol
protected void onCreate (Bundle savedInstanceState) {
^
symbol: class Bundle
location: class AndroidLauncher
<path-to-project>\sprouts\android\src\com\github\glusk2\sprouts\AndroidLauncher.java:10: error: method does not override or implement a method from a supertype
@Override
^
<path-to-project>\sprouts\android\src\com\github\glusk2\sprouts\AndroidLauncher.java:12: error: cannot find symbol
super.onCreate(savedInstanceState);
^
symbol: variable super
location: class AndroidLauncher
<path-to-project>\sprouts\android\src\com\github\glusk2\sprouts\AndroidLauncher.java:15: error: cannot find symbol
initialize(new Sprouts(), config);
^
symbol: method initialize(Sprouts,AndroidApplicationConfiguration)
location: class AndroidLauncher
6 errors
:android:compileJava FAILED
解决方案
默认情况下,将checkstyle
插件应用于 Android gradle 项目什么都不做(它不添加任何 checkstyle 任务),因为:
请注意,Android 插件使用自己的源集实现。
Android 插件有自己的
sourceSet
概念,并不填充插件的sourceSet
集合java-base
。
因此,默认情况下应用类似的插件checkstyle
不会执行任何操作。
https://issuetracker.google.com/issues/36972352#comment21
但就我而言,包含字符串“checkstyle”的任务列表如下所示:
$ ./gradlew tasks --all | grep checkstyle
android:checkstyleMain - Run Checkstyle analysis for main classes //<-- this should not be here :)
core:checkstyleMain - Run Checkstyle analysis for main classes
desktop:checkstyleMain - Run Checkstyle analysis for main classes
html:checkstyleMain - Run Checkstyle analysis for main classes
ios:checkstyleMain - Run Checkstyle analysis for main classes
core:checkstyleTest - Run Checkstyle analysis for test classes
desktop:checkstyleTest - Run Checkstyle analysis for test classes
html:checkstyleTest - Run Checkstyle analysis for test classes
ios:checkstyleTest - Run Checkstyle analysis for test classes
那个任务是怎么android:checkstyleMain
加到列表中的?
原来这是原因(android项目build.gradle
文件):
// ...
eclipse {
// ...
sourceSets {
main {
java.srcDirs "src", 'gen'
}
}
// ...
}
// ...
它设置了sourceSets
插件checkstyle
可以检测到的,但配置显然是不完整的,因为 build failed for task: android:checkstyleMain
。
上面的eclipse
块添加了Eclipse ADT插件的配置,现在已经有一段时间不支持了,所以我决定完全删除该块。
$ ./gradlew tasks --all | grep checkstyle
core:checkstyleMain - Run Checkstyle analysis for main classes
desktop:checkstyleMain - Run Checkstyle analysis for main classes
html:checkstyleMain - Run Checkstyle analysis for main classes
ios:checkstyleMain - Run Checkstyle analysis for main classes
core:checkstyleTest - Run Checkstyle analysis for test classes
desktop:checkstyleTest - Run Checkstyle analysis for test classes
html:checkstyleTest - Run Checkstyle analysis for test classes
ios:checkstyleTest - Run Checkstyle analysis for test classes
成功!但我仍然没有设法让 checkstyle 与 android 项目一起工作。
在阅读了许多关于 checkstyle 任务未显示为 android 项目的类似问题之后......
...我想出了解决方案。
解决方案
根build.gradle
:
// ...
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
+ apply plugin: "checkstyle"
+ checkstyle {
+ configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
+ }
// ...
}
// ...
安卓build.gradle
:
+task checkstyle(type: Checkstyle) {
+ source 'src'
+ include '**/*.java'
+ exclude '**/gen/**'
+ exclude '**/R.java'
+ exclude '**/BuildConfig.java'
+ classpath = files()
+}
+check.dependsOn "checkstyle"
// ...
-// sets up the Android Eclipse project, using the old Ant based build.
-eclipse {
- // need to specify Java source sets explicitly, SpringSource Gradle Eclipse -plugin
- // ignores any nodes added in classpath.file.withXml
- sourceSets {
- main {
- java.srcDirs "src", 'gen'
- }
- }
-
- jdt {
- sourceCompatibility = 1.6
- targetCompatibility = 1.6
- }
-
- classpath {
- plusConfigurations += [ project.configurations.compile ]
- containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
- }
-
- project {
- name = appName + "-android"
- natures 'com.android.ide.eclipse.adt.AndroidNature'
- buildCommands.clear();
- buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
- buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
- buildCommand "org.eclipse.jdt.core.javabuilder"
- buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
- }
-}
推荐阅读
- javascript - 将数组的文件对象数据分别映射到另一个数组的字段
- adfs - 是否可以针对 ADFS 2019 中的 Sql Db 进行身份验证?
- r - 将所有标有“N / A”的空字段转换为R中的NA
- json - 如何从python中的http响应解析json?
- mysql - 如果找到 SoldTable,则创建一个触发器以从 NotSoldTable 中删除
- php - 在php中如何获取深度未知的多维数组的所有键
- docker - Docker Bench for Security 用于 Google Cloud 上的映像
- java - Android studio:在android studio中配置项目
- wordpress - 无法更改 WordPress 地址 (URL)
- c# - AuthorizeAttribute 不适用于 ASP.NET Core 3.1 中的端点路由