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.
 

derez

Expert
Licensed User
Longtime User
Erel
It looks easy !

What are the tools to control where in the device the application is located after installation (which folder ?).

What happens when you make an application that needs additional files (like images, sound, text files etc.) - how do you include them with the package for installation ?

Another issue - I am thinking of buying A81-E which uses a resistive screen, so it cannot handle multiple touch. How will the code handle the difference between capcitive screen and resistive ?

(more questions will follow until you give us work to do in the B4A...:))
 

agraham

Expert
Licensed User
Longtime User
What are the tools to control where in the device the application is located after installation (which folder ?).
You don't and can't. Android takes care of the details of installation and deinstallation. For 2.2 and later I believe there is an option to install a package on the SD card and not internally on the device but the package has to specifically permit this. B4A packages at the moment don't include this option.

What happens when you make an application that needs additional files (like images, sound, text files etc.) - how do you include them with the package for installation ?
You place them in a folder in the B4A source tree and they are included as assets in the package that you can access from your code like a normal file.

How will the code handle the difference between capcitive screen and resistive ?
It doesn't have to as there is no difference visible to either Android or your application.
 

SarahWard

Banned
Assets and memory overheads

>> What about files and images you wish to use during runtime?
You place them in a folder in the B4A source tree and they are included as assets in the package that you can access from your code like a normal file.
.
Andrew, what happens if the program has a very large amount of these, say, MP3, JPG, TXT files? In a b4ppc program these would not affect the overall size of the program unless they were 'loaded'. Will including them as 'assets' in a b4a program increase its size massively?

Sarah
 

agraham

Expert
Licensed User
Longtime User
I haven't worked out what really happens when an Android package is installed nor have I worked out how assets are accessed - whether from the apk as needed or whether extracted and saved - so can only comment in general terms.

Having a set of large assets in the package is probably undesirable as prior to Android 2.2 the apk is copied to the, usually limited amount of, internal device flash memory and installed there. The apk file is saved in flash memory, not deleted after app installation, and other data extracted from the apk is also saved. Having said this I see the popular AngryBirds game package is over 12Mbytes in size.

If an application requests and the user permits it on Android 2.2 and later an app can be installed on the SDcard. The Android docs say "The .apk file is saved on the external storage, but all private user data, databases, optimized .dex files, and extracted native code are saved on the internal device memory." Such an app can only run while the SDcard is mounted implying to me that assets are accessed from the apk as required.

The "Android" way of dealing with this would be to download the data from the cloud like most Google applications but not everyone has access to a 24/7 web server to host their data.

How Basic4Android applications are installed is something that will need thinking about.
 

SarahWard

Banned
I haven't worked out what really happens when an Android package is installed nor have I worked out how assets are accessed - whether from the apk as needed or whether extracted and saved - so can only comment in general terms.

Having a set of large assets in the package is probably undesirable as prior to Android 2.2 the apk is copied to the, usually limited amount of, internal device flash memory and installed there. The apk file is saved in flash memory, not deleted after app installation, and other data extracted from the apk is also saved. Having said this I see the popular AngryBirds game package is over 12Mbytes in size.

If an application requests and the user permits it on Android 2.2 and later an app can be installed on the SDcard. The Android docs say "The .apk file is saved on the external storage, but all private user data, databases, optimized .dex files, and extracted native code are saved on the internal device memory." Such an app can only run while the SDcard is mounted implying to me that assets are accessed from the apk as required.

The "Android" way of dealing with this would be to download the data from the cloud like most Google applications but not everyone has access to a 24/7 web server to host their data.

How Basic4Android applications are installed is something that will need thinking about.
Thank you.

Some of my freeware nature applications use in excess of 90mb of data. With b4ppc I simply read in the small amount of data needed for each record. Android looks like it highly prefers web-based applications when data is in use.
 

agraham

Expert
Licensed User
Longtime User
On all Android devices I know of the SDcard looks like a USB memory stick to a desktop when the device is plugged in so you even if your app was installed from the Android Market it could look for the data on the SDcard and if missing prompt the user with a download link from which to obtain it.

If the application is to be downloaded from your own site then you just need two components, the apk for the application and a separate data package to copy to the SDcard of the device. I don't know if it is universal on Android devices but both my devices came with a File Manager that can look at the SDcard and if you click on an apk on the card will install it. Even if a device doesn't come with this ability then as long as it can access the market (not all devices, particularly tablets, can) there are many free installers and file managers that can do so. I use ES File Explorer which seems to be acknowledged as one of the best.
 

agraham

Expert
Licensed User
Longtime User
Unless you are running with UAC disabled Program Files is not a good location for the Android SDK owing to the extra protection Vista and Windows 7 give to the contents of that folder. You would be better moving your Android folder to be directly under C:.
 

mikejsmith1985

Member
Licensed User
Longtime User
So I have 0 programming experience but want to start. I just got my first android phone and decided I wanted to start by writing apps for my phone. I bought Basic4android installed following all the tutorials to the T.

Trying to walk through this tutorial for some reason every time I try to start my AVD I get these 2 messages.

emulator: ERROR: unknown virtual device name: 'Emulator1'
emulator: could not find virtual device named 'Emulator1'

I have uninstalled SDK and reinstalled, I've deleted the AVD and rebuilt still same thing. Any suggestions?
I do have the green checkmark notating "A valid Android Virtual Device"

There must be a configuration that I have missed Just not sure what it is.

Thanks to anyone that can point me in the right direction!
 

mikejsmith1985

Member
Licensed User
Longtime User
here are the screen shots you requested.
 

Attachments

  • img1.jpg
    img1.jpg
    22.5 KB · Views: 1,059
  • img2.jpg
    img2.jpg
    13.4 KB · Views: 942
  • img3.jpg
    img3.jpg
    18.8 KB · Views: 989
Top