将 Fabric 与 Libgdx 集成

Posted

技术标签:

【中文标题】将 Fabric 与 Libgdx 集成【英文标题】:Integrate Fabric with Libgdx 【发布时间】:2015-08-16 16:32:43 【问题描述】:

我正在尝试将 Fabric SDK 与我的 android 版 libgdx 应用程序集成。

fabric.io 提供的 Android Studio 插件不适用于我的 Gradle 构建文件(由 Libgdx tool 生成)。该插件只是没有触及它们。

所以我尝试手动编辑它们。 这是我的build.gradle 文件:

buildscript 
    repositories 
        mavenCentral()
        maven  url 'https://maven.fabric.io/public' 
    
    dependencies 
        classpath 'com.android.tools.build:gradle:1.1.+'
        classpath 'io.fabric.tools:gradle:1.+'
    


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

    version = '1.0'
    ext 
        appName = 'App'
        gdxVersion = '1.5.4'
        roboVMVersion = '1.0.0-beta-04'
        box2DLightsVersion = '1.3'
        ashleyVersion = '1.3.1'
        aiVersion = '1.5.0'
    

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


project(":desktop") 
    apply plugin: "java"

    dependencies 
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
    


project(":android") 
    apply plugin: "android"
    apply plugin: 'io.fabric'

    configurations  natives 

    dependencies 
        compile project(":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-x86"
        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-x86"
    

    repositories 
        mavenCentral()
        maven  url 'https://maven.fabric.io/public' 
    


project(":core") 
    apply plugin: "java"

    dependencies 
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    


tasks.eclipse.doLast 
    delete ".project"

我没有接触过android/build.gradle 文件。

android 
    buildToolsVersion "22.0.1"
    compileSdkVersion 22
    sourceSets 
        main 
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        

        instrumentTest.setRoot('tests')
    


// needed to add JNI shared libraries to APK when compiling on CLI
tasks.withType(com.android.build.gradle.tasks.PackageApplication)  pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(projectDir, '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("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each  jar ->
        def outputDir = null
        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.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', 'net.snuzzle.android/net.snuzzle.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:22.1.1'
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.melnykov:floatingactionbutton:1.2.0'

SDK 只是不在类路径中。

【问题讨论】:

【参考方案1】:

我正在使用这种方式将 Fabric (Crashlytics) 添加到我的 LibGDX 项目中。

这是我的build.gradle

buildscript 
    repositories 
        mavenCentral()
        maven  url "https://oss.sonatype.org/content/repositories/snapshots/" 
        maven  url 'https://maven.fabric.io/public' 
    
    dependencies 
        classpath 'com.android.tools.build:gradle:1.2.3'
        ///...
        classpath 'io.fabric.tools:gradle:1.+'
    


///...

project(":android") 
    apply plugin: 'com.android.application'
    apply plugin: 'io.fabric'

    repositories 
        jcenter()
        maven  url 'https://maven.fabric.io/public' 
    

///....
  dependencies 
   //...
   compile('com.crashlytics.sdk.android:crashlytics:2.2.2@aar') 
        transitive = true;
    
//...

我认为与您的 gradle 配置的唯一区别是添加了 crashlytics 依赖项。

现在在您的清单中添加android.permission.INTERNET 权限,也在AndroidLauncheronCreate 方法中添加此行

@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
///...

不要忘记在您的 android 模块中创建 fabric.properties 文件并将这些行添加到其中:

apiSecret= YOUR_API_SECRET_FROM_PANEL
apiKey=YOUR_API_KEY_FROM_PANEL

现在点击(android studio 的fabric 插件)并登录,然后点击install crashlytics(或其他服务),在一些next按钮之后,您将查看一个对话框,告诉您运行应用程序以激活结构。

现在您将在织物面板中看到您的游戏。

【讨论】:

谢谢,我稍后再试。我已经想知道我没有 Crashlytics 依赖项,但我在 Fabric 文档中没有找到相关内容。

以上是关于将 Fabric 与 Libgdx 集成的主要内容,如果未能解决你的问题,请参考以下文章

在 pyenv 中安装 Fabric 错误

如何更改 Fabric 中的时区? [复制]

fabric.js 有任何官方的 Typescript 定义吗?

Service Fabric 与 Ocelot 集成

如何故意使fabric.io中的构建过期

HyperLedger Fabric安装在中间并且在MacOS中没有进展?