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.
 

Crolt

Member
Licensed User
Longtime User
Hi,
I'm quite new in this framework.
I have a similar problem.
I used Android 2.3.3 with 1GB SD Card for the Virtual Device.
Simply copying several MP3 files (around 30MB) on the folder "ProjectName/Files", even without using those in the code, I had, after a very long time in compiling (and a "Process is running longer than expected" message), the following error:

Installing file to device. Error
failed to copy 'ProjectName_DEBUG.apk' to '/data/local/tmp/ProjectName_DEBUG.apk': No space left on device

Thank you for the great help from this forum.
Carlo


>> What about files and images you wish to use during runtime?

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
 

Crolt

Member
Licensed User
Longtime User
Thank you Erel,
I've tried in different ways, new projects, new devices. But when I try to add the other files it doesn't work. Today I'll do some other tests...
Sorry, how can I install it on external storage in the simulator?

Have you checked Project - Can install to external storage?

You can try to delete this AVD and create a new one.
 

Crolt

Member
Licensed User
Longtime User
At the beginning I used to copy manually the files into the "files" folder. Then I've tried using the "Add Files" button within the IDE, but it seems that it doesn't change anything...

With the external storage option checked it seems it changes a little bit!
I've tried adding file one by one, and when it reaches a certain amount it could start to delay the compilation phase, then:
Process timed out.
You can change the timeout value under Tools - IDE Options.
Restarting ADB Server may solve this problem.
Tools - Restart ADB Server. Device serial: emulator-5554

Deleting back files, it gives you again the message that it takes too much, but after that it works!

With the simulator I don't know how to copy to external storage manually. Which is the related folder?. With Adb Tool do you mean via command line?



It will be installed to the external storage by default if you check this option.

Another option is to remove the large files from the project and manually copy them to the external storage (with adb tool).
 
Last edited:

derez

Expert
Licensed User
Longtime User
In the command line write:
>adb push {filename with the complete PC path, use this \} sdcard/{directory/filename}

if you write just directory name in both parts - all the files will be copied.
pressing F3 rewrites the command so you can now change only the relevant part for another command.

To copy from the external storage to the PC use adb pull, with the ext. storage path first and then the PC path.
 
Last edited:

Crolt

Member
Licensed User
Longtime User
Thank you very much to you all...



In the command line write:
>adb push {filename with the complete PC path, use this \} sdcard/{directory/filename}

if you write just directory name in both parts - all the files will be copied.
pressing F3 rewrites the command so you can now change only the relevant part for another command.

To copy from the external storage to the PC use adb pull, with the ext. storage path first and then the PC path.
 

s0m4y3h

New Member
hello please help me

hello
Sorry I asked the wrong question
When creating the project I get this error please help me.

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

GaNdAlF89

Active Member
Licensed User
Longtime User
hello
Sorry I asked the wrong question
When creating the project I get this error please help me.

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

Basic4android requires Java 6. It will not work with Java 7!!
To resolve this problem, you can install Java 6, and then you must configure Basic4android to use Java 6.
 

Robert

New Member
Licensed User
Longtime User
The emulator doesn't start...

Hallo,

I'm new to B4A and maybe this is a easy settings ... but something doesn't work...
I follow this tutorial and cannot finish because I cannot start the emulator. Press start button and it will open a small window with a progress bar then it will close. No error, nothing, just the emulator doesn't start.
I have windows8 32bit.
Android SDK is installed in C:\Android\android-sdk\tools

What can I do?
Connect remotely to device it's working. I compile and run on device just fine using remote connections trough B4A Bridge but using the emulator no way :eek:(

Thank you for any help/suggestions.
Robert
 

Robert

New Member
Licensed User
Longtime User
I create more AVD but no one starts the emulator... also if I use a real device for development every time I compile I need to install the app correct? or replace the current install...
Also if emulator it's not starting how can I test for more platforms?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you are using B4A-Bridge then you need to confirm the installation. If your device supports USB debugging then you can use it instead and it allows you to install applications without the need to confirm the installation.

There seems to be a problem with Android SDK on your computer. As it doesn't give any error, it is difficult to say what is the error.

Note that you can use the UI cloud to test your layout on different devices:
Supporting multiple screens - tips and best practices
 

Robert

New Member
Licensed User
Longtime User
Hallo Erel,

Noted and thank you!
I will reinstall Android SDK and let you know. My device support USB debugging and I will try that.

Thank you,
Robert
 

deadmarshal

New Member
compile error

i get this error what should i do ?
adb and sdk are correctly installed
:sign0085:
notice me by email too :)
 

Attachments

  • problem.PNG
    problem.PNG
    9.7 KB · Views: 312

John316

New Member
Hi friends! I'm a newbie to android and infact to the programming field. I want to become an android app developer.

I just know that one has know some part of Java and xml. Could any one suggest where to start.

Kindly post any links to books, blogs, websites, video tutorials etc that I can access freely online to learn the basics and advanced concepts.

Any help would be much appreciated.

Thanks in advance!

Cheers. :)
 
Top