Creating an eclipse project for android. Setting up Eclipse for Android development. Slow launch of the emulator
In this guide, you will learn how to start developing for Android, namely: mobile applications on the Android Eclipse + SDK platform. Today, there are many approaches to developing and creating mobile applications for Android. Complex hardware devices, tablet PCs and various software platforms (Symbian OS, iOS, WebOS, Windows Phone 7...) open up endless space for developers.
So, the list of applications to start developing mobile applications for Android is huge. Which platform should you choose? What programming language should I learn? Which set of applications to choose for project planning? In this guide, you'll learn how to get started developing mobile apps for the Android platform, Google's open mobile OS.
Why Android platform?
Android is an open platform based on the Linux kernel. It is installed on thousands of devices from a wide range of manufacturers. Android provides applications for every type of hardware you can imagine. mobile devices- digital compasses, video cameras, GPS, sensors and much more.
Free Android development tools let you quickly start creating free or almost free applications. When you're ready to show the world your app, you can publish it through the Android Market. Publishing to the Android Market requires a one-time registration fee ($25 at the time of writing) and, unlike App Store Apple (which is very strict about qualifications) makes your app available for download and purchase after quick overview- if the application does not violate the rules and laws.
Here are a few others Android differences SDK that offers you benefits as a developer:
- The Android SDK is available for Windows, Mac, and Linux, so you don't have to pay for new hardware to write apps.
- SDK built into Java. If you are familiar with the Java programming language, you are already halfway there.
- Taking into account the distribution of the application through the Android Market, it will be immediately available to hundreds of thousands of users. You are not limited to just the official Market, as there are alternatives. For example, you can publish the application on your blog. Amazon is rumored to be preparing its own Android application store.
- As well as the SDK technical documentation, new resources are being created for Android developers. The platform is gaining increasing popularity among users and developers.
Enough talking - let's start developing Android apps!
Installing Eclipse and Android SDK
Orientation to the Android platform during development
Before you start writing applications for Android, you need to download the SDK for the Android platform. Each platform has its own version of Android SDK, which is installed on users' devices. For Android version 1.5 and higher, there are two platforms: Android Open Source Project and Google.
Android Open Source Project is an open source platform source code, but does not include Google extensions, such as Google Maps. If you don't want to use the Google API, the functionality Google Maps will not be available for your application. Unless you have a specific reason to do this, I would recommend that you target on one of Google's platforms, as this will allow you to use Google's own extensions.
- Select Window->Android SDK and AVD Manager.
- Click Available Packages in the left column and check the repository to show a list of available Android platforms.
- You can select platforms to download from the list or leave all checked to download all available platforms. Once complete, click Install Selected and follow the installation instructions.
Once everything has been successfully downloaded, you are ready to start developing for Android.
Creating a New Android Project
The Eclipse New Project Wizard helps you create an Android application by generating files and code ready to run to the right of the block. This quick way to ensure functionality and a good starting point when starting application development:
After clicking Finish, Eclipse will create a new Android project, ready to run and develop on Android. Remember when you told Eclipse to generate an Activity called BrewClockActivity? This is the code that Android actually uses to run the application. The generated code will be displayed as a simple 'Hello World' style message.
Packages
The package name is an identifier for your application. When it comes time to publish the result to the Android Market, this ID will be used to track updates for the application, so it is important to ensure that this ID is unique. Although we are using the name com.example.brewclock here, for a real application it would be better to choose something like com.yourcompanyname.yourapplication.
SDK versions
Min SDK Version (the name speaks for itself) - the earliest Android version, on which the application will run. With each new Android release, the SDK adds and changes various methods. When you select an SDK version, Android (and the Android Market) knows that your app will only run on devices running the specified Android platform version and higher.
Launching your application
Now let's try to run our application in Eclipse. Since this is the first run, Eclipse will ask which project type you are working with:
- Select Run->Run or press Ctrl+F11.
- Select Android Application and click OK.
Eclipse will try to run the application on the Android device. On this moment, however, you do not have devices running Android control, so the project will fail to run and you will be prompted to create a new Android Virtual Device (AVD).
Virtual devices (Android Virtual Devices)
Android Virtual Device (AVD) emulates the Android environment, be it mobile phone or tablet PC. You can create as many AVD devices as you like, with different versions of the Android platform. For each AVD, you can configure various device parameters, indicating the presence of a physical keyboard, GP support, camera resolution, and so on.
Before you run the application, you must create your first AVD device with the Platform SDK (Google API, 1.6).
Let's do it now:
- If you haven't tried running the application yet, click Run (or the keyboard shortcut Ctrl+F11)
- When a warning pops up, click Yes to create the AVD.
- Click New in the Android SDK and AVD Manager dialog.
- Set the following parameters for AVD: Name: Android_1.6 Target: Google APIs (Google Inc.) - API Level 4 SD Card Size: 16 MiB Skin Built In: Default (HVGA)
- Click Create AVD.
- Close the Android SDK and AVD Manager dialog.
Running the code
Try running the application again (Ctrl + F11). Eclipse will now create your project and launch a new AVD. Remember, AVD completely emulates the Android environment, and you don't even have to observe the rather slow boot process that is the case with a real device. For this reason, once the AVD is ready to go, it's best to keep the window open while you're done programming for the entire day.
Android development: user interface design
One of the first steps to creating any program at all is designing the user interface. Here's a short sketch of our application:
The user will be able to set the boiling time in minutes using the + and - buttons. When he presses Start, the time will begin to count down for the specified number of minutes. If the user cancels boiling by pressing the button again, the time will be increased when the timer reaches 0.
Interface construction
Custom Android interface, or wrapper, which are described in the XML documentation, can be found in the res/layouts folder. The template code, already generated by Eclipse, is declared in res/layouts/main.xml and, as you notice, is already running in the emulator.
Eclipse has its own layout tool that allows you to create a drag-and-drop interface across the screen. However, at times it is easier to write the interface in XML and use a graphical layout to preview the results.
Let's do it now by changing main.xml as per the sketch above:
- Open res/layouts/main.xml in Eclipse double click from Package Explorer.
- Select the main.xml tab at the bottom of the screen to enter XML mode.
Now let's change the contents of main.xml to the following:
# /res/layouts/main.xml
As you can see, there are a lot of files in Android XML, but this will allow you to control almost every element on the screen.
One of the most important UI elements in Android are Layout containers, such as the LinearLayout used in this example. These elements are invisible to the user, but act as containers for other elements such as Buttons and TextViews.
There are several types of layout layout, each of which is used to build various types layouts. Like LinearLayout and AbsoluteLayout, TableLayout allows for a grid-based interface. You can read more about this in the general object layout section of the API documentation.
Linking your layout to code
After saving the layout, try running the application in the emulator by pressing Ctrl + F11, or click the Run icon in Eclipse. Now, instead of the "Hello World" message, you will see that Android now displays the app interface.
If you press any button they will light up as expected, but don't do anything other than that. Let's continue writing code following the layout interface:
# /src/com/example/brewclock/BrewClockActivity.java ... import android.widget.Button; import android.widget.TextView; public class BrewClockActivity extends Activity ( /** Properties **/ protected Button brewAddTime; protected Button brewDecreaseTime; protected Button startBrew; protected TextView brewCountLabel; protected TextView brewTimeLabel; ... )
Next, we'll change the onCreate call. This call occurs whenever an application is launched on Android. In the code generated by Eclipse, onCreate creates an activity view on R.layout.main. This is the line of code that gives Android command decode the XML document layout and display it to the user.
Resource Object
In Android, R is special object, which is automatically created to provide access to your project's resources (layouts, rows, menus, icons...) inside the code. Each resource is assigned its own id. In the layout file (above) these are the @+id XML attributes. We'll use these to bind the Buttons and TextViews in our layout:
# /src/com/example/brewclock/BrewClockActivity.java ... public class BrewClockActivity extends Activity ( ... public void onCreate(Bundle savedInstanceState) ( super.onCreate(savedInstanceState); setContentView(R.layout.main); / / Connect interface elements to properties brewAddTime = (Button) findViewById(R.id.brew_time_up); brewDecreaseTime = (Button) findViewById(R.id.brew_time_down); startBrew = (Button) findViewById(R.id.brew_start); brewCountLabel = (TextView) findViewById(R.id.brew_count_label); brewTimeLabel = (TextView) findViewById(R.id.brew_time); ) )
Listening to events
To detect when buttons are pressed, we need to listen for them. You may be familiar with callback listeners, or callbacks, from other frameworks such as Javascript/JQuery or Rails.
Android provides a similar mechanism by providing Listener interfaces, such as OnClickListener, which define methods that will fire when an event occurs. The OnClickListener interface will notify your application when the user clicks on the screen or a specific button. In addition, you need to tell each button about the ClickListener:
# /src/com/example/brewclock/BrewClockActivity.java ... // Be sure not to import // `android.content.dialoginterface.OnClickListener`. import android.view.View.OnClickListener; public class BrewClockActivity extends Activity implements OnClickListener ( ... public void onCreate(Bundle savedInstanceState) ( ... // Setup ClickListeners brewAddTime.setOnClickListener(this); brewDecreaseTime.setOnClickListener(this); startBrew.setOnClickListener(this); ) .. . public void onClick(View v) ( // TODO: Add code to handle button taps ) )
Next, we'll add code that handles each of our button clicks. We will also add four new Activity properties that will allow the user to set and track brewing times: how many brews have been made and whether the timer has started.
# /src/com/example/brewclock/BrewClockActivity.java ... public class BrewClockActivity extends Activity implements OnClickListener ( ... protected int brewTime = 3; protected CountDownTimer brewCountDownTimer; protected int brewCount = 0; protected boolean isBrewing = false; . .. public void onClick(View v) ( if(v == brewAddTime) setBrewTime(brewTime + 1); else if(v == brewDecreaseTime) setBrewTime(brewTime -1); else if(v == startBrew) ( if( isBrewing) stopBrew(); else startBrew(); ) ) )
Note that we are using the CountDownTimer class provided by Android. This makes it easy to create and run a simple counter and receive notifications at regular intervals as the countdown progresses. You will use this in the startBrew method, read about it below.
The following methods are models of the behavior of the slider for setting the cooking time, starting and stopping cooking, as well as the counter. We also initialize the brewTime and brewCount properties in onCreate.
It would be good practice to move this code into a separate model class, but for simplicity we'll add the code to the BrewClockActivity:
# /src/com/example/brewclock/BrewClockActivity.java ... public class BrewClockActivity extends Activity implements OnClickListener ( ... public void onCreate(Bundle savedInstanceState) ( ... // Set the initial brew values setBrewCount(0); setBrewTime(3); ) /** * Set an absolute value for the number of minutes to brew. * Has no effect if a brew is currently running. * @param minutes The number of minutes to brew. */ public void setBrewTime( int minutes) ( if(isBrewing) return; brewTime = minutes; if(brewTime< 1) brewTime = 1; brewTimeLabel.setText(String.valueOf(brewTime) + "m"); } /** * Set the number of brews that have been made, and update * the interface. * @param count The new number of brews */ public void setBrewCount(int count) { brewCount = count; brewCountLabel.setText(String.valueOf(brewCount)); } /** * Start the brew timer */ public void startBrew() { // Create a new CountDownTimer to track the brew time brewCountDownTimer = new CountDownTimer(brewTime * 60 * 1000, 1000) { @Override public void onTick(long millisUntilFinished) { brewTimeLabel.setText(String.valueOf(millisUntilFinished / 1000) + "s"); } @Override public void onFinish() { isBrewing = false; setBrewCount(brewCount + 1); brewTimeLabel.setText("Brew Up!"); startBrew.setText("Start"); } }; brewCountDownTimer.start(); startBrew.setText("Stop"); isBrewing = true; } /** * Stop the brew timer */ public void stopBrew() { if(brewCountDownTimer != null) brewCountDownTimer.cancel(); isBrewing = false; startBrew.setText("Start"); } ... }
Only the Android-specific parts of this code create label mappings using the setText method. In startBrew we create and run a CountDownTimer to start a second-by-second countdown when the brewing/boiling is finished. Note that we are defining CountDownTimer listeners (onTick and onFinish). onTick will be called every 1000 milliseconds (1 second), while onFinish will be called when the timer reaches zero.
Avoiding complexity in Android programming
To save this development guide under Android simple, I intentionally used labels directly in the code (for example, "Brew Up!", "Start", "Stop"). Actually, it's not very good practice, as it makes it difficult to find and change these strings in large projects.
Android provides a neat way of keeping your text strings separate from your R object code. R allows you to define all of an application's strings in an XML file (res/values/strings.xml), which can be accessed in code via a link. For example:
# /res/values/strings.xml
Now if you want to change the Brew Up! something else, you need to change it once in the strings.xml file. If your application contains dozens of code files, having labels in one place makes a lot of sense!
Checking BrewClock
We've finished writing the code, and it's time to "try out" the application. Press "Run" or Ctrl + F11 to run BrewClock in the emulator. If everything went well, you will see the setup interface and you are ready to brew tea! Try setting different brew times and press Start to count down.
Summary
In this short introduction to Android development, you downloaded and installed the Android SDK and the Eclipse Android Development Tools (ADT) plugin. You have set up an emulator or virtual appliance to test your applications. You also created a working Android application which highlighted a number of key concepts , which you will use when developing future Android applications.
Hopefully this will whet your appetite to create mobile apps and experiment in this exciting field. Android offers a great way to write applications for a range of current and future mobile devices. If you have already written a working application for this platform, do not forget to let us know in the comments!
The Android SDK offers a custom plugin for the Eclipse IDE called Android Development Tools (ADT). This plugin provides a powerful integrated environment in which you can develop Android applications. This extends Eclipse's capabilities, allowing you to quickly create new Android projects, create an application interface, debug the application, export signatures for application packages (APK) and distribute them.
Note: You should install the ADT plugin if you already have Eclipse installed and want to continue using it. If Eclipse was not installed, you should install the full one instead Android package SDK which includes the latest IDE for Android developers.
Your existing installation Eclipse for Android must meet the following requirements:
- Eclipse 3.7.2 (Indigo) or higher
Note: Eclipse 3.6 (Helios) is no longer supported with the latest version of ADT.
- Eclipse JDT plugin (included in most Eclipse IDE packages)
- JDK 6 (JRE is not enough)
- Not compatible with GNU Compiler for Java (GCJ)
Download the ADT plugin
To add the ADT Eclipse plugin for Android:
- Launch Eclipse, then select Help > Install New Software.
- Click Add in the top right corner.
- In the Add Repository dialog, in the dialog box that appears, enter "ADT Plugin" for Name ( Name) and the following address for Location (location): https : //dl-ssl.google.com/android/eclipse/
Note: Android Developer Tools requires a secure connection when updating. Make sure the update URL you enter starts with HTTPS.
- Click OK.
- In the Available Software dialog box, check the box next to Developer Tools and click Further.
- Further.
- Read and accept the terms of the license agreement, and then click Ready.
If you receive a security warning that the authenticity and validity of software cannot be installed, click anyway OK.
- After installation is complete, restart Eclipse.
Setting up the ADT plugin
Once Eclipse for Android has restarted, you need to specify the location of your Android SDK directory:
- In the "Welcome" window that appears, select Use existing SDKs (Use existing SDKs).
- Specify the location of the Android SDK directory where you recently downloaded and extracted the files.
- Click Further.
Your Eclipse IDE is built for development Android applications, but you must add the new platform SDK tools and the Android platform for your environment. To get these packages for your SDK, continue Adding Platforms and Packages.
Troubleshooting ADT Installation
If you are having trouble loading the ADT plugin after following the steps above and you are running behind a firewall (such as a corporate firewall), make sure you have configured your proxy settings correctly in Eclipse for Android. In Eclipse, you can configure a proxy from the Eclipse main menu, under Window(on Mac OS X, Eclipse)> Preferences> General > Network Connections.
If you are still unable to use Eclipse, you can download the ADT archive at local computer and install it manually:
- Download the ADT Plugin zip archive (do not extract it):
Plastic bag Size MD5 amount ADT-23.0.2.zip 103287135 bytes - Launch Eclipse, then select Help > Install New Software (Help > Install new software).
- Click Add in the upper right corner.
- In the Add Repository dialog, click Archive.
- Select the downloaded ADT-23.0.2.zip file and click OK.
- Enter "ADT Plugin" as the name and click OK.
- In the Available Software dialog box, check the box next to Developer Tools and click Further.
- In the next window you will see a list of tools that will be downloaded. Click Next.
- Read and accept the terms of the license agreement, and then click Ready.
If you receive a security warning that the authenticity and validity of the software cannot be determined, click anyway OK .
- Once installation is complete, restart Eclipse for Android.
Other installation errors
Please note that ADT has its own quirks. For example, some optional packages for Eclipse (such as WST) are required. If you encounter an error installing ADT, your Eclipse may not include these packages. For information on how to quickly add the required packages for your Eclipse installation, see the topic troubleshooting.
For Linux users
If this error occurred while installing the ADT Plugin for Eclipse:
An error occurred during provisioning. Cannot connect to keystore. JKSThis means your system does not have a suitable Java VM. Installing Sun Java 6 will resolve this issue and you can then reinstall the ADT Plugin.
Problem
You want to use the Eclipse environment to develop your application for the Android platform.
Solution
Install the Eclipse environment and the AndMore plugin. Then create your project and start writing the application. Build it and test it using an emulator in Eclipse.
Discussion
Before you can start building an application using the Eclipse framework, you need to install three items:
- Eclipse IDE
- Android SDK
- AndMore module
For more information about installing these elements, see.
Once you have done this, create a new Android project using the File → New menu and you will see a screen like the one in Fig. 1.
Select the SDK version for the target device (Figure 3). Version 4.0 covers almost all devices in use today; later versions provide even more features. You decide!
Rice. 1.
Rice. 2.
Rice. 3.
In Fig. Figure 4 shows the project structure expanded in the project panel on the right. It also shows to what extent you can use Eclipse autocomplete on the Android platform - I added a gravity attribute to the label, and the Eclipse framework suggests full list possible attribute values. I chose central-horizontal so the shortcut should be centered when the app launches.
Rice. 4.
If you set the gravity attribute in the LinearLayout to center_vertical and in the TextView to center_horizontal , the text will be centered both vertically and horizontally. Listing 1 shows the corresponding main.xml layout file (located in the res/layout directory).
Listing 1. File HelloWorld. java
As always, when you save a source file, Eclipse generates a compiled version. In an Android project, it also creates a compiled APK package that is ready to run, so you only need to run it. Right click on the project and select Run command As → Android Application (Run as → Application for the Android platform) (Fig. 5).
This article will describe how to set up Eclipse for developing Android applications. I will list all the necessary software: 1) JDK 6 (Java Development Kit) 2) Eclipse Classic 3) Android SDK (Software Development Kit) 4) ADT (Android Development Tools - Tools for Android development ) - plugin for Eclipse JDK The JDK installation process is described in the article: Eclipse After installing the JDK, proceed to installing Eclipse. Download the latest version of Eclipse from the official website eclipse.org. Click on the bookmark Downloads, you will be presented with various builds of Eclipse. For our purposes we need classic Eclipse - Eclipse Classic(at the time of writing latest version was 4.2.2). Unpack the downloaded archive into a directory c:\eclipse(or wherever is more convenient for you) For convenience, create a shortcut to executable file eclipse.exe. Run eclipse. When you first start it, you will be asked to select the directory of your workspace. Workspaces group projects. If one workspace is enough for you, you can set the flag "Use this as the default and do not ask again". By setting this flag, eclipse will not display this window the next time, and will immediately open your default workspace. Android SDK Go to the download page http://d.android.com/sdk This page suggests an assembly that contains everything you need for Android development. But we will go a different way. We will download only the Android SDK. Select an item "DOWNLOAD FOR OTHER PLATFORMS"(download for other platforms), in the table called "SDK Tools Only"(SDK only) Download the Windows SDK installer (*.exe). After reading and agreeing to the license agreement, download this installer. Before Android installation You need to make sure the SDK is configured environment variable JAVA_HOME(), otherwise the installer will crash without finding the JDK. Run the installer installer_r21.1-windows.exe. In the welcome window, click Next
In the next step, the installer determines the location JDK by system variable JAVA_HOME. If the JDK is found, click the button "Next".
In the user selection window, specify the option "Install for anyone this computer"(install for all computer users). If you want to install only for yourself, then select the second option "Install just for me". Click "Next".
In the installation location selection window, leave everything as default and click "Next".
In the shortcut creation window in the menu, you can also leave everything unchanged and click .
After the installation is completed successfully, click "Next".
In the final installer window, leave the checkbox "Start SDK Manager", to start it SDK Manager, after pressing the button "Finish".
Once launched, SDK Manager will prompt you to install the latest version of the API. If you need previous versions, indicate them and click the button "Install packages..."
Agree to the license "Accept License" and press
The download of your selected packages from the Internet will begin. A.D.T. Installing the ADT plugin for Eclipse. Launch Eclipse. On the menu Help select "Install New Software...". Click the button "Add..."(Add) The add storage window will appear:
Indicate the name there "Android Development Tools" and location address "https://dl-ssl.google.com/android/eclipse/". Click "OK" After a few seconds of waiting, an entry should appear in the list "Development Tools", tick it and click "Next". In the next window also click "Next". In the review of the license agreement, agree to it and click "Finish". During the installation process a window will appear Security Warning, just click in it OK. Once the plugin installation is complete A.D.T. you will be prompted to restart Eclipse, accept this. On startup Eclipse The following error may appear:
Eclipse can't find the SDK because we installed the default SDK in the directory: C:\Program Files (x86)\Android\android-sdk. To reassign the path to the SDK, open the menu Windows -> Preferences. In the window Preferences in field SDK Location indicate the path to Android SDK and press OK.
All Eclipse ready for development Android-applications.
Android provides a special plugin for the Eclipse IDE called Android Development Tools (ADT). It provides a powerful integrated development environment in which you can make Android applications. ADT extends the capabilities of Eclipse so that you can quickly and easily create a new Android project, design user interface application, debug the application, and export APK files to distribute your application.
Comment: If you prefer to work in another IDE, then you do not need to install either Eclispe or ADT. Instead, you can directly use the SDK's capabilities to build and debug your application.
Download the ADT plugin
- Launch Eclipse, select Help > Install New Software.
- Click Add in the upper right corner.
- In the Add Repository dialog that appears, enter "ADT Plugin" in the field Name and the following URL in the field Location:
https://dl-ssl.google.com/android/eclipse/
- Click OK.
- In the Available Software dialog, check the box next to Developer Tools and click Next.
- In the next window you will see a list of tools to download. Click Next.
- Read and accept the license agreement, then click Finish.
- After installation is complete, restart Eclipse.
Setting up the ADT plugin
After restarting Eclipse you must specify the directory with your Android SDK:
- Select Use existing SDKs in the window that appears.
- Find and select the directory containing the Android SDK that you recently downloaded and extracted.
- Click Next.
Troubleshoot installation problems
If you have difficulty downloading the ADT plugin after the above steps, here are some solutions:
- If Eclipse cannot find the remote update site that contains the ADT plugin, then try changing the site link to
Http://dl-ssl.google.com/android/eclipse/
- If you have a firewall (or enterprise firewall) enabled, make sure you have entered the correct proxy settings in Eclipse. In Eclipse you can configure proxy from the main menu from Window(on Mac OS X, Eclipse)> Preferences > General > Network Connections.
If you are still unable to download the ADT plugin using Eclipse from the remote update site, you can download the ADT zip file to your computer and install it manually:
For Linux users
If you encounter problems installing the ADT plugin for Eclipse like:
An error occurred during provisioning. Cannot connect to keystore. JKS
This means that your computer does not have a suitable Java VM. Installing Sun Java 6 will solve this problem and then you can reinstall the ADT plugin.