Libgdx 上的 Crashlytics

Crashlytics on Libgdx

我正在尝试在基于 Libgdx 框架的当前游戏项目中使用 Fabric。我不确定我应该把指令代码放在哪里 gradle 文件。

buildscript {
  repositories {
    maven { url 'https://maven.fabric.io/public' }
  }

  dependencies {
    // These docs use an open ended version so that our plugin
    // can be updated quickly in response to Android tooling updates

    // We recommend changing it to the latest version from our changelog:
    // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
    classpath 'io.fabric.tools:gradle:1.+'
  }
}

首先,这个 buildscript 块存在于项目的构建 gradle 中,而不是 android。由于我将这些放入父 gradle 文件,android gradle 文件无法识别库。

apply plugin: 'io.fabric'

repositories {
  maven { url 'https://maven.fabric.io/public' }
}



compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
    transitive = true;
  }

我只能再次将这些代码块添加到整个 gradle 文件中,如下所示。

project(":android") {
    apply plugin: "android"

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

    }
}

你能帮我在我的游戏中正确使用 Fabric 吗?

您应该将所有这些块放入您的应用级别 build.gradle 文件中。这是我的应用程序的 gradle 文件的示例,我在其中设置了带有 Crashlytics 并正在运行的 Fabric:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
    maven { url 'https://maven.fabric.io/public' }
} 
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.yourappid"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 6
        versionName "1.5"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
} 
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true;
    }
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    testCompile 'junit:junit:4.12'
}

确保您以类似的方式构建了 build.gradle 文件,并且在您同步项目后一切正常。

应在应用级别 gradle 添加结构依赖项(而非 项目级别 gradle )。还要记得在清单中添加 API 密钥。

因此您的应用级别 gradle 应如下所示:

buildscript {
    repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
  }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}
dependencies {
    compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
}

在 LibGDX 项目的根 build.gradle 中注入几行代码。

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url 'https://maven.fabric.io/public' }          //<- 1st
        jcenter()
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'io.fabric.tools:gradle:1.+'         // <- 2nd       
    }
}

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

    version = '1.0'
    ext {
        appName = "MyGDXTest"
        gdxVersion = '1.9.6'
        roboVMVersion = '2.3.0'
        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/" }
        maven { url 'https://maven.fabric.io/public' }      // <-- 3rd
    }
}

project(":android") {
    apply plugin: "android"
    apply plugin: 'io.fabric'                //<-- 4th

    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-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"

        compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {  //<--5th
            transitive = true;
        }    
    }
}

保存并 sync/refresh 您的 gradle 项目。

您还可以在应用级别 build.gradle 文件中注入 crashlytics 依赖项(第 5 块),默认情况下 LibGDX 项目中不存在依赖项标记。