Android 将libGDX与gradle一起使用

Android 将libGDX与gradle一起使用,android,android-ndk,libgdx,android-gradle-plugin,gradle-experimental,Android,Android Ndk,Libgdx,Android Gradle Plugin,Gradle Experimental,对于一个小游戏项目,我们必须使用gradle-experiative来包含共享库,并使用NDK,对于图形,我们使用libgdx 以前,我们没有使用实验gradle和libgdx,但现在我们有一个例外: FATAL EXCEPTION: main Process: eu.epitech.gomoku.android, PID: 28180

对于一个小游戏项目,我们必须使用
gradle-experiative
来包含
共享库
,并使用
NDK
,对于图形,我们使用
libgdx

以前,我们没有使用实验gradle和libgdx,但现在我们有一个例外:

FATAL EXCEPTION: main
                                                                       Process: eu.epitech.gomoku.android, PID: 28180
                                                                           java.lang.ExceptionInInitializerError
                                                                               at java.lang.Class.newInstance(Native Method)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1090)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                            Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load shared library 'gdx' for target: Linux, 32-bit
                                                                               at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:118)
                                                                               at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:34)
                                                                               at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:61)
                                                                               at java.lang.Class.newInstance(Native Method) 
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1090) 
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
                                                                            Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/eu.epitech.gomoku.android-1/base.apk"],nativeLibraryDirectories=[/data/app/eu.epitech.gomoku.android-1/lib/arm64, /data/app/eu.epitech.gomoku.android-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]] couldn't find "libgdx.so"
                                                                               at java.lang.Runtime.loadLibrary(Runtime.java:367)
                                                                               at java.lang.System.loadLibrary(System.java:1076)
                                                                               at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:113)
                                                                               at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:34) 
                                                                               at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:61) 
                                                                               at java.lang.Class.newInstance(Native Method) 
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1090) 
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Android文件夹:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "eu.epitech.gomoku.android"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 23
            versionCode = 4
            versionName = "1.0.1"
        }
    }

    android.ndk {
        moduleName = "GomokuRules"
        cppFlags.add("-I${file("jni/headers/")}".toString())
        stl = "c++_shared"
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file("proguard-rules.pro"))
        }
    }

    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }

    android.sources {
        main {
            manifest {
                source {
                    srcDir '.'
                    include 'AndroidManifest.xml'
                }
            }
            resources {
                source {
                    srcDir 'res/'
                }
            }
            assets {
                source {
                    srcDir 'assets/'
                }
            }
            java {
                source {
                    srcDir 'src/eu/epitech/gomoku/android/'
                }
            }
            jniLibs {
                source {
                    srcDir 'libs/'
                }
            }
        }
    }

}
// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
    file("src/main/libs/armeabi/").mkdirs();
    file("src/main/libs/armeabi-v7a/").mkdirs();
    file("src/main/libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("src/main/libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("src/main/libs/armeabi")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("src/main/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', 'eu.epitech.gomoku.android/eu.epitech.gomoku.android.AndroidLauncher'
}

// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
    // need to specify Java source sets explicitely, 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"
    }
}

// sets up the Android Idea project, using the old Ant based build.
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 {
    compile 'com.android.support:support-v4:23.+'
}
也许这会有帮助:也许这会有帮助:
apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "eu.epitech.gomoku.android"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 23
            versionCode = 4
            versionName = "1.0.1"
        }
    }

    android.ndk {
        moduleName = "GomokuRules"
        cppFlags.add("-I${file("jni/headers/")}".toString())
        stl = "c++_shared"
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file("proguard-rules.pro"))
        }
    }

    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }

    android.sources {
        main {
            manifest {
                source {
                    srcDir '.'
                    include 'AndroidManifest.xml'
                }
            }
            resources {
                source {
                    srcDir 'res/'
                }
            }
            assets {
                source {
                    srcDir 'assets/'
                }
            }
            java {
                source {
                    srcDir 'src/eu/epitech/gomoku/android/'
                }
            }
            jniLibs {
                source {
                    srcDir 'libs/'
                }
            }
        }
    }

}
// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
    file("src/main/libs/armeabi/").mkdirs();
    file("src/main/libs/armeabi-v7a/").mkdirs();
    file("src/main/libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("src/main/libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("src/main/libs/armeabi")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("src/main/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', 'eu.epitech.gomoku.android/eu.epitech.gomoku.android.AndroidLauncher'
}

// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
    // need to specify Java source sets explicitely, 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"
    }
}

// sets up the Android Idea project, using the old Ant based build.
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 {
    compile 'com.android.support:support-v4:23.+'
}