There are many open source tools available to achieve Android APP programming. The options include Android Studio, NetBeans and Eclipse, or even Microsoft Visual Studio in the Windows world.
For a beginner like me, I prefer to use NetBeans since it's free and is capable of generating compact Android APPs that take very little cell phone ROM space. See the
(1) Install Android SDK Packages
- (a) Find the SDK development tool kit at http://developer.android.com/sdk/index.html#Other, and choose the sdk package, say android-sdk_r24.4.1-linux.tgz (approximately 326M bytes). Move the downloaded file to an appropriate folder and use the command tar xvzf android-sdk_r24.4.1-linux.tgz to unzip it.
- (b) Start a terminal, locate in the unzipped files for the the executable named android in the tools folder, and run android command in terminal to install essential packages, including supporting packages for different versions of Android operating system, tools, Google APIs, SDK build tools, emulator images for different versions and sample codes. This step is essential for preparing Android developing environment, and must be done before NetBeans can identify if an SDK folder is valid or not. It can be later modified after the installation and configuration of NetBeans [see step (2)(c)] using menu Tools|Android SDK Manager and AVD manager.
- (c) Configure path variables for PATH to include sdk tools and platform-tools folder, ANDROID_HOME to point to the sdk folder, and ANDROID_SDK_ROOT in /etc/profile file. PATH="/root/Android/Sdk/tools:/root/Android/Sdk/platform-tools:..."
- (a) Install the proper Java SDK if it not yet done. See the other blog page for instructions, and make sure PATH and JAVA_HOME are configured.
- Go to https://netbeans.org/downloads/ and locate the right NetBeans IDE package. For example, the current version is netbeans-8.1-javase-linux.sh, and the size is 94 MB if only NetBeans Platform SDK, and only Java SE and FX are needed.
- (b) move the NetBeans executable to appropriate location and run it in terminal to install NetBeans. The installation will take about 400 MB of disk space.
- (c) If you prefer, use file manager (ROX Filer) and browse to /usr/share/applications to find the netbeans application icon and drag it to desktop to create a shortcut link on desktop.
(3) Install Android plug-ins for NetBeans
- (a) Run the NetBean IDE, Select menu Tools|Plugins|Settings (select the Setting tab) and add a new update center with the Add button: http://nbandroid.org/updates/updates.xml for NetBeans 7.3 to 8.0, http://nbandroid.org/release81/updates/updates.xml for NetBeans 8.1+. The old link http://kenai.com/projects/nbandroid/downloads/download/updatecenter/updates.xml can also be added.
- (b) Click the Available Plugins tab, and there should have Android (Android Gradle Support may also be available). Check them and install the Adnroid. Restart NetBeans to activate the plugins.
- (c) Select Tools|Options|Miscellaneous|Android menu and tab combination, and browse for the installed SDK folder. In my case, it is /aufs/devsave/Downloads/android-sdk-linux. The Apply button will be enabled if the folder is correctly located. Apply to proceed.
- (d) Load the 32bit-slacko6.sfs and then run NetBeans, then run Tools|Android SDK Manager to create simulation devices if intended SDK system, simulation image devices, and API packages are not yet available.
- (e) Run NetBeans|Tools|AVD Manager and create new Android Virtual Devices for running emulation.
(4) Trouble shooting
Often times, the installation might not work as expected when you try to create your first Android APP. This is normal since there are many ways the development system can go wrong. The following are typical problems that I have encountered:
- R.java not found issue: try to load the 32bit-slacko6.sfs, use Run|Clean and Build project menu to let NetBeans regenerate R.java.
- NetBeans cannot compile program: (1) 32bit-slack6.sfs needs to be loaded before starting NetBeans, (2) need to download proper versions of SDK Platform, Google APIs as well as proper virtual simulation system images through Tools|Android SDK Manager menu.
- NetBeans cannot start Android emulator for debugging: (1) Run Tools|AVD Manager to create desired Android Virtual Devices. Notice that there are many options that may require different software and hardware resources, such as android version, screen size, CPU type, skin, camera, SD card, acceleration and so on. Try different combinations to find out one that can successfully launch. For example, the "kvm is not installed" issue can be avoided by picking different CPU type (i.e. ARM CPU) and other options.
- No such file or director for adb or emulator even though these files are installed: 32bit-slacko6.sfs has to be loaded before these executable files can be launched.
- If APPs crash, try to find more information through the LogCat located in monitor.
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE: APP package not uninstalled cleanly. Run adb shell, su, pm list package, pm uninstall <package>. Also adb shell chmod 666 /data/system/packages.xml, adb pull /data/system/packages.xml, remove the problematic bock in the packages.xml, then adb push packages.xml /data/system, adb shell rm /data/dalvik-cache/*, adb reboot.
(5) Configurations for debugging
Once the above steps are finished successfully, NetBeans is ready for programming and debugging basic Android APP functionalities through the integrated emulators created with proper configurations. However, some debug processes such as GPS and SMS need separate tools to emulate. These involve two tools that come with the Android SDK package, available in the installed Android/Sdk/tools folder. These tools somehow don't show up as the NetBeans plugins.
Once the above steps are finished successfully, NetBeans is ready for programming and debugging basic Android APP functionalities through the integrated emulators created with proper configurations. However, some debug processes such as GPS and SMS need separate tools to emulate. These involve two tools that come with the Android SDK package, available in the installed Android/Sdk/tools folder. These tools somehow don't show up as the NetBeans plugins.
- (a) DDMS (Dalvik Debug Monitor Service) can be found in the monitor as a tab in step (b).
- (b) Android Debug Monitor and Emulator. These tools need to be configured in a few steps to run with NetBeans.
- Start the emulator separately (without NetBeans) through shell command "emulator -avd ????" & where ???? can be found as $HOME\.android\avd\????.avd.
- Start the monitor separately (with emulator running) through shell command "monitor &". The monitor will detect the emulator (i.e. emulator-5554). Use File|Static Port Configuration, then click New or Edit button to configure emulation port for the monitor to be visible to NetBeans. Enter the port number to 8700 so that it will be able to talk to NetBeans.
- Quit the monitor and start it again to make sure the settings take effect. If the monitor find the emulator, select the simulated device, and some simple tests can be done to verify the connection, such as sending a text message to the emulator or setting GPS location coordinates, or changing voice/data dropdown lists.
- .
- Start NetBeans. It appears to me that the monitor will talk to the emulated device. Right click the APP project icon, and select the target device that the emulator is based on. Start a debug process for the APP. It's possible that NetBeans won't be able to find the emulator and pop up a new emulator. Just kill the new emulator and try a few times. The APP will be installed on the emulator.
- On the emulator, go through the setting menu, and turn on the debug option for the APP.
- In NetBeans, run the Debug|Debug Project menu again, the emulator will show a dialog saying waiting for debugger, waiting for the debugger to attach, like this:
- In NetBeans, use menu Debug|Attach Debug, then connect to the debugger using the option like this:
- Click ok and the debugger be connected to the emulator, and at the same time, the emulator can also be controlled by the monitor.
- In order to simulate GPS, the APP must have <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in manifest file. Be very careful about the spelling because there will be no warning when it's compiled, leading to no GPS service providers using LocationManager.getProviders. Please also run LocationManager.requestLocationUpdates with listeners in order to get non-null locations when calling LocationManager.getLastKnownLocation(provider), otherwise getLastKnownLocation will always return null for the provider.
- It appears that NetBeans and the monitor can be connected to the same emulator at the same time only at some specific order. One working process seems to be like this: run emulator from command shell, then run monitor from command shell, then run the debug target APP in the emulator, it will wait for debugger if the APP has setup correctly for debugging, then in NetBeans try to attach debugger using the screenshot as follows to identify the simulated device and the APP to debug, then NetBeans will enter debug mode for the APP:
- Some useful links about settings for GPS emulation: http://stackoverflow.com/questions/13691943/this-app-wont-run-unless-you-update-google-play-services-via-bazaar/13869332#13869332 and http://stackoverflow.com/questions/18459338/how-to-run-google-map-api-v2-on-android-emulator
- Since I am still some puzzled by the way emulator works, the following statements are experimental. A good reference article (Under the hood of Android Emulator) for understanding the emulation can be found at https://wiki.diebin.at/Under_the_hood_of_Android_Emulator_(appcert), in addition, creating AVD with Google APIs x.x.x (Google Inc.) seems to have google map installed by default, while Android x.x.x doesn't.
- Some programmer pointed out that in order to show Google Map on simulator, GPU acceleration must be turned on.
- Install necessary packages in SDK Manager in the Extras in addition to the Google APIs, SDK platform, and simulation image for targeted Android versions. The extra packages may look as follows.
- The APP needs to refer to the google-play-services.jar library. A complete set of google-play-services.jar files are located in folder android_sdk_folder/extras/google/m2repository/com/google/android/gms/play-services/??.??.??, just rename the *.aar fils to *.zip and unzip them. Right click the APP project, select properties, click JAR Libraries, then browse to the jar library located in the extras/google/google_place_services/libproject/google-play-services_lib/libs folder, as follows. Some people recommend copying the jar file to local folder corresponding to the lib directory for the APP project. This will integrate the jar library into the compiled APP. Some programmers suggest using android update lib-project -p . --target android-17 command at .../extras/google/google_play_services/libproject/google-play-services_lib directory and then use Project|Rightclick|Properties|Libraries|Add to refer to the library folder. Also do the same for .../extras/android/support/v7/appcompat.
- Use SDK Manager through Tools|Manage add-on sites menu and add https://dl.google.com/geosdk/android-m4b-addon.xml as user defined sites.
- Install packages including com.android.vending.apk, com.google.android.gms.apk and com.google.android.apps.maps.apk. Then the Google Maps Mobile SDK for Work will show up in available packages Extras of Package by first downloading these files from http://stackoverflow.com/questions/14040185/running-google-maps-v2-on-the-android-emulator, or better search for the right version from https://androidfilehost.com or apk4fun.com, and copy them to Android SDK's platform-tools folder. Start the emulated device, then in terminal, run adb remount to make system writable, adb install com.android.vending.apk, adb install com.google.android.gms.apk and adb install com.google.android.apps.maps.apk, wait for success messages. It's possible that the emulator may have existing packages, use adb install -r ...apk. Use adb to find options of adb command. Use adb shell, and then su command, then pm list packages to find installed packages, pm uninstall a_package to uninstall.
- Install packages including GoogleServicesFramework.apk, GoogleLoginService.apk and Phonesky.apk. Download these APPs like the way for the vending, gms and maps. These are system APPs that needs to be granted access and pushed with the following terminal shell commands: (0) from terminal, increase emulator ROM space by running shell command emulator -avd your-avd-device -partition-size 512 -no-boot-anim and then adb shell mount -o remount -t yaffs2 /dev/block/mtdblock0 /system (1) adb remount (2) adb shell chmod 777 /system/app/ (3)adb push GoogleServicesFramework.apk /system/app/, adb push GoogleLoginService.apk /system/app/, adb push Phonesky.apk /system/app/. With this and the previous step, just click Google Play in the emulator, set up gmail account carefully. Make this system change persistent by using mkfs.yaffs2 tool to create a system image (adb push mkfs.yaffs2, adb shell mkfs.yaffs2 /system /sdcard/system-new.img) and pull the created system image (adb pull /sdcard/system-new.img) and save it as ../avd/avdname.avd/system-qemu.img. But this mkfs.yaffs2 method doesn't work for me and the created system image file is smaller than the default system.img.
- The simplest method to make changes to /system persistent is to copy the temporary image file in the form of /tmp/android-????/emulator-???? file to the ~/.android/avd/....avd/system.img (of course save the original image file). (a) run emulator -avd your-avd-device -partition-size 512 -no-boost-anim (of course load 32-bit sfs first), /tmp/android-root/emulator-?????? will show up (b) adb shell mount to check what is mounted as /system in simulated device, it is /dev/block/mtdblock0 for /system yaffs2 ro for example (c) adb remount /dev/block/mtdblock0 /system yaffs2 rw 0 0 to make /system writable (d) push necessary files to /system subfolders, the emulator-?????? file will grow in size (d) copy the emulator-?????? file before closing emulator. (e) copy the emulator-?????? to ~/.android/avd/avdname.avd/system-qemu.img. Next time the emulator is started, the change in /system will be saved, and no /tmp/android-root/emulator-?????? will be available. I guess that the easiest way of making persistent change to system is to copy original system.img that came with android SDK as ~/.android/avd/avdname.avd/system-qemu.img, then everything done to the /system will be saved in system-qemu.img, and will be used for next invocation of emulator.
- DO NOT copy maps.jar from Sdk/add-ons/addon-google_apis-google??/libs to the library folder of APP project folder to avoid issues such as "class resolved by unexpected dex has used a different com/google/android/maps/MapActivity". Instead right click project|JAR Libraries|Browse to ...add-ons/addon-google_apis-google-??/libs, this will allow com.google.android.maps.MapActivity been imported. The first import statement should be import com.google.android.maps.*;
Download and push com.google.android.maps.jar to /system/framework, com.google.android.maps.xml to /system/etc/permissions. This will resolve the strange issue where compiled APP with <uses-library android:name="com.google.android.maps"/> between <application /application> cannot be pushed to emulator.- Go to https://developers.google.com/maps/documentation/android-api/signup and click the GET A KEY button for standard Google Maps Android API. There is an option to create key for individual packages, but it's only an option. The following shows how to create key for debug (a) find the SHA-1 fingerprint of the APP package by running keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey, use android as password, then a 20-byte hexadecimal fingerprint numbers will show up after SHA1. (2) click link to go to Google Developers Console: https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&reusekey=true, create a new project, then add package name and fingerprint. (3) Copy the generated key, and add a child element for <application> element in AndroidManifest.xml in the form of <meta-data android:name="com.google.android.geo.API_KEY" android:value="????...????".
- Need to copy sdk extra google-play-service_lib folder to the lib directory of the APP project, add rightclick|properties|libraries and point to this copied folder.
- Must run adndroi update lib-project --path the_copied_google-play-service_lib.
- Copy the .jar in the google-play-service_lib folder to the APP's lib folder
- Copy the androi-support-v4.jar in extras android-support-v4 folder.
- https://www.youtube.com/playlist?list=PLxE2viIA-WCy9hrXIRlgKYHEFX7CW9F_P
https://blog-emildesign.rhcloud.com/?p=527 says that downloading and installing HAXM http://software.intel.com/en-us/articles/installation-instructions-for-intel-hardware-accelerated-execution-manager-microsoft can accelerate emulator speed, and someone told me that turning on GPU emulator can make Google Map showing on emulator.
(7) Accessing web pages with URL
- For accessing web pages with URL, sometimes it is necessary to put in the following codes
- connection= new URL().openConnection();
- connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
- connection.setRequestProperty("Accept","*/*");