Skip to content

Android setup

Louis Beltramo edited this page Jun 8, 2013 · 2 revisions
  1. Create a new project:
cd workspace
nme create project "fr.hyperfiction.fbexample" "Hyperfiction"
  1. Copy the extension folder:
git clone git://github.com/hyperfiction/HypFacebook.git

Now you have:

    workspace/
        Fbexample/
            Assets/
                nme.svg
            Source/
                Main.hx
            Fbexample.hxproj
            project.nmml
        HypFacebook/
           ...
  1. Go to your new project folder and test if you compile
cd Fbexample
nme test android -debug

Now you have:

    workspace/
        Fbexample/
            Assets/
                nme.svg
            Export/
                android/
                    bin/
                        AndroidManifest.xml
                        build.xml
                        src/
                    haxe/
                    obj/
            Source/
                Main.hx
            Fbexample.hxproj
            project.nmml
        HypFacebook/
           ...
  1. Create a template folder:
cd workspace/Fbexample
mkdir -p Templates/android/res

Now you have:

    workspace/
        Fbexample/
            Assets/
                nme.svg
            Export/
                android/
                    bin/
                        AndroidManifest.xml
                        build.xml
                        src/
                    haxe/
                    obj/
            Source/
                Main.hx
            Templates/
                android/
                    res/
            Fbexample.hxproj
            project.nmml
        HypFacebook/
           ...
  1. Copy the AndroidManifest.xml to the template folder:
cd workspace/Fbexample
cp  Export/android/bin/AndroidManifest.xml Templates/android

Now you have:

    workspace/
        Fbexample/
            Assets/
                nme.svg
            Export/
                android/
                    bin/
                        AndroidManifest.xml
                        build.xml
                        src/
                    haxe/
                    obj/
            Source/
                Main.hx
            Templates/
                android/
                    res/
                    AndroidManifest.xml
            Fbexample.hxproj
            project.nmml
        HypFacebook/
           ...
  1. Edit your project.nmml file:
cd workspace/Fbexample
vim project.nmml

Add this lines:

<include path="../HypFacebook" />
<template path="Templates/android/AndroidManifest.xml" rename="AndroidManifest.xml"/>
<template path="../HypFacebook/templates/android/MainActivity.java"
   rename="src/fr/hyperfiction/fbexample/MainActivity.java"/>
  1. Edit the AndroidManifest.xml:
cd workspace/Fbexample/Templates/android
vim  AndroiManifest.xml

Add this line:

<activity   android:name="com.facebook.LoginActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="::APP_TITLE::" />

Now you have:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" android:versionCode="13" android:versionName="1.0.0" package="fr.hyperfiction.fbexample">

	<application android:label="Fbexample" android:debuggable="true" android:icon="@drawable/icon">
		
		<activity   android:name="com.facebook.LoginActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="::APP_TITLE::" />
		
		<activity android:name="MainActivity" android:label="Fbexample" android:configChanges="keyboard|keyboardHidden|orientation">
			
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
				<category android:name="ouya.intent.category.GAME"/>
			</intent-filter>
			
		</activity>
		
	</application>

	<uses-sdk android:minSdkVersion="8"/>
	
	<uses-permission android:name="android.permission.WAKE_LOCK" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.VIBRATE" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	
</manifest> 
  1. Start coding:
cd workspace/Fbexample/Source
vim Main.hx
import nme.display.Sprite;
import fr.hyperfiction.HypFacebook;

class Main extends Sprite {
	
	var fb  : HypFacebook;
	
	public function new () {
		super ();
		connectToFacebook( );
	}
	
	function connectToFacebook( ) : Void {
        	fb = new HypFacebook( "" );
        	var session_is_valid = fb.connect( false ); // false to disallow login UI

	        if( session_is_valid ) {
	            _doFacebookStuff( );
	        } else {
	            fb.addEventListener( HypFacebookEvent.OPENED, _onFbOpened );
	            fb.connect( true ); // true to allow login UI
	        }
    	}

	function _onFbOpened( _ ) {
        	fb.removeEventListener( HypFacebookEvent.OPENED, _onFbOpened );
        	_doFacebookStuff( );
        }

	function _doFacebookStuff( ) {
        	fb.addEventListener( HypFacebookRequestEvent.GRAPH_REQUEST_RESULTS, _onGraphResults );
        	fb.call( GRAPH_REQUEST("/me") );
        }

	function _onGraphResults( event : HypFacebookRequestEvent ) : Void {
		trace( 'sResult:'+event.sResult );
	}
	
}
  1. Test to get the PXR:
nme test android -debug

The facebook login won't work, just copy your app package name and your pxr from the ouput:

I/trace   (32187): nme_key_hash : +fr.hyperfiction.fbexample 
I/trace   (32187): PXR : pRqGr00vyKadfeNtfCttxWWk8JflaJk=
  1. You should now create or update your facebook app:

    • Tick "Native Android App"
    • Paste the package name: fr.hyperfiction.fbexample
    • Paste the PXR in "Key Hashes": pRqGr00vyKadfeNtfCttxWWk8JflaJk=
    • Save Changes
  2. Update your code with your app id:

fb = new HypFacebook( "6233362678093339" );
  1. Test again:
nme test android -debug

It works ;)

  1. If it doesn't works:

Be careful when testing your app. For some user having the Facebook app installed prevents them to test their app with sandbox mode on. Try with a different permission (admin/developer/tester...) or disable the sandbox mode.

The first time it will open a web view or directly ask you the basic permissions if you have the Fb app installed. Then it will trace the result of the Graph request. The second time it will directly trace the result as it cache the Facebook token.

Clone this wiki locally