Distributing and installing apk’s using MacOS and Eclipse

Once you’ve developed an android app, you’ll want to send it to beta testers and end customers. This is actually pretty painless once you know how.

Create a keystore file.

To make an apk file that they can install, you have to sign it, and in order to sign it, you first have to generate a keystore. Luckily the android sdk comes with the tool to do this. You can generate the keystore file with the keytool app:

    keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -validity 10000

Replace alias_name with the name you want to use for the key. The first 8 characters are used.

You will be asked for a password. This will be requested whenever you sign an apk file with this keystore, so be sure to remember it! It will ask a number of questions after. I’m not sure which ones are required, but since it helps your customers to know how to contact you, its best to put in as much information as you’re comfortable with. I generally use the same password for the alias.

This will a file called my-release-key.keystore which you will use when signing the app.

Creating a signed apk file.

Now that you have a keystore file, you can create a signed apk file. To do this, right click on the base of your application’s tree in Package Explorer, select the ‘Android Tools’ menu, and select ‘Export Signed Application Package…’ Select after confirming the project. Click on ‘Use Existing keystore’, and Browse to where you generated the keystore earlier, and select the keystore. You will need to enter the password you used for the keystore file, and then again for the alias. Finally, it will ask you for the path the the apk file. Once you’ve selected where you want it to go, click on ‘Finish’, and the signed apk is saved for you!

Distributing/installing the apk file.

This is the easy part. You can email this apk file, and android devices will recognize it as being installable. You can put it on a web server, and browse for it using the android device. If there’s a way the android device can see the file, then you should be able to install from that.

The device will probably ask you to change the security setting to allow you to install the apk because its not a known source.

Playing a full screen video in an android app the easy way.

The Android sdk has a MediaPlayer api that should make it easy to play videos. There is, however, a much easier way.

VideoView makes it super simple to play videos. Here’s how we use it.

The XML files

First, add a VideoView to the activities xml file. We’ll call it videoplayer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <VideoView
        android:id="@+id/myvideoview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
    />
</LinearLayout>

We set the LinearLayout to horizontal orientation in this case because my videos are all landscape. We set the layout’s to “fill_parent” so our VideoView takes up the whole parents view. We don’t want to stretch it out of proportion, but we do want the video centered, so we set the layout_gravity to “center”.

We also need to update the AndroidManifest.xml file to make sure our VideoPlayer activity is full screen (using theme), stays in landscape orientation (using screenOrientation) and only has one instance, so we don’t get a bunch of video players running. (launchMode)

    <activity
        android:name=".VideoPlayer" 
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="landscape" ></activity>

Calling code

When we want to show a full screen video, we’ll do it by creating the intent for the VideoPlayer activity, grabbing the video resource id, pass it to the VideoPlayer activity, and start the activity:

private void playVideo(String resourceName) {
    Intent videoPlaybackActivity = new Intent(this, VideoPlayer.class);
    int res=this.getResources().getIdentifier(resourceName, "raw", getPackageName());
    videoPlaybackActivity.putExtra("fileRes", res);
    startActivity(videoPlaybackActivity);
}

The VideoPlayer Activity code

Now we can get to the VideoPlayer activity code. It turns out to be not that complex. Here’s VideoPlayer.java:

public class VideoPlayer extends Activity implements OnCompletionListener {
		   
    private VideoView mVV;
		 
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);

        setContentView(R.layout.videoplayer);

        int fileRes=0;     
        Bundle e = getIntent().getExtras();
        if (e!=null) {
            fileRes = e.getInt("fileRes");
        }

        mVV = (VideoView)findViewById(R.id.myvideoview);
        mVV.setOnCompletionListener(this);
        mVV.setOnPreparedListener(this);
        mVV.setOnTouchListener(this);

        if (!playFileRes(fileRes)) return;

        mVV.start();
    }
		 
    private boolean playFileRes(int fileRes) {
        if (fileRes==0) {
            stopPlaying();
            return false;
        } else {
            mVV.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + fileRes));
            return true;
        }
    }

    public void stopPlaying() {
        mVV.stopPlayback();
        this.finish();		    	
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        finish();
    }
}

It’s pretty simple. We set the VideoView’s video URI to point to the resource id passed (if it exists), and start playing. If it doesn’t exist, we exit.

Playing new video files

Since we added the singleLaunch taskMode to the activity, that means that if we attempt to launch the VideoPlayer activity while its still running, onCreate() won’t be called, so it won’t know that its supposed to be playing a different video. In this case, the onNewIntent() function will be called with the new intent. We simply parse the extras bundle to get the file resource, if any, and it will play the new file:

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        int fileRes = 0;
        Bundle e = getIntent().getExtras();
        if (e != null) {
            fileRes = e.getInt("fileRes");
        }
        playFileRes(fileRes);
    }

Making the video loop

We would also like our video to loop until we stop it. The best place to do this is once the MediaPlayer has been prepared. We add OnPreparedListener to the implements list of the VideoPlayer activity, and a simple onPrepared function that simply sets our MediaPlayer instance to looping:

public class VideoPlayer extends Activity implements OnCompletionListener,OnPreparedListener {
...
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
...
}

Stopping at a Touch

We would also like to be able to stop the video with a tap on the screen, so we add OnTouchListener to the implements list of the VideoPlayer activity, and a simple onTouch function that simply calls our stopPlaying() function:

public class VideoPlayer extends Activity implements OnCompletionListener,OnPreparedListener,OnTouchListener {
...
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        stopPlaying();
        return true;
    }
...
}

Complete Java Source

Here’s the complete source, in case you missed any additions:

public class VideoPlayer extends Activity implements OnCompletionListener,OnPreparedListener,OnTouchListener {
		   
    private VideoView mVV;
		 
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);

        setContentView(R.layout.videoplayer);

        int fileRes=0;     
        Bundle e = getIntent().getExtras();
        if (e!=null) {
            fileRes = e.getInt("fileRes");
        }

        mVV = (VideoView)findViewById(R.id.myvideoview);
        mVV.setOnCompletionListener(this);
        mVV.setOnPreparedListener(this);
        mVV.setOnTouchListener(this);

        if (!playFileRes(fileRes)) return;

        mVV.start();
    }
		 
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        int fileRes = 0;
        Bundle e = getIntent().getExtras();
        if (e != null) {
            fileRes = e.getInt("fileRes");
        }
        playFileRes(fileRes);
    }
		    
    private boolean playFileRes(int fileRes) {
        if (fileRes==0) {
            stopPlaying();
            return false;
        } else {
            mVV.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + fileRes));
            return true;
        }
    }

    public void stopPlaying() {
        mVV.stopPlayback();
        this.finish();		    	
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        finish();
    }
			
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        stopPlaying();
        return true;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
}