目录

我的学习分享

记录精彩的程序人生

X

Marauroa和LibGDX项目搭建过程

生成LibGDX工程

执行gdx-setup.jar
1.png
根据需要完成表单填写,按下Generate按钮,生成项目

增加server子项目

服务器和客户端有部分代码共享,服务器和客户端的代码都放到core子项目中,而server子项目中放入服务器的启动代码,与desktop和android子项目(包含客户端的启动代码)类似。

首先打开项目所在目录D:\GitHub\acuigame-pacman,将desktop目录拷贝一份并重命名为server目录

在build.gradle中,增加如下配置

project(":server") {
    apply plugin: "java"

    dependencies {
        compile project(":core")
    }
}

在settings.gradle中,增加server
修改前include 'desktop', 'android', 'core'
修改后include 'desktop', 'server', 'android', 'core'

子项目改名

默认子项目名称都是core、desktop、android等,如果同时打开多个项目,不同项目的子项目之间不容易区分。故在子项目名称前加上项目名称。
首先启动Netbeans8.2,并打开第一步中生成的项目acuigame-pacman
2.png
此时子项目尚未展开,等待一段时间后,系统自动加载子项目
3.png
打开build.gradle文件进行如下修改
project(":desktop")改为project(":acuigame-pacman-desktop")
project(":server")改为project(":acuigame-pacman-server")
project(":android")改为project(":acuigame-pacman-android")
project(":core")改为project(":acuigame-pacman-core")
compile project(":core")改为compile project(":acuigame-pacman-core")
此时build.gradle文件内容如下

buildscript {
    

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        

    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
        appName = "acuigame-pacman"
        gdxVersion = '1.9.9'
        roboVMVersion = '2.3.5'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        google()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":acuigame-pacman-desktop") {
    apply plugin: "java"


    dependencies {
        compile project(":acuigame-pacman-core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"
        
    }
}

project(":acuigame-pacman-server") {
    apply plugin: "java"

    dependencies {
        compile project(":acuigame-pacman-core")
    }
}

project(":acuigame-pacman-android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile project(":acuigame-pacman-core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
        compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
        compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-controllers-android:$gdxVersion"
        
    }
}

project(":acuigame-pacman-core") {
    apply plugin: "java"


    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
        compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
        compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
        
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

settings.gradle内容如下

include 'acuigame-pacman-desktop', 'acuigame-pacman-server', 'acuigame-pacman-android', 'acuigame-pacman-core'

相应的更改子项目目录名,在其前面加上项目名前缀acuigame-pacman-

子项目调整

acuigame-pacman-server

将acuigame-pacman-server子项目中的DesktopLauncher重命名为ServerLauncher作为服务端的启动类,同时删掉main方法中的代码,将包结构重构为com.acuigame.pacman.server

package com.acuigame.pacman.server;

public class ServerLauncher {
	public static void main (String[] arg) {
	    // TODO: 增加服务端的启动代码
	}
}

修改子项目下的build.gradle文件

// 修改前
project.ext.mainClassName = "com.acuigame.pacman.desktop.DesktopLauncher"
// 修改后
project.ext.mainClassName = "com.acuigame.pacman.server.ServerLauncher"

// 修改前
name = appName + "-desktop"
// 修改后
name = appName + "-server"

// 修改前
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/android/assets'
// 修改后
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/acuigame-pacman-android/assets'

acuigame-pacman-desktop

修改子项目下的build.gradle文件

// 修改前
project.ext.assetsDir = new File("../android/assets");
// 修改后
project.ext.assetsDir = new File("../acuigame-pacman-android/assets");

// 修改前
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/android/assets'
// 修改后
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/acuigame-pacman-android/assets'

acuigame-pacman-android子项目编译后,报错Could not find org.jetbrains.trove4j:trove4j:20160824.,参考Could not find org.jetbrains.trove4j:trove4j:20160824.

至此,几个子项目都可以正常编译执行。下面的工作是集成Marauroa相关lib。
在acuigame-pacman-core目录下创建libs目录,并将Marauroa相关jar包拷入其中。
在build.gradle中,增加compile fileTree(dir:"libs",include:["*.jar"])

project(":acuigame-pacman-core") {
    apply plugin: "java"


    dependencies {
        compile fileTree(dir:"libs",include:["*.jar"])
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
        compile "com.badlogicgames.gdx:gdx-ai:$aiVersion"
        compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
    }
}

修改完成后,在acuigame-pacman-core子项目右键,打开菜单并选择Reload Project刷新


别忘了下面这个,修改acuigame-pacman-core的build.gradle文件

apply plugin: "java"

sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = sourceSets.main.java.srcDirs


eclipse.project {
    name = appName + "-core"
}

解决Gradle工程编译源文件忽略xml,properties,config等文件的问题

又一个报错

:acuigame-template-android:lint FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':acuigame-template-android:lint'.
> Lint found errors in the project; aborting build.
  
  Fix the issues identified by lint, or add the following to your build script to proceed with errors:
  ...
  android {
      lintOptions {
          abortOnError false
      }
  }
  ...
  
  The first 3 errors (out of 12) were:
  D:\GitHub\acuigame-template\acuigame-template-core\libs\log4j.jar: Error: Invalid package reference in library; not included in Android: javax.jms. Referenced from org.apache.log4j.or.jms.MessageRenderer. [InvalidPackage]
  D:\GitHub\acuigame-template\acuigame-template-core\libs\marauroa-3.9.6.jar: Error: Invalid package reference in library; not included in Android: java.awt. Referenced from marauroa.common.net.message.MessageC2SAction. [InvalidPackage]
  D:\GitHub\acuigame-template\acuigame-template-core\libs\marauroa-3.9.6.jar: Error: Invalid package reference in library; not included in Android: java.lang.management. Referenced from marauroa.server.marauroad. [InvalidPackage]
  

上面的错误信息里已经有提示,就是在android子项目的build.gradle配置文件中增加

  android {
      lintOptions {
          abortOnError false
      }
  }

另一个问题

android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

另外注意,下面的gradle版本号是3.2.0,而不是别的什么

buildscript {
    

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'com.mobidevelop.robovm:robovm-gradle-plugin:2.3.5'
        classpath 'org.multi-os-engine:moe-gradle:1.4.0'
        

    }
}