Thursday, May 28, 2015

Run Unity-generated android APK file in device, with FB app installed (Part 2)

From previous post, now we can proceed to test on the Android device. If you are directly generated the APK, then you are wrong (partially. If your device does not has Facebook app, then it works fine mostly, but it'll not work if other way round)

1) Make sure you have Android SDK installed. Go to Unity Editor, click on "Build Settings"


2) Add the interactive scene, and click "Android" platform, then click "Player Settings ...". Configure the "Company Name", "Product Name, and "bundle identifier"


3) Click "Build" button in "Build Settings" to generate APK file.

4) Copy the APK file to your device, and install it. Some of the tools (eg. "ASTRO File Manager" you might need it. You need to enable third-party apps installation on android phones too.

5) After installation is done, launch the app you installed. Click the "FB.Init" and follow by "Login". Everything should be fine.




6) But if you install Facebook app in your device and run the sample interactive apk. This time will fail.

7) To generate the debug log (idea is from here; You might need these tools: adb, logcat), connect your device to laptop with cable, and run this in command prompt:

$ adb logcat -d > logcat.txt

then start the app in device again. Repeat the login process until you hit the login error. Then press Ctrl-C to stop the logging in command prompt.

8) I saw this message "Login cancelled by player" in the log captured. By printing the keyHash sent to server, then I realized it was caused by missing keyhash.

9) To run in Andoid, you need to get a keyhash. To get a keyhash,  you need OpenSSL.
If you are on Windows, you need to install OpenSSL. If you use 64-bit Windows, download and install "Win64 OpenSSL v1.0.2a". If you use 32-bit Windows, download and install "Win32 OpenSSL v1.0.2a". Make sure you configure the "PATH" variable in "Environment Variables" too. I get the idea here.
(I was reported this step is not needed in MacOS.)

10) Restart the Unity Editor, Click on "Facebook" menu. This time you go to: "Android Build Facebook Settings". You see the "Debug Android Key Hash" automatically now.



*If you found out you see nothing in the "Debug Android Key Hash" then you probably miss the above step (9), it is needed, as in GetKeyHash() function in "~\Assets\Facebook\Editor\android\FacebookAndroidUtil.cs"(see more here)


* Execute the command can see the same result too(you can use OpenSSL version 0.9.8h too):

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | C:\openssl-0.9.8h-1-bin\bin\openssl.exe sha1 -binary | C:\openssl-0.9.8h-1-bin\bin\openssl.exe base64

default password for debugging purpose is: android



11) This time, I built the APK file and run again in device. Still failed. From the debug log, this time I get:

OnLoginComplete({"cancelled":true})

12) I have to go to Facebook developer page again (the one you created early to get the AppId) to add configuration for Android. (got the idea here) Go to "Settings" and click "Add Platform".


13) Choose "Android"


14) Copy all the values "Package Name", "Class Name" and "Debug Android Key Hash" from Unity Editor and paste in the new "Android" setting.


15) Build the APK again and run from device. This time everything is OK, even with Facebook app installed. (this time the the login pop up will be from Facebook app)



Phew! Finally done.






2 comments:

Blogger said...

Ever wanted to get free Twitter Followers?
Did you know you can get them AUTOMATICALLY & ABSOLUTELY FREE by registering on Like 4 Like?

Blogger said...

eToro is the ultimate forex broker for new and advanced traders.