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

标签 gradle libgdx android-gradle-plugin

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

我尝试将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任务),因为:

Note that the Android plugin uses its own implementation of source sets.


Android Gradle plugin; sourceSets{}

The Android plugin has its own sourceSet concept and does not populate the sourceSet collection of the java-base plugin.
So applying a plugin like checkstyle will do nothing by default.


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,但是配置显然不完整,因为构建任务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项目未显示该问题。
  • Get Android gradle plugin & checkstyle working together / command line usage
  • Checkstyle Plugin does not add gradle tasks

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


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

    关于gradle - 如何将检查样式添加到libGDX项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54851229/

    相关文章:

    java - Intellij找不到commons.dbcp2

    java - 梯度配置错误

    gradle - 从单个项目源构建多个 jar

    java - LibGDX 项目无法在我的安卓设备上运行

    javascript - 如何修复 "Its dependency ' kotlinx-coroutines-core' 未找到。”

    java - libgdx 捕获所有异常,只显示 RuntimeException 而不是原始异常

    具有多个倾斜集的 Libgdx AtlasTmxMapLoader

    android - 如何解决 Android Studio 找不到项目 list 文件的错误?

    android - build.gradle 存储库合并

    android - 无法使用 Superpowered 库为 arm64-v8a 创建 so 文件