简体   繁体   中英

Crashlytics Unity Plugin - export to Android Studio

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?

==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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM