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.
 

mikejsmith1985

Member
Licensed User
Longtime User
I was just thinking, I wonder if my PC could be the problem. I'm running Win7 Ultimate X86 on a virtual machine via parallels on my mac. this has caused some funny drive names and folder configs. I'm probably 6 months out from buying a new Windows based machine. Just a thought if I find any quirks to get it to run I'll post them up.
 

mikejsmith1985

Member
Licensed User
Longtime User
I have found that the Parallels virtual machine created a "psf" drive and when I add \\psf in front of the file paths I am able to run my emulator. I didn't really expect that to help but I guess that was the problem.

Thanks!
 

DST123

Member
I moved my Android folder to C, emulator didn't work. I also tried reinstalling sdk, didn't work. I went to download java from the link you provided and it said I had a newer version already installed. Could that be why it doesn't work? I looked at the last forum link you provided, but didn't know how to proceed. I run Windows 7, 32 bit.

Thank you very much for your time,
DST123
 
Last edited:

DST123

Member
I did some rearranging of files and now I get this error:

Failed to start emulator: Cannot run program "C:\Android\android-sdk-windows\tools\emulator.exe": CreateProcess error=2, The system cannot find the file specified

The emulator.exe is stored in my C drive, but the AVD folder automatically is stored in my Student Data folder on my desktop. When I try to move the AVD folder to C drive, i can't even create an emulator.

I'm wondering where I need to move the AVD folder so the manager can find the file?

Thanks!
Sawyer
 

DST123

Member
Yes, it's there. How can I change paths for my SDK, because that manager is taking the wrong path to find my AVD.

Thank you very much for your help!

DST123
 

DST123

Member
No, running it on my regular operating system. The path is:

C:\Android\android-sdk-windows

Thanks Again,
DST123
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I've asked before where the complete documentation is for B4a and was told that the tutorial web page is it; however, the tutorial web page only scratches the surface of a lot of things and I'm left not understanding how to do anything more advanced than what is in the tutorial.

For example, I have an Archos 70-250G (7" tablet, Android 2.2) with a screen res of 800x480. The A70-250G does not have an SD card but treats the 250GB hard drive as an SD, so I assumed that under "Size" on the AVD Manager, I should enter 250000 MiB. Given the way the AVD choked, I figure that must have been a mistake, but who knew?

Is there some reason to use "Default (HVGA)" resolution instead of the actual resolution? When I do put in the actual res of 800x480, it tells me that the resolution is 160, but in the designer, it says that it is 240 dpi.

For the "New" button under "Hardware"...
1. I don't know what to put for "cache partition size" or "cache partition support".
2. The tutorial doesn't mention having to specify "touch screen support", so is that not necessary?
3. How about "keyboard support"?
4. "Max VM application size"?
5. "Device RAM size"?

And this is just the start of the tutorial. I run into these kinds of questions all the way through. Is there really not a complete documentation anywhere? When we have to guess at everything, there are bound to be problems and it is very frustrating.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Thank you. Here's a few more questions:

It appears that it is not possible to edit an existing AVD, that instead a new AVD must be written with the modified specs and saved over the old one that I wish to change. Is this correct?

Do I understand correctly that a different layout variant must be created for every possible screen size? And in both horizontal and vertical orientations? I had assumed that Android just resized the layout for different screen sizes, or does it do a resize only if I don't create a specific layout variant for a particular screen size?

There is no need to add any more features than the ones described in this tutorial.

Why are the other features shown if they are not needed? (Serious question.)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It appears that it is not possible to edit an existing AVD, that instead a new AVD must be written with the modified specs and saved over the old one that I wish to change. Is this correct?
Yes.
Do I understand correctly that a different layout variant must be created for every possible screen size? And in both horizontal and vertical orientations? I had assumed that Android just resized the layout for different screen sizes, or does it do a resize only if I don't create a specific layout variant for a particular screen size?
Usually you will not need to create a variant for each layout.
There is a section about multiple layouts on this tutorial: http://www.b4x.com/forum/basic4andr...2-guess-my-number-visual-designer-events.html

You can also choose to lock your application to one orientation (Project - Oritenations supported).
Why are the other features shown if they are not needed? (Serious question.)
The emulator and AVD components are part of Android SDK. So there are some advanced features that you will usually not need to use.
You can see the official documentation here: Android Emulator | Android Developers
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User

Before reading the above link, I had created a layout using an AVD with 160 dpi. Per the tutorial, I changed the AVD's dpi to 240 which seems to have caused most of the layout I had created to be off the AVD's screen.

I figure that I'm now supposed to go back and create a new layout sized for the AVD at 240? The problem is that I don't know if that is really the right thing to do or if that is going to cause layout problems on an actual device.

I guess I need to figure out how to attach my tablet to my PC and use it instead of an AVD. I looked at some instructions somewhere for that, but they seemed pretty complicated. I don't suppose there's some easy way to do that?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should set the dpi value to match the value of the device you are trying to emulate. If this device is a high resolution device then you will usually need to set it to 240.

You can also run this code to check the values of your device:
B4X:
msgbox(GetDeviceLayoutValues,"")
I don't suppose there's some easy way to do that?
That depends on your device. For many devices all you need to do is enable USB debugging under the Applications settings menu and connect it. For some devices you will need to install a specific driver and some devices just don't support it.
 

wgodsoe

Member
Licensed User
Longtime User
I've asked before where the complete documentation is for B4a and was told that the tutorial web page is it; however, the tutorial web page only scratches the surface of a lot of things and I'm left not understanding how to do anything more advanced than what is in the tutorial.

For example, I have an Archos 70-250G (7" tablet, Android 2.2) with a screen res of 800x480. The A70-250G does not have an SD card but treats the 250GB hard drive as an SD, so I assumed that under "Size" on the AVD Manager, I should enter 250000 MiB. Given the way the AVD choked, I figure that must have been a mistake, but who knew?

Is there some reason to use "Default (HVGA)" resolution instead of the actual resolution? When I do put in the actual res of 800x480, it tells me that the resolution is 160, but in the designer, it says that it is 240 dpi.

For the "New" button under "Hardware"...
1. I don't know what to put for "cache partition size" or "cache partition support".
2. The tutorial doesn't mention having to specify "touch screen support", so is that not necessary?
3. How about "keyboard support"?
4. "Max VM application size"?
5. "Device RAM size"?

And this is just the start of the tutorial. I run into these kinds of questions all the way through. Is there really not a complete documentation anywhere? When we have to guess at everything, there are bound to be problems and it is very frustrating.

I agree! A document would be a huge help, currently, there is an endless search for method, properties within examples all over the place. I realize this is not an easy feat, but a product as terrific and robust as B4A is requires a manual. Along the same lines what Android application development books would be suggested, to get the big picture. Thanks for your extraordinary work.
 
Top