Android Tutorial Hello world - Installing Android Emulator

Please follow the installation (and configuration) instructions if you have not done it yet:
http://www.b4x.com/android/downloads.html

In this tutorial we will create a new AVD (Android Virtual Device) which is an emulator instance. Then we will create a simple program that displays a simple message box and also writes a message to the log.
You can also connect a real device to the IDE:
Connecting your device with B4A-Bridge
Common issues are listed at the end of this post. If you encounter an error that is not listed or that you are unable to solve, please contact [email protected]

Create new AVD

- Run Basic4android.
- Choose Tools Menu - Run AVD Manager.
Wait a few seconds.
- The SDK Manager may appear depending on the version and configuration. You should choose Tools - Manage AVDs.

newavd.png


- The AVD Manager should appear:

avd_manager_empty.png


- Choose New and fill the fields similar to the following image (it is recommended to choose HVGA) :

SS-2013-04-08_10.51.28.png


- Press on Create AVD.
- Note that you can create more than one AVD. Each can have a different resolution or can target a different API version (you will need to install additional platforms first).
- Now press Start in order to start the emulator
emulator_start.png


- You will see several windows popping up and disappearing. This is fine.
- The emulator should boot up:

emulator1.png


Wait... on the first time it can take several minutes till the emulator is ready.

The emulator is ready when it gets to this screen:
emulator2.png


You may see this screen, which is the lock screen, instead:
emulator_locked.png


Drag the lock icon to the right to unlock the device.

Note that there is no need to restart the emulator each time you deploy a program. The emulator can be kept running all the time.

If you are not familiar with Android you can play with the emulator. Press on the button with the small squares to get to the application page.

Troubleshooting: If you get an error message similar to:
invalid command-line parameter: Files\Android\android-sdk\tools/emulator-arm.exe.
Hint: use '@foo' to launch a virtual device named 'foo'.

Then you should reinstall Android SDK in a path without spaces, such as c:\android.


Writing your first Basic4android program

- As this is a new program we should first set its location by choosing File - Save.
It is highly recommended to save each project in its own folder.
- Create a new folder: "Hello world", open the folder and save the program as "Hello world".

- Write the following code under Sub Activity_Create:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Log("Hello world!")
    Msgbox("Hello world?", "First program")
End Sub
- Press F5 to compile and deploy your program to the emulator.
The package dialog should appear (empty):

package_dialog.png


Each Android application is identified by a unique package string.
This is a string built of several parts separated with periods.
The string should include at least two parts. You cannot install two applications with the same package on one device.
Note that you can always change the package name (and the label) under tools menu.

- Enter a package name.
- Next you will be asked to enter the application "label". This is the application name that the user will see.

Your program will now be compiled and installed to the emulator:

hello_world_success.png


The emulator is significantly slower than a real device.
In many cases it is more convenient to work with a real device as the installation is much faster.

Note that you can always redeploy your program. There is no need to close the running program on the emulator.

Tracking the log with LogCat

Android devices keep an internal buffer of log messages. These messages can be very handy for debugging.

To view the logs you should switch to the Logs tab in the right pane and press connect:
hello_world_logcat.png


There are two "Hello world!" messages in the screenshot as I ran the program twice.
Unchecking "Filter" will show all available messages (not just messages relevant to your program).

Hello world :)

Common issues
- "emulator: ERROR: no search paths found in this AVD's configuration
weird, the AVD's config.ini file is malformed. Try re-creating it."
This error happens when you have non-ASCII characters in your Windows user name. Android SDK fails to find the proper path.
The solution is to create a folder named: c:\android
And to add an environment variable named ANDROID_SDK_HOME with the value of c:\android.

- "invalid command-line parameter: Files\Android\android-sdk\tools/emulator-arm.exe.
Hint: use '@foo' to launch a virtual device named 'foo'." This error sometimes happen when you install Android SDK in a path with spaces. Copy the SDK to C:\Android and update the paths configuration to solve it.
 

jacksnipe

New Member
Licensed User
Longtime User
I keep getting this error when following the example posted at the start of this thread:

Compiling code. 0.00
Compiling layouts code. 0.00
Generating R file. 0.00
Compiling generated Java code. Error
javac 1.6.0_26
javac: file not found: src\anywheresoftware\b4a\helloworld\main.java
Usage: javac <options> <source files>
use -help for a list of possible options

Any ideas?
 

jacksnipe

New Member
Licensed User
Longtime User
OK, I figured that out finally. It would be nice if that was in the docs or in the tutorial somewhere.
 

kuntalpaul

Member
Licensed User
Longtime User
B4A IDE does not connect to the Emulator any more

Hi Erel,

I have purchased B4A couple of weeks back and have been using it since then. So far it was working just fine. But, today I am not able to connect to emulator from the IDE. I tried to many of the sample programs, I faced the same issue with all of them. See the screen print for the error message I am getting. Please help me to resolve this issue.

The emulator is running while I am compiling the program. Also I deleted current instance and created new instance of the emulator in AVD manager multiple times. Still it did not work. I even uninstalled the application with SDK and JDK. It didn't work even after reinstalling everything.

It is really frustrating, please help.
:sign0085:
 

kuntalpaul

Member
Licensed User
Longtime User
I have Android 4.0.3 (API 15) installed. Recently Java 6 was upgraded from 6.26 to 6.31. Can this be a problem?
 

kuntalpaul

Member
Licensed User
Longtime User
I can see the emulator listed using 'adb devices' command only after I bring up the emulator using AVD Manager. Otherwise I do not see any emulator listed using 'adb devices' command. Also 'adb -e shell' does not bring up the emulator.
 

kuntalpaul

Member
Licensed User
Longtime User
Yes, path has been correctly configured. I have installed Android SDK under c:\android and same has been mentioned in B4A configuration.

Can this problem be related to Windows 7 OS? I have installed B4A on my laptop (Windows Vista) following the same steps, naintaining same files paths. It is working fine on my laptop - though I would be comfortable in working on my desktop.
 

kuntalpaul

Member
Licensed User
Longtime User
Please suggest what else can I try? I really want it to run on my desktop. Do you want me to try any specific shell command?

Though 'adb -e shell' appeared to get connected to the emulator, do you want me to try any specific shell command to ensure that adb correctly gets connected?
 

kuntalpaul

Member
Licensed User
Longtime User
I see following text in the log file. Does it help?

===============
optimizedDex receive: 33
OptimizedDexer exitCode=0
Optimized worked...
wait one blocked.
starting adb start-server.

Trying again... 5

Trying again... 4

Trying again... 3

Trying again... 2

Trying again... 1
=====================
 
Top