You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android构建APK报错:Program type already present: Json$Serializer

Android APK Build Failure (No Clear Root Cause)

This issue popped up out of nowhere and only affects my Android project and APK build process. I haven't been able to pinpoint the root cause yet—I spent hours last night searching for solutions but came up empty-handed, so I'm reaching out for help here.

Below are the Gradle configuration files for my project:

Global build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.mobidevelop.robovm:robovm-gradle-plugin:2.3.1'
    }
}

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

    version = ""
    ext {
        appName = "fade-online"
        gdxVersion = '1.9.8'
        roboVMVersion = '2.3.1'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

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

project(":desktop") {
    apply plugin: "java"
    dependencies {
        implementation project(":core")
        implementation "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

project(":rtoserver") {
    apply plugin: "java"
    dependencies {
        implementation "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

project(":editorsuite") {
    apply plugin: "java"
    dependencies {
        implementation project(":core")
        implementation "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android"
    configurations { natives }
    android {
        dexOptions {
            preDexLibraries = false
        }
    }
    dependencies {
        implementation project(":core")
        implementation "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"
    }
}

project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"
    dependencies {
        implementation project(":core")
        implementation "com.mobidevelop.robovm:robovm-rt:$roboVMVersion"
        implementation "com.mobidevelop.robovm:robovm-cocoatouch:$roboVMVersion"
        implementation "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
    }
}

project(":core") {
    apply plugin: "java"
    dependencies {
        implementation "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}

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

Android Module build.gradle

android {
    buildToolsVersion '28.0.3'
    compileSdkVersion 28
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
    }
    packagingOptions {
        exclude 'META-INF/robovm/ios/robovm.xml'
    }
    defaultConfig {
        applicationId "com.forgottenartsstudios.fadeonline"
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 7
        versionName '0.7.000'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
    }
}

task copyAndroidNatives() {
    file("libs/armeabi/").mkdirs()
    file("libs/armeabi-v7a/").mkdirs()
    file("libs/arm64-v8a/").mkdirs()
    file("libs/x86_64/").mkdirs()
    file("libs/x86/").mkdirs()

    configurations.natives.files.each { jar ->
        def outputDir = null
        if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
        if (jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
        if (outputDir != null) {
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}

task run(type: Exec) {
    def path
    def localProperties = project.file("../local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        if (sdkDir) {
            path = sdkDir
        } else {
            path = "$System.env.ANDROID_HOME"
        }
    } else {
        path = "$System.env.ANDROID_HOME"
    }

    def adb = path + "/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.forgottenartsstudios.fadeonline/com.forgottenartsstudios.fadeonline.AndroidLauncher'
}

eclipse {
    sourceSets {
        main {
            java.srcDirs "src", 'gen'
        }
    }
    jdt {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    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"
    }
}

idea {
    module {
        sourceDirs += file("src")
        scopes = [COMPILE: [plus: [project.configurations.compile]]]
        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance()
                builder.current = node
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
                            option(name: "UPDATE_PROPERTY_FILES", value: "true")
                        }
                    }
                }
            }
        }
    }
}

dependencies {
    implementation files('libs/kryonet-2.21-all.jar')
}

Core Module build.gradle

apply plugin: "java"

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

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

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

dependencies {
    implementation files('libs/kryonet-2.21-all.jar')
    implementation project(':rtoserver')
}

Server (rtoserver) Module build.gradle

apply plugin: 'java'

dependencies {
    implementation files('libs/kryonet-2.21-all.jar')
    implementation 'com.badlogicgames.gdx:gdx:1.9.8'
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

Desktop Module build.gradle

apply plugin: "java"

sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = ["src/"]

project.ext.mainClassName = "com.forgottenartsstudios.fadeonline.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../android/assets")

task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task debug(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}

task dist(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from { configurations.compile.collect { zipTree(it) } }
    from files(project.assetsDir)
    baseName = "FADEOnline"
    manifest {
        attributes 'Main-Class': project.mainClassName
    }
}

dist.dependsOn classes

eclipse {
    project {
        name = appName + "-desktop"
        linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC'
    }
}

task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
    doLast {
        def classpath = new XmlParser().parse(file(".classpath"))
        new Node(classpath, "classpathentry", [kind: 'src', path: 'assets'])
        def writer = new FileWriter(file(".classpath"))
        def printer = new XmlNodePrinter(new PrintWriter(writer))
        printer.setPreserveWhitespace(true)
        printer.print(classpath)
    }
}

dependencies {
    implementation project(path: ':android', configuration: 'default')
}

I'm new to Stack Overflow, so please forgive me if I've made any missteps in formatting or information provided. Feel free to ask for any additional details you might need—thank you so much for your help!


内容的提问来源于stack exchange,提问作者Perfekt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:49:01