如何将检查样式添加到libGDX项目

如何将检查样式添加到libGDX项目

本文介绍了如何将检查样式添加到libGDX项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下libGDX项目: https://github.com/Glusk2 /sprouts

我尝试将checkstyle添加到根build.gradle文件中的整个项目,如下所示:

I've tried adding checkstyle to the entire project in the root build.gradle file, like so:

// ...
allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"
    apply plugin: "checkstyle"
    // ...
}
// ...

并将config/checkstyle/checkstyle.xml添加到根项目.

但是它不起作用.运行./gradlew build connectedCheck后出现此错误:

But it doesn't work. I get this error after running ./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任务),因为:

Applying the checkstyle plugin to an Android gradle project does nothing (it adds no checkstyle tasks) by default because:

Android Gradle插件; sourceSets {}

https://issuetracker.google.com/issues/36972352#comment21

但是在我的情况下,包含字符串"checkstyle"的任务列表如下所示:

But in my case, the task list containing the string "checkstyle" looked like this:

$ ./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为何添加到列表中?

How come that task android:checkstyleMain was added to the list?

事实证明这是原因(android项目build.gradle文件):

It turns out this was the reason (android project build.gradle file):

// ...
eclipse {
    // ...
    sourceSets {
        main {
            java.srcDirs "src", 'gen'
        }
    }
    // ...
}
// ...

它设置了checkstyle插件可以检测到的sourceSets,但是配置显然不完整,因为任务android:checkstyleMain的构建失败.
上面的eclipse块添加了 Eclipse ADT 的配置插件,已经有一段时间不被支持了,所以我决定将其全部删除.

It set the sourceSets that the checkstyle plugin could detect, but configuration was obviously incomplete because the build failed for task: android:checkstyleMain.
The above eclipse block among others added configuration for the Eclipse ADT plugin, which has been unsupported for a while now so I've decided to remove that block altogether.

$ ./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项目一起工作.

Success! But I still haven't managed to get checkstyle working with the android project.

在阅读了有关checkstyle任务的许多类似问题后,这些问题并未显示在android项目中...

After reading many similar questions about checkstyle tasks not showing for android projects...

  • Get Android gradle plugin & checkstyle working together / command line usage
  • Checkstyle Plugin does not add gradle tasks

...我想出了解决方案.

... I've come up with the solution.

build.gradle:

// ...
allprojects {
     apply plugin: "eclipse"
     apply plugin: "idea"
+    apply plugin: "checkstyle"

+    checkstyle {
+        configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
+    }
// ...
}
// ...

Android 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"
-    }
-}

这篇关于如何将检查样式添加到libGDX项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:07