崩溃分析工具Crashlytics Unity插件 - 导出到Android Studio

3

我将Crashlytics插件集成到了我的Unity项目中。如果我直接从Unity构建APK文件,则运行正常。

但是,如果我在Unity中使用“导出到Google Android项目”选项

-> 然后打开Android Studio,选择“导入项目(Eclipse ADT、Graddle等)”

-> 运行

-> 应用启动时崩溃,并抛出异常:“java.lang.ClassNotFoundException: io.fabric.unity.android.FabricApplication”

这是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "25.0.2"

defaultConfig {
    applicationId "com.xct.poke.khongchien"
    minSdkVersion 15
    targetSdkVersion 25
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
}

dependencies {
compile project(':answers')
compile project(':an_social')
compile project(':beta')
compile project(':common')
compile project(':crashlytics')
compile project(':crashlyticswrapper')
compile project(':fabric')
compile project(':fabricinit')
compile project(':facebookandroidsdk470')
compile project(':facebookandroidwrapperrelease')
compile project(':googleAIDL')
compile project(':googlePlay')
compile project(':unityadsrelease')
compile 'com.android.support:support-v4:23.4.0'
compile 'com.google.android.gms:play-services-analytics:9.0.0'
compile 'com.google.android.gms:play-services-auth:9.0.0'
compile files('libs/bolts-android-1.2.0.jar')
compile files('libs/EtceteraPlugin.jar')
compile files('libs/mobilenativepopups.jar')
compile files('libs/Prime31UnityActivity.jar')
compile files('libs/unity-classes.jar')
compile files('libs/WebViewPlugin.jar')
}

有人之前遇到过这个问题吗?

1
Fabric 的 Mike 在这里。那是你的应用程序 build.gradle 还是项目的 build.gradle? - Mike Bonnell
嗨,Mike,这是我的应用程序的build.gradle文件,类io.fabric.unity.android.FabricApplication应该在项目“fabricinit”中。 - Son Le
@MikeBonnell,你有关于这个的任何信息吗? - plainjimbo
@plainjimbo,很遗憾我无法复现这个问题。你能否分享更多关于你的实例的细节? - Mike Bonnell
1个回答

11

==TLDR==

您需要配置Unity的Android Build Settings,以自定义build.gradle文件并在导出项目时生成proguard-user.txt文件:

  1. 单击文件菜单-> 单击构建设置菜单项 -> 在构建设置窗口中选择Android enter image description here
  2. 构建系统更改为Gradle并选中导出项目
  3. 单击Player Settings按钮
  4. Player Settings检查器选项卡中单击Publish Settings enter image description here
  5. 构建部分下勾选Custom Gradle TemplateUser Proguard File
  6. Minify部分下选择Proguard Release
  7. 编辑Assets/Plugins/Android/mainTemplate.gradleAssets/Plugins/Android/proguard-user.txt文件使其与下面的文件类似。

mainTemplate.gradle应该在buildTypesrelease部分中添加以下内容:

release {
  minifyEnabled true
  useProguard true
  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt', 'proguard-user.txt'
}

proguard-user.txt看起来像这样:

-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception
-keep class com.crashlytics.** { *; }
-dontwarn com.crashlytics.**

==我的具体情况== 我尝试使用Unity的Gradle构建系统导出项目时遇到了类似的问题。术语略有不同,可能是因为我使用的是不同版本的Unity(2017.1.1f1)。

此外,我不是尝试在Android Studio中导入,而是尝试使用Gradle从命令行构建。

我在尝试执行gradlew assembleRelease时遇到了以下错误,它类似但又不同:

java.lang.RuntimeException: Unable to create application io.fabric.unity.android.FabricApplication: io.fabric.unity.android.b: Could not instantiate kits
....
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.crashlytics.android.Crashlytics" on path: DexPathList[[zip file "..."]]

我注意到根目录下的build.gradle文件中,release buildType的定义如下:

    release {
      minifyEnabled true
      useProguard true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
    }

Kongregate on Unity的一篇博客文章中,他们谈到了导出 Android 项目时遇到的问题。他们提到了两个 ProGuard 配置文件——一个是包含在 Android SDK 中的标准文件(proguard-android.txt),另一个是从 Unity 5.4 开始随 Unity 项目一起导出的文件(proguard-unity.txt)。你几乎肯定需要维护另一个 ProGuard 配置文件,其中规定了游戏使用的插件需要保留哪些类和方法。幸运的是,@MikeBonnell 指向了我 Fabric Android Crashlytics 集成文档,告诉我如何在ProGuard文件中排除Crashlytics的混淆,并且如果你使用Gradle,必须在build.gradle文件中有minifyEnabled: true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接