Google firebase analytics plugin for unity

2019-01-24 21:37发布

Google recently launched firebase analytics for iOS and android, which is great and I want to use it in my android game made in unity3d 5.4b19. I tried to build a plugin for this in android studio following this link. I have successfully build the jar, imported in unity, and build an apk file. But when I try to initialize firebase analytics I get following errors

AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/analytics/FirebaseAnalytics;

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/analytics/FirebaseAnalytics;

Here is my main activity class from android studio

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import com.unity3d.player.UnityPlayerActivity;
import com.google.firebase.analytics.FirebaseAnalytics;

public class MainActivity extends UnityPlayerActivity {
    private FirebaseAnalytics mFirebaseAnalytics;

    public void init() {
        // [START shared_app_measurement]
        // Obtain the FirebaseAnalytics instance.
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        // [END shared_app_measurement]
    }

    public void setUserProperty(String Property, String value) {
        // [START user_property]
        mFirebaseAnalytics.setUserProperty(Property, value);
        // [END user_property]
    }

    public void logCustomEvent(MenuItem item) {
        // [START custom_event]
        Bundle params = new Bundle();
        params.putString("image_name", "name");
        params.putString("full_text", "text");
        mFirebaseAnalytics.logEvent("share_image", params);
        // [END custom_event]
    }

    public void logEvent(String id, String name, String type) {
        // [START image_view_event]
        Bundle bundle = new Bundle();
        bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, type);
        mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
        // [END image_view_event]
    }

    public void shareText(String subject, String body) {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
}

I understand that this is not working because it cannot find the firebase classes. I don't know how to include these files in my project. I have tried to find jar files for firebase analytics, but I couldn't find it.

2条回答
放荡不羁爱自由
2楼-- · 2019-01-24 21:58

You need to add this files to the plugin/android folder: http://joxi.ru/Q2KeQD7C3zdYrj . after that your sdk will work fine.

P.S. I dont understand why log event doesn't work, maybe need to wait. I think, google json file will setting the app, but package name of sdk module and unity app package name need to equal

查看更多
放荡不羁爱自由
3楼-- · 2019-01-24 22:12

Update 31/11/16

Now you can do this from this unitypackage

I extracted all required jar file from SDK folder. It is all there inside Extra folder. Now it is working perfectly fine.

Update: Few people have requested me to post the whole process. So here is the overview of how I got firebase working in the unity. These are basically snapshots I have taken during the process for record.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Download jar files from here if have trouble finding them

I hope this much information will be enough for you to get started. If you need any other information then please comment.

查看更多
登录 后发表回答