When I found myself having to split an android application (APK file) to allow it to be uploaded onto the Google Play store, I found a serious lack of current information and much misdirection possibly due to the recent changes within Unity and the actual Play store itself.
So here is a rough guide that works with unity 4.3 or up, in this example I am only catering for devices running android 4.0 ICS or up ( API level 16), not quite sure what to do with earlier Android API versions at this very moment.
STEP -1
Firstly ignore the asset store OBB plug-in (it was created for earlier versions of unity, or possibly for catering for earlier Android API’s) going down this route will burn hours of your life.
STEP 1
If you haven’t done so already go to EDIT>PROJECT SETTINGS>PLAYER SETTINGS select the android role out and go to the PUBLISHING SETTINGS roll-out and do the following:-
1) Create a new keystore by selecting a keystore name and password (confirm the password)
2) Select “Create a new key” under Key Alias
3) A new window opens; enter the necessary information.
4) Select the newly created key.
n.b. Remember the passwords; they are not stored with the project (for security reasons), every time you load the project you will need to enter this password from now on.
5) Tick Split Application Binary at the bottom of this roll out!
STEP 2
Again if you haven’t already done so create a primary loading scene with very little content, this will allow unity to split most of your app data off to the OBB data/zip file and keep the APK file size small. As an example I created a scene called Android Loader, so that I could exclude when I build the same project for IOS, I also put a tiny script in there to load the first real app scene/level which would be in the split OBB, this could be as simple as:-
Application.LoadLevel(“NameOfYourFirstAppScene);
However to expand on that I used the below, if you have Streaming Asset folder files to move (see my other post re:- vuforia) :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using UnityEngine; using System.Collections; using System.IO; public class AndroidSplitLoadFirstScene: MonoBehaviour { //Only use in Preloader Scene for android split APK private string nextScene = "YourSceneName"; public Texture2D background; public GUISkin mySkin; private bool obbisok=false; void Update() { if (Application.dataPath.Contains(".obb")&&!obbisok) { obbisok=true; Application.LoadLevel(nextScene); // If you need to unpack anything could do it here // StartCoroutine(CheckSetUp()); } } void OnGUI() { GUI.skin = mySkin; GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height)); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label(background,GUILayout.Width(background.width),GUILayout.Height(background.height)); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (!obbisok) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("There is an installation error with this application, Please re-install!"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } GUILayout.FlexibleSpace(); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndArea(); } } |
STEP 3
When your done with your scene loader and happy with your app, build the android split apk, it will create two files:-
YourAPKName.apk and YourAPKName.main.obb
Rename the .obb file only to main. (your version number from settings) . (your bundle identifier i.e: com.companyname.appname) . obb
For instance lets say my version number was 4 and and bundle name was com.jonmartinmedia.game my obb file name became:
main.4.com.jonmartinmedia.game.obb
STEP 4
Upload to google play as an Alpha tester (don’t miss this step!), you will need to set yourself up as an alpha tester (follow other instructions for this as otherwise this tutorial would be very very long indeed), the google play developer console will allow you to upload an apk, go ahead and do this, afterwards it should ask you if you want upload and .obb expansion file etc.. however if it doesn’t re-upload the apk for a second time then it will give you the option after the apk is uploaded for the second time.
After you have successfully uploaded your apk and obb file, you can download to your android test device after a short wait (delete any old test versions and install using the alpha testing account (again follow google play instructions for this)).
STEP 5
Hopefully if all is well,and you’ve tested on your device, you can then promote the alpha version to a published version in developer console, and make it all live!
However:
You may want to make some adjustments to the app, so rather than upload a new split apk version every time you can now, just replace the obb manually located on the android device in Android/obb/YOURBUNDLEID/main.VER.YOURBUNDLEID.obb being sure your obb name is renamed exactly the same and then copy the apk file across and re-install as usual (* DO NOT CHANGE THE VERSION NUMBER WHILST PERFORMING THIS TESTING, KEEP IT ALL THE SAME!). Again when your happy with it all you can change the version number and re-upload to the alpha console repeating steps 3 and 4.
And that’s it, well at least the way it worked for me and I wasted several hours if not days trying to get it all working by following the wrong information, so I hope this short guide helps you and I will endeavour to keep it up to date as such.
Hi,
I tried to test manually the .obb file. I copied the .obb file to Android/obb/com.companyname.appname/main.4.com.jonmartinmedia.game.obb.
But my Application.dataPath is /mnt/asec/com.company.app-1/pkg.apk
Is there a way to test the .obb file manually?
Best regards,
Gabor
Hi Gabor,
As far as i’m aware the first time you cannot test manual, but after using beta/alpha once and as long as you don’t change the bundle id or version number, you can then test manually as many times as you wish as long as those details remain the same.
—
However below is important:-
—
Before splitting as an obb I take it everything works and runs as an normal android apk on your device, do this first to make sure.
—
Then once you have built a split apk with obb file.Name he obb as described in the article do not copy “main.4.com.jonmartinmedia.game.obb” that is an example name I provided and will only work for me, you must rename your obb file to your match your own app details for instance:-main.1.com.company.app-1.obb (the number 1 replaced by your version number, and com.company.app-1 with your bundle id)
—
Secondly you must upload your apk to google play the first time to test as described in the article or each time you change the version number, To do this add as an alpha/beta in the google play developer console, upload both the apk and RENAMED obb file and then to do your first test install on your device from the beta web address you should get provided when you added the beta on google play.
—
Thirdly once you have installed from the play store, it will put your apk and obb files in the right places, apk somewhere internal or external (this is irrelevant) and most importantly the obb file in a directory such as mnt/android/obb/com.compny.app-1/ main.1.com.company.app-1.obb.
—
Once you have done the above, you can then edit your apk and repeat this last step as many time as you wish, build as a split obb and apk file, rename the obb file, copy the obb file over the existing file in the mnt/android/obb folder, and the install the apk file as nomal by just adding to your device directory somewhere and clicking on it from a folder/file/directory explorer on the device.
—
Hope that helps with any confusion.
Hi,
Thanks for your fast reply. It`s resolved my problems.
I’m in the middle of this problem atm and your tutorial is helpful. My understanding is that the plugin is needed for certain devices and/or android versions that don’t download the .obb automatically. This may be a legacy problem and my game is aimed at newer devices so I could well be wasting my time.
What surprises me is that Unity doesn’t rename the .obb to the version and package name. That is just strange.
Hey Jon! Good stuff. Did you ever get around to figuring out the streamingAssets folder copy with vueforia.
I’m currently trying out some code from the vuforia dev threads but I hit a wall with the app crashing.
Here is that thread:
https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/dataset-does-not-exist-error-when-using-unity-4-android
Thanks so much!
Hi yup, thanks for pushing me for it, the code I use is now in this post http://jon-martin.com/?p=597
Dear Jon, great work, thanks for the tutorial!
however, it is said in the google play docs that every app needs to provide a custom download action in case the app store download didn’t work. how did you solve this problem? or is it not neccessary anymore? thanks in advance!
Yup true it does say that in various places, in simple terms if the obb is not found I rather ashamedly – something I will fix if I am definately wrong – tell the user their was a problem with their download of the app to uninstall and re-download the app from google. The option was and if still required to host the obb on your own site also and push to the correct location on the device if not found, you may need to write a new plugin for this though and it could be tricky the file system access has become a little bit off an on/off affair with manifests/permisisions over the past year to my knowledge, that is what the original unity plugin did and possibly why it may no longer really work, plus there would be a ton of possible security implications, hence I went with the easier, ‘opt out’, drop out, need more information, Re-download this, so far had no bad feedback on this being an issue from any of my app subscribers thankfully! let me know how you get on or if you find out otherwise and I will also update this page if I find out! Sorry for the vagueness, but its been a tricky year for us indy devs with IOS 8 upgrades and now to face 64 bit upgrade challenges! Cheers Jon
ic thank you very much for your reply. i think it will save me hours of work
cheers wolfgang
Looking around yet again, although personally experienced no problems, at the bottom of the thread found here:- http://forum.unity3d.com/threads/help-please-question-about-google-play-obb-downloader.239456/ florian from unity tech replies last September 2014 that it still does require a downloader in case a) some devices (presume older) not automatically download the obb with the apk and b) the user deletes or obb file gets removed from the device. I don’t see why forcing them to re download in the second instance is a problem, as it’s probably their own action that caused the missing obb in the first place, however the first problem does greatly depend on whether these “some devices” are just older lower API devices or are current API +4.0.3 devices information is sparse, messy and unclean but I can see that a back up plan would be usefull in high end products. The work around however would be then to check if the app isn’t from the obb location and is android device re-download it through the specific google API’s, this would require a plug-in with the correct android libs, and setting specific manifest permissions. If I get time to approach this plug-in patch in a clean way (ideally all mono c# no plug-in would be great butunlikely) I will update, but after having headaches with IOS 64 bit plugins and concerned about android 64bit on the horizon I am a little reticent to provide just now, there are plenty plug-in examples out there (each with their own issues as such), I will most likely stick with this method for now until I see a device not installing the obb or a complaint from an end user, however understand the UX would be better if this was also catered for.
Great stuff. Thank you so much for sharing this, and for saving others all the heartache and trouble you’ve had to go through yourself. It’s much appreciated.
To clarify, for others like me who stumble here after reading so many older and contradictory tutorials… it’s now become ridiculously easy to implement. There’s absolutely no need for the package in the Unity store and you don’t need to use anything complicated like a Google API key.
The code needed by the preloader can even be further reduced (I just added it to my existing splash screen.):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO;
public class AndroidSplitLoadFirstScene: MonoBehaviour {
public string nextScene = “YourSceneName”;
public Text warningText; // add a canvas and Text object to your scene
private bool obbisok=false;
void Update() {
if (Application.dataPath.Contains(“.obb”)&&!obbisok) {
obbisok=true;
Application.LoadLevel(nextScene);
}
}
void OnGUI()
{
if (!obbisok) {
warningText.text = “There is an installation error with this application, Please re-install!”;
}
}
}
All it does is check that the .obb file was properly loaded *by the Google Play Store* – this is the bit I didn’t understand at first: it’s the play store that handles the loading of the extended asset package (the .obb) You don’t need to do anything clever in your Unity scripts, like displaying a loading bar. All this happens when the user clicks ‘install’ on the app’s Google Play Store page.
then, as explained by Jon:
1. in Unity’s Player Settings just tick Split Application Binary.
2. Rename the .obb file as per the instructions above ( e.g. main.4.com.companyname.appname.obb )
3. Upload in the Google Developer Console.
ps. sorry my post got cutoff by the comment system.
3. Upload in the Google Developer Console.
-> First the .apk then the .obb As jon warned, I had to re-upload the .apk before being given the option to add an expansion file (the .obb)