Crashlytics Unity Plugin - export to Android Studio

落爺英雄遲暮 提交于 2019-12-08 00:55:40

问题


I integrated Crashlytic plugin into my Unity project. It work fine if I build an APK file directly from Unity.

But if I use option export to "Google Android Project" in Unity

-> Then open Android Studio, select "Import project (Eclipse ADT, Graddle, etc)"

-> Run

-> App crash on Launch with exception: "java.lang.ClassNotFoundException: io.fabric.unity.android.FabricApplication"

Here's my 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')
}

Anyone got this problem before?


回答1:


==TLDR==

You need to configure Unity's Android Build Settings to customize the build.gradle file and emit a proguard-user.txt file when it exports the project:

  1. Click the File Menu -> click Build Settings menu item -> select Android in the Build Settings window
  2. Change Build System to Gradle and check the Export Project box
  3. Click Player Settings button
  4. Click Publish Settings in the Player Settings inspector tab
  5. Check both Custom Gradle Template and User Proguard File boxes under the Build section
  6. Select Proguard for Release under the Minify section
  7. Edit the Assets/Plugins/Android/mainTemplate.gradle and Assets/Plugins/Android/proguard-user.txt files to look like the ones below.

mainTemplate.gradle should have this in the buildTypes release section:

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

proguard-user.txt looks like this:

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

==Specifics for my situation== I was having a similar problem trying to export the project using the Gradle Build System in Unity. The terminology is a bit different, but it might be because I'm on a different Unity version (2017.1.1f1).

Also, rather than trying to import to Android Studio I'm trying to build from the command line using Gradle.

I was getting the following error while trying to do a gradlew assembleRelease, which is similar, but different:

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

I noticed the release buildType in the root build.gradle was defined as follows:

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

In a blog article from Kongregate on Unity, where they talk about the problems with exporting your Android project, they mention (take note of the last bolded sentence):

We specified two ProGuard configuration files – the standard one that’s included with the Android SDK (proguard-android.txt) and one which is exported with the Unity Project as of Unity 5.4, (proguard-unity.txt). You almost certainly need to maintain another ProGuard configuration file with rules specifying which classes and methods need to be kept for the plugins your game uses.

Thankfully, @MikeBonnell pointed me to the Fabric Android Crashlytics integration docs which tells you how to exclude Crashlytics from minification in your ProGuard file and that you must have minifyEnabled: true in your build.gradle file if you're using Gradle.



来源:https://stackoverflow.com/questions/43774062/crashlytics-unity-plugin-export-to-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!