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.
 

JohnH

New Member
Licensed User
Longtime User
Error message on launch emulator

Hi,
Just getting started with B4A, and am unable to launch an emulator.
Getting the following error:

invalid command-line parameter: Files\Android\android-sdk\tools/emulator-arm.exe.
Hint: use '@foo' to launch a virtual device named 'foo'.
please use -help for more information

Looks like maybe there's a forward-slash that should be a backslash? How do I fix this?

Thanks.
 

JohnH

New Member
Licensed User
Longtime User
Still having path issues

Hi Erel, and thanks for the quick reply. I uninstalled and reinstalled both the Android sdk and B4A.
Android sdk is in c:\AndroidSDK\ and B4A is in c:\B4A\

Now as soon as I click "Run AVD Manager" I get an error box:
"An error occurred.
The system cannot find the file specified."

It doesn't say what file it's looking for.

I appreciate your help.

-John
 

vinny

Member
Licensed User
Longtime User
target release error

I finally got the emulator running but now, when I try to run the app I get this message in a 'Compile & Debug" message box:

Compiling code. 0.02
Generating R file. 0.01
Compiling generated Java code. Error
javac 1.7.0
javac: target release 1.5 conflicts with default source release 1.7


I don't know anythin about java yet so please help.
 

vinny

Member
Licensed User
Longtime User
target release 1.5 conflicts with default source release 1.7

I wish I had 1.7. It isn't available in my sdk. I have 1.5, 1.6, 2.2, 2.3.1, 2.3.3 and some higher. Testing is really slow, I guess I need more than 2gig.

Is there a way to use one of the versions I do have? Is this a property of b4w?
 

agraham

Expert
Licensed User
Longtime User

vinny

Member
Licensed User
Longtime User
target release 1.5 conflicts with default source release 1.7

@Vinny. It look like you have installed Java 1.7 which will not work. See http://www.b4x.com/forum/basic4android-updates-questions/10567-new-java-update-b4a.html#post58798 and http://www.b4x.com/forum/basic4andr...477-installing-basic4android-android-sdk.html for the link to Java 1.6.[/QUOTE]

Thanks everyone. Installing 1.6 did the trick. I guess I should have read the install instructions more carefully. I think I'm giving up on the emulator. I'll be working on my laptop, core 2 duo 2.4 with 2gigs doesn't seem to be enough. Do you think more memory will help or would I need more processor too?
 

Kim Rowden

Member
Licensed User
Longtime User
Getting started...

Erel,
Many thanks and congratulations on a great product!

I have some experience with the following:
- the Android SDK : too much for most of my requirements
- Google's App Inventor : very simple (too simple) but in the process of being discontinued
- Appcelerator's Titanium : very powerful but overly complex at times

I have not yet had a chance to explore B4A too deeply but my first impressions are that it is simple, straightforward and works right out of the box.

I think it fills a need and will enjoy a considerable following...

Congratulations! I'm looking forward to working with all the folk in this community,
-Kim
 

martinfd

Member
Hi. I have a problem I can't resolve.
I install JDK 6-u21
I install SDK Manager and I have downloaded everything related with API 8.
I have installed correctly basic4android.

I have configured the paths:
C:\Archivos de programa\Java\jdk1.6.0_21\bin\javac.exe
C:\Android\android-sdk\platforms\android-8\android.jar
C:\Archivos de programa\Anywhere Software\Basic4android\Libraries

and I have this error:

Compiling code. 0.00
Generating R file. 0.02
Compiling generated Java code. 0.55
Convert byte code - optimized dex. Error
Unable to access jarfile C:\Android\android-sdk\platforms\android-8\tools\lib\dx.jar
Standard dexer.

This path doesn't exist, "tools" and "platform-tools" exist under "C:\Android\android-sdk" (where I have installed sdk).

Do I need to move this folder?

Thank You.
 

Avon

Member
Licensed User
Longtime User
AVD hangs

I started today with this software, which I installed on the same PC that I have Eclipse, etc, running. I've downloaded the latest Java 6 SDK.

The paths in the "paths configuration" dialog appear to be correct.

My AVD is configured as described at the start of this tread, except that I have set the size to 640 x 640. It hangs on the "android" screen (big script) when running B4A.

Does this ring bells with anyone?

I tried removing all the packages from the AVD which correspond with versions of Android higher than 2.2, the only difference in the result being that some dependent packages are shown in the AVD "installed packages" view with red "broken" icons.

Any suggestions please?
 
Top