HOW TOS AND NOTES ON ANDROID PROBLEMS FOR BEGINNERS Topics covered below: Build problems Emulator - problems and how to install apps from the net GUI - problems and using perspectives Misc - pasting samples from the net into code as separate lines, getting text to look right, pasting to xml code without loads of errors Changing project path - sounds easy but isn't (surprise!) Debugging - how to get more useful messages, how to output large volumes of debug data as a csv file Debug on real phone Prepare for Publishing - Signing and stuff Different but similar programs - changing application or package name Update checking Android market warning and apk details - READ THIS BEFORE YOU UPLOAD YOUR APP TO ANDROID MARKET Getting apk off phone Build problems ************** Apparent code errors in resources (R.) you can't find a reason for: .net users are used to sorting errors before compiling. If you are new to java this may be due to not understanding the java model; it is not enough to put correct references in the .xml files, these have to get built in the R.java file before the source files see them. Ignore the errors and do a build. Any errors you see after that should be real ones. References to R show as error after cut and paste Auto complete may cause alterations to case. For example, if you have a src file called ViewIdent.java but a layout of viewident.xml, references to R.layout.viewident will get changed to R.layout.ViewIdent. Java is case sensitive Cannot resolve R.layout etc Import Android.R only if it's an android thing. Remove to refer to your own resource. R.layout.xxxx.yyyy refers to yyy in layout/xxx.xml etc Done above but still keep getting layout etc cannot be resolved Add another resource, compile then remove it again afterwards Errors in log sometimes seem to prevent rebuild Delete the error log and the problems report (see window views) before you retry Changed files do not always seem to save before build Explicitly save changed files before build to remove errors Internal error a) Have no other projects but do build all instead of build b) Sometimes just ignore and run anyway Failed to load properties file Clean and build path error Have you got the correct project selected in left hand panel? project missing folder gen Try: a) Clean and build, Project -> clean b) Edit any .java source file, save, then clean and build project path errors. Try: a) and b) as last c) Edit the R.java source file, save and clean d) Right click and delete "gen" folder in your Package Explorer, save and clean No run just run last launched Have you selected a project in explorer? If you only started Eclipse in the last 100 years just wait few more centuries and it may get there. It may be doing an Android update - see bottom right. Try cleaning error log and clean/build/select If all else fails File->import->general->existing projects into workspace and get back working copy from backup Summary: If you can't see a rational reason for an error: Delete errors and problems in logs Do save all files Mod any source file .Java for no reason and save Mod R.Java for no reason and save Delete gen directory for no reason Clean and buld all projects, not just yours run If that dont work, add pointless xml or java files, do all above, and remove again If that dont work, close Eclipse and open again Always leave it lots of time because it is slow and may not appear to work because it is still fannying around doing something else. Repeat all above several times, bizarre though it may sound, just doing the same things over and over again often sorts the "problems". Seriously. Try chanting the incantation from The Army of Darkness, "Klaatu barada nikto" I expect that helps too. Emulator ******** attribute minsdk lower than project target API or does not run on emulator Ensure emulator emulates an equal or later device than build - use 2.1 for emulator initially Most current Android phones use 2.1 App says success but does not launch in emulator and ages later get Internal error Sometimes need to click on emulator menu and navigate to app on 2nd and subsequent runs - no idea why 2.2 doesn't work Use 2.1 Proably instal problem. Most current phones only seem to use 2.1 anyway. Missing xxx APK Run a project like Hello Android that does work - then go back to original An app will only reinstall if the code has changed, otherwise it simply opens the version on the emulator. Changes to raw resources like images will not be detected. If you change a raw resource then make a pointless change to some java just to get it it reinstall. Install freeware apps on emulator. Its handy to see how other apps operate and there are two approaches: 1) Enable the android market in the emulator and use like a normal phone, see: http://www.howtogeek.com/howto/21862/how-to-enable-the-android-market-in-the-google-android-emulator/ 2) If those don't work for any reason, download apks to your PC and then load to emulator a) Download to your PC as apk, see http://www.mascobz.com/3-places-to-download-android-apk/ for useful links: b) Run this batch file to install, changing files\paths to suit obviously: cd C:\Utils\android-sdk_r06-windows\android-sdk-windows\tools adb install C:\Android\eclipse\offline\Shuffle_1.6.3.apk pause If you have more than one emulator defined - Run/Configurations/android applications/your app/Target tab/manual. It will then ask which you want each time you run. Similar with debug. You can then select which AVD to use or set it to manual so it asks you each time you run the app. GUI *** Crappy perspectives When you have the layout you want always save the perspective as, cos it can all get screwed up very easily. Have one for normal, one for debug. If the view all looks screwed or explorer panel is black normalise and maximise again. Misc **** Text imported from a file has horrid little squares Text should be UNIX format. Use a freeware DOS to UNIX converter or edit in Eclipse. If you cut and paste sample code from the net it ends up as single line or has errors on every line Paste it as text into Excel (2007 version anyway) and then cut n paste from there, that makes it into lines. Pasting into Eclipse will still give errors as there are hidden characters at beginning and end of lines - they don't seem to be tabs or spaces, so use a text editor and replace these whatever they ares with nothings. Paste into Eclipse now should only give you errors due to undeclared imports which most examples don't seem to include - hover over each error and do Import activity (Android app). Pasted xml shows all lines as invalid -“No grammar constraints detected for this document” Get rid of warning by going into Window > Preferences > XML > XML Files/validation and setting the “Indicate when no grammar is specified” option to “Ignore”. Then do an XML validation (right-click > Validate) Unbound prefix in xml files At least the FIRST or containing element must be followed by xmlns:android="http://schemas.android.com/apk/res/android" Changing project path ********************* You might want to move a project to another directory. In Visual studio you just copy and click on the .xxproj file but fat chance it will be that easy in Eclipse: a) Delete the project from workspace, close Eclipse. Do NOT select remove from disc. b) Move or copy the project directory in explorer. c) Open Eclipse. Go to file/switch workspace/other and choose the new path. This is where it puts all its .metadata crap. d) That will cause Eclipse to close without warning, so open it again. Have several cups of coffe while waiting. e) Do file import/existing projects into workspace & chose new project path. An alternative is not to copy folder but use file/new/android project/create from existing source. f) It may say the android path is not set up, so go to window/preferences android and set path to sdk again. If you extracted it to C:\utils it would be C:\Utils\android-sdk_r06-windows\android-sdk-windows. g)If you get errors in your code that were not there before go to window/preferences/java/compiler and raise level to 1.6. h) You will probably find that stupid spell checker turned on again (who wants spell checking in comments FFS?) so turn it off again in Window/preferences/General/Editors/Text Editors/spelling. i) Throughout the above chant the incantation from The Army of Darkness, "Klaatu barada nikto" to keep away the evil Eclipse spirits. Debugging ********* Debug messages can be meaningless. I got a "Source not found" and "Edit Source Lookup Path" button and spent ages looking for something undefined or not installed. The problem was actually that I was using ScrollView with more than one child. "Source not found" seems to be shown when any array goes out of bounds. A Logcat message told me the above. Logcat can contain useful messages among all the rheams and rheams of meaningless crap. Note, can clear at top right of window. - Window/Show View/Other/Android/Logcat. If prog just bombs out with source not found when going to a new view and debug tells you nowt check the new view has a suitable view definition. It may be created in code eg summit like getSystemService(Activity.LAYOUT_INFLATER_SERVICE) or an xml layout like setContentView(R.layout.viewident); Error messages when execution halts are not always helpful or relevant, like source not found, add a try/catch round offending line: try { line that bombs out here } catch (Exception e) { Exception a=e; } Now hovering the mouse over e will give you more useful info. Producing larger volumes of output data for checking. While they are ok to track down the odd bug, the Eclipse debugger is very slow while Logcat has superfluous info and it's hard to find things. The best way if you want to output a large amount of debug data is to create a csv file that you can then easily compare with expected results in Excel. a) Sample code in java to write some arrays as comma delimited strings: //various imports needed at top of file import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import android.os.Environment; ... blah, blah, your crappy code here ... //temp test only comment out try { File root = Environment.getExternalStorageDirectory(); if (root.canWrite()){ String fileName="test.csv"; File f = new File(root, fileName); FileWriter fw = new FileWriter(f); BufferedWriter out = new BufferedWriter(fw); //your variables here out.write("number,instance,score\r\n"); for (int scnt = 0; scntILikeFatBlokes In eclipse: Change package name in .Manifest file package="I.Like.FatBlokes" Change package name at top of all source files package I.Like.FatBlokes; Change app name in res\values\strings.xml I Like Fat Blokes Run. As this is Eclipse, probably delete a char in a source file and put it back, delete gen to remove path errors a few times etc etc but it should work Not really necessary but if the top level view is based on app name you may want to change it: In AndroidManifest.xml file