blob: 8cdf0b91d7b80e4a807b2a7f90fb246323787a98 [file] [log] [blame]
Scott Mainbd13c242009-04-21 19:09:24 -07001page.title=Developing In Other IDEs
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002@jd:body
3
Scott Mainbd13c242009-04-21 19:09:24 -07004<div id="qv-wrapper">
5<div id="qv">
6 <h2>In this document</h2>
7 <ol>
8 <li><a href="#CreatingAProject">Creating an Android Project</a></li>
9 <li><a href="#Signing">Preparing to Sign Your Application</a></li>
10 <li><a href="#Building">Building Your Application</a>
11 <ol>
12 <li><a href="#DebugMode">Building in debug mode</a></li>
13 <li><a href="#ReleaseMode">Building in release mode</a></li>
14 </ol>
15 </li>
16 <li><a href="#Running">Running Your Application</a></li>
17 <li><a href="#AttachingADebugger">Attaching a Debugger to Your Application</a></li>
18 </ol>
19
20 <h2>See also</h2>
21 <ol>
Scott Main8a4c53a2009-04-24 13:41:44 -070022 <li><a href="{@docRoot}guide/developing/tools/othertools.html#android">android Tool</a></li>
Scott Mainbd13c242009-04-21 19:09:24 -070023 <li><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></li>
24 <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a></li>
25 </ol>
26</div>
27</div>
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029<p>The recommended way to develop an Android application is to use
Scott Mainbd13c242009-04-21 19:09:24 -070030<a href="{@docRoot}guide/developing/eclipse-adt.html">Eclipse with the ADT plugin</a>.
31The ADT plugin provides editing, building, debugging, and .apk packaging and signing functionality
32integrated right into the IDE.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Scott Mainbd13c242009-04-21 19:09:24 -070034<p>However, if you'd rather develop your application in another IDE, such as IntelliJ,
35or in a basic editor, such as Emacs, you can do that instead. The SDK
36includes all the tools you need to set up an Android project, build it, debug it and then
37package it for distribution. This document is your guide to using these tools.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39
Scott Mainbd13c242009-04-21 19:09:24 -070040<h2 id="EssentialTools">Essential Tools</h2>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Scott Mainbd13c242009-04-21 19:09:24 -070042<p>When developing in IDEs or editors other than Eclipse, you'll require
43familiarity with the following Android SDK tools:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Scott Mainbd13c242009-04-21 19:09:24 -070045<dl>
Scott Main8a4c53a2009-04-24 13:41:44 -070046 <dt><a href="{@docRoot}guide/developing/tools/othertools.html#android">android</a></dt>
Scott Mainbd13c242009-04-21 19:09:24 -070047 <dd>To create/update Android projects and to create/move/delete AVDs.</dd>
48 <dt><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></dt>
49 <dd>To run your Android applications on an emulated Android platform.</dd>
50 <dt><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a></dt>
51 <dd>To interface with your emulator or connected device (install apps,
52 shell the device, issue commands, etc.).
53 </dd>
54</dl>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Scott Mainbd13c242009-04-21 19:09:24 -070056<p>In addition to the above tools, included with the SDK, you'll use the following
57open source and third-party tools:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Scott Mainbd13c242009-04-21 19:09:24 -070059<dl>
60 <dt>Ant</dt>
61 <dd>To compile and build your Android project into an installable .apk file.</dd>
62 <dt>Keytool</dt>
63 <dd>To generate a keystore and private key, used to sign your .apk file.</dd>
64 <dt>Jarsigner (or similar signing tool)</dt>
65 <dd>To sign your .apk file with a private key generated by keytool.</dd>
66</dl>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Scott Mainbd13c242009-04-21 19:09:24 -070068<p>In the topics that follow, you'll be introduced to each of these tools as necessary.
69For more advanced operations, please read the respective documentation for each tool.</p>
70
71
72<h2 id="CreatingAProject">Creating an Android Project</h2>
73
74<p>To create an Android project, you must use the <code>android</code> tool. When you create
75a new project with <code>android</code>, it will generate a project directory
76with some default application files, stub files, configuration files and a build file.</p>
77
78
79<h3 id="CreatingANewProject">Creating a new Project</h3>
80
81<p>If you're starting a new project, use the <code>android create project</code>
82command to generate all the necessary files and folders.</p>
83
84<p>To create a new Android project, open a command-line,
85navigate to the <code>tools/</code> directory of your SDK and run:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086<pre>
Scott Mainbd13c242009-04-21 19:09:24 -070087android create project \
88--target <em>&lt;targetID&gt;</em> \
89--path <em>/path/to/your/project</em> \
90--activity <em>&lt;your_activity_name&gt;</em> \
91--package <em>&lt;your_package_namespace&gt;</em>
92</pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94<ul>
Scott Mainbd13c242009-04-21 19:09:24 -070095 <li><code>target</code> is the "build target" for your application. It corresponds
96 to an Android platform library (including any add-ons, such as Google APIs) that you would like to
97 build your project against. To see a list of available targets and their corresponding IDs,
98 execute: <code>android list targets</code>.</li>
99 <li><code>path</code> is the location of your project directory. If the directory does not exist,
100 it will be created for you.</li>
101 <li><code>activity</code> is the name for your {@link android.app.Activity} class. This class file
102 will be created for you inside
103 <code><em>&lt;path_to_your_project&gt;</em>/src/<em>&lt;your_package_namespace_path&gt;</em>/</code>.</li>
104 <li><code>package</code> is the package namespace for your project, following the same rules as for
105 packages in the Java programming language.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106</ul>
107
Scott Mainbd13c242009-04-21 19:09:24 -0700108<p>Here's an example:</p>
109<pre>
110android create project \
111--target 1 \
112--path ./myProject \
113--activity MyActivity \
114--package com.example.myproject
115</pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Scott Mainbd13c242009-04-21 19:09:24 -0700117<p>The tool generates the following files and directories:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
Scott Mainbd13c242009-04-21 19:09:24 -0700119<ul>
120 <li><code>AndroidManifest.xml</code> - The application manifest file,
121 synced to the specified Activity class for the project.</li>
122 <li><code>build.xml</code> - Build file for Ant.</li>
123 <li><code>default.properties</code> - Properties for the build system. <em>Do not modify
124 this file</em>.</li>
125 <li><code>build.properties</code> - Customizable properties for the build system. You can edit this
126 file to overried default build settings used by Ant.</li>
127 <li><code>src<em>/your/package/namespace/ActivityName</em>.java</code> - The Activity class
128 you specified during project creation.</li>
129 <li><code>bin/</code> - Output directory for the build script.</li>
130 <li><code>gen/</code> - Holds <code>Ant</code>-generated files, such as <code>R.java</code>. </li>
131 <li><code>libs/</code> - Holds private libraries.</li>
132 <li><code>res/</code> - Holds project resources.</li>
133 <li><code>src/</code> - Holds source code.</li>
134 <li><code>tests/</code> - Holds a duplicate of all-of-the-above, for testing purposes.</li>
135</ul>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Scott Mainbd13c242009-04-21 19:09:24 -0700137<p>Once you've created your project, you're ready to begin development.
138You can move your project folder wherever you want for development, but keep in mind
139that you must use the <a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a>
140(adb) &mdash; located in the SDK <code>tools/</code> directory &mdash; to send your application
141to the emulator (discussed later). So you need access between your project solution and
142the <code>tools/</code> folder.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Scott Mainbd13c242009-04-21 19:09:24 -0700144<p class="warning"><strong>Note:</strong> You should refrain from moving the
145location of the SDK directory, because this will break the build scripts. (They
146will need to be manually updated to reflect the new SDK location before they will
147work again.)</p>
148
149
150<h3 id="UpdatingAProject">Updating a project</h3>
151
152<p>If you're upgrading a project from an older version of the Android SDK or want to create
153a new project from existing code, use the
154<code>android update project</code> command to update the project to the new development
155environment. You can also use this command to revise the build target of an existing project
156(with the <code>--target</code> option). The <code>android</code> tool will generate any files and
157folders (listed in the previous section) that are either missing or need to be updated,
158as needed for the Android project.</p>
159
160<p>To update an existing Android project, open a command-line
161and navigate to the <code>tools/</code> directory of your SDK. Now run:</p>
162<pre>
163android update project --target <em>&lt;targetID&gt;</em> --path <em>path/to/your/project/</em>
164</pre>
165
166<ul>
167 <li><code>target</code> is the "build target" for your application. It corresponds to
168 an Android platform library (including any add-ons, such as Google APIs) that you would
169 like to build your project against. To see a list of available targets and their corresponding IDs,
170 execute: <code>android list targets</code>.</li>
171 <li><code>path</code> is the location of your project directory.</li>
172</ul>
173
174<p>Here's an example:</p>
175<pre>
176android update project --target 2 --path ./myProject
177</pre>
178
179
180<h2 id="Signing">Preparing to Sign Your Application</h2>
181
182<p>As you begin developing Android applications, understand that all
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183Android applications must be digitally signed before the system will install
Scott Mainbd13c242009-04-21 19:09:24 -0700184them on an emulator or device. There are two ways to do this:
185with a <em>debug key</em> (for immediate testing on an emulator or development device)
186or with a <em>private key</em> (for application distribution).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Scott Mainbd13c242009-04-21 19:09:24 -0700188<p>The Android build tools help you get started by automatically signing your .apk
189files with a debug key at build time. This means
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190that you can compile your application and install it on the emulator without
191having to generate your own private key. However, please note that if you intend
Scott Mainbd13c242009-04-21 19:09:24 -0700192to publish your application, you <strong>must</strong> sign the application with your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193own private key, rather than the debug key generated by the SDK tools. </p>
194
Scott Mainbd13c242009-04-21 19:09:24 -0700195<p>Please read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your
196Applications</a>, which provides a thorough guide to application signing on Android
197and what it means to you as an Android application developer.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199
Scott Mainbd13c242009-04-21 19:09:24 -0700200
201<h2 id="Building">Building Your Application</h2>
202
203<p>There are two ways to build your application: one for testing/debugging your application
204&mdash; <em>debug mode</em> &mdash; and one for building your final package for release &mdash;
205<em>release mode</em>. As described in the previous
206section, your application must be signed before it can be installed on an emulator
207or device.</p>
208
209<p>Whether you're building in debug mode or release mode, you
210need to use the Ant tool to compile and build your project. This will create the .apk file
211that is installed onto the emulator or device. When you build in debug mode, the .apk
212file is automatically signed by the SDK tools with a debug key, so it's instantly ready for installation
213(but only onto an emulator or attached development device).
214When you build in release mode, the .apk file is <em>unsigned</em>, so you must manually
215sign it with your own private key, using Keytool and Jarsigner.</p>
216
217<p>It's important that you read and understand
218<a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>, particularly
219once you're ready to release your application and share it with end-users. That document describes
220the procedure for generating a private key and then using it to sign your .apk file.
221If you're just getting started, however,
222you can quickly run your applications on an emulator or your own development device by building in
223debug mode.</p>
224
225<p>If you don't have Ant, you can obtain it from the
226<a href="http://ant.apache.org/">Apache Ant home page</a>. Install it and make
227sure it is in your executable PATH. Before calling Ant, you need to declare the JAVA_HOME
228environment variable to specify the path to where the JDK is installed.</p>
229
230<p class="note"><strong>Note:</strong> When installing JDK on Windows, the default is to install
231in the "Program Files" directory. This location will cause <code>ant</code> to fail, because of
232the space. To fix the problem, you can specify the JAVA_HOME variable like this:
233<code>set JAVA_HOME=c:\Prora~1\Java\<jdkdir></code>. The easiest solution, however, is to
234install JDK in a non-space directory, for example: <code>c:\java\jdk1.6.0_02</code>.</p>
235
236
237<h3 id="DebugMode">Building in debug mode</h3>
238
239<p>For immediate application testing and debugging, you can build your application
240in debug mode and immediately install it on an emulator. In debug mode, the build tools automatically
241sign your application with a debug key. However, you can (and should) also test your
242application in release mode. Debug mode simply allows you to run your application without
243manually signing the application.</p>
244
245<p>To build in debug mode:</p>
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247<ol>
Scott Mainbd13c242009-04-21 19:09:24 -0700248 <li>Open a command-line and navigate to the root of your project directory.</li>
249 <li>Use Ant to compile your project in debug mode:
250 <pre>ant debug</pre>
251 <p>This creates your Android application .apk file inside the project <code>bin/</code>
252 directory, named <code><em>&lt;your_DefaultActivity_name&gt;</em>-debug.apk</code>. The file
253 is already signed with the debug key.</p>
254 </li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255</ol>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
Scott Mainbd13c242009-04-21 19:09:24 -0700257<p>Each time you change a source file or resource, you must run Ant
258again in order to package up the latest version of the application.</p>
259
260<p>To install and run your application on an emulator, see the following section
261about <a href="#Running">Running Your Application</a>.</p>
262
263
264<h3 id="ReleaseMode">Building in release mode</h3>
265
266<p>When you're ready to release and distribute your application to end-users, you must build
267your application in release mode. Once you have built in release mode, it's a good idea to perform
268additional testing and debugging with the final .apk.</p>
269
270<p>To build in release mode:</p>
271
272<ol>
273 <li>Open a command-line and navigate to the root of your project directory.</li>
274 <li>Use Ant to compile your project in release mode:
275 <pre>ant release</pre>
276 <p>This creates your Android application .apk file inside the project <code>bin/</code>
277 directory, named <code><em>&lt;your_DefaultActivity_name&gt;</em>.apk</code>.</p>
278 <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point.
279 You can't install it on an emulator or device until you sign it with your private key.</p>
280 </li>
281</ol>
282
283<p>Because release mode builds your application unsigned, your next step is to sign
284it with your private key, in order to distribute it to end-users. To complete this procedure,
285read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>.</p>
286
287<p>Once you have signed your application with a private key, you can install it on an
288emulator or device as discussed in the following section about
289<a href="#Running">Running Your Application</a>.
290You can also try installing it onto a device from a web server.
291Simply upload the signed APK to a web site, then load the .apk URL in your Android web browser to
292download the application and begin installation.
293(On your device, be sure you have enabled <em>Settings > Applications > Unknown sources</em>.)</p>
294
295
296<h2 id="Running">Running Your Application</h2>
297
298<p>Unless you'll be running your application on device hardware,
299you need to launch an emulator upon which you will install your application.
300An instance of the Android emulator runs a specific Android platform with specific device configuration
301settings. The platform and configuration is defined with an Android Virtual Device (AVD).
302So before you can launch your emulator, you must define an AVD.</p>
303
304<p>If you'll be running your application on device hardware, please read about
305<a href="{@docRoot}guide/developing/device.html">Developing On a Device</a> instead.</p>
306
307<ol>
308 <li><strong>Create an AVD</strong>
309 <ol>
310 <li>Open a command-line and navigate to your SDK package's
311 <code>tools/</code> directory.</li>
312 <li>First, you need to select a "deployment target." To view available targets, execute:
313 <pre>android list targets</pre>
314 <p>This will output a list of available Android targets, such as:</p>
315<pre>
316id:1
317 Name: Android 1.1
318 Type: platform
319 API level: 2
320 Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
321id:2
322 Name: Android 1.5
323 Type: platform
324 API level: 3
325 Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
326</pre>
327 <p>Find the target that matches the Android platform upon which you'd like
328 to run your application. Note the integer value of the <code>id</code> &mdash;
329 you'll use this in the next step.</p>
330 </li>
331 <li>Create a new AVD using your selected deployment target:
332 <pre>android create avd --name <em>&lt;your_avd_name&gt;</em> --target <em>&lt;targetID&gt;</em></pre>
333 <li>Next, you'll be asked whether you'd like to create a custom hardware profile.
334 If you respond "yes," you'll be presented with a series of prompts to define various aspects of the
335 device hardware (leave entries blank to use default values, which are shown in brackets). Otherwise,
336 press return to use all default values ("no" is the default).</li>
337 </li>
338 </ol>
339 </li>
340
341 <li><strong>Launch an emulator</strong></li>
342 <p>From your SDK's <code>tools/</code> directory, launch an emulator
343 using an existing AVD (created above):
344 <pre>emulator -avd <em>&lt;your_avd_name&gt;</em></pre>
345 <p>An instance of the emulator will now launch, running the target and configuration
346 defined by your AVD.</p>
347 </li>
348
349 <li><strong>Install your application</strong>
350 <p>From your SDK's <code>tools/</code> directory, install the .apk on the emulator:
351 <pre>adb install <em>/path/to/your/application</em>.apk</pre>
352 <p>If there is more than one emulator running, you must specify the emulator upon which to install
353 the application, by its serial number, with the <code>-s</code> option. For example:</p>
354 <pre>adb -s emulator-5554 install /my/project/path/myapp.apk</pre>
355 </li>
356 <li><strong>Open your application</strong>
357 <p>In the emulator, open the list of available applications to find
358 and open your application.</p>
359 </li>
360</ol>
361
362<p>If you don't see your application on the emulator. Try restarting the emulator
363(with the same AVD). Sometimes when you install an Activity for the
364first time, it won't show up in the application launcher or be accessible by other
365applications. This is because the package manager usually examines manifests
366completely only on emulator startup.</p>
367
368<p class="note"><strong>Tip:</strong> If you have only one emulator running,
369you can build your application and install it on the emulator in one simple step.
370Navigate to the root of your project directory and use Ant to compile the project
371with <em>install mode</em>:
372<code>ant install</code>. This will build your application, sign it with the debug key,
373and install it on the currently running emulator.
374If there is more than one emulator currently running
375when using the <code>install</code> command, it will fail &mdash; it can't select between the
376multiple emulators.</p>
377
378<p>For more information on the tools used above, please see the following documents:</p>
379<ul>
Scott Main8a4c53a2009-04-24 13:41:44 -0700380 <li><a href="{@docRoot}guide/developing/tools/othertools.html#android">android Tool</a></li>
Scott Mainbd13c242009-04-21 19:09:24 -0700381 <li><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></li>
382 <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> (ADB)</li>
383</ul>
384
385
386<h2 id="AttachingADebugger">Attaching a Debugger to Your Application</h2>
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388<p>This section describes how to display debug information on the screen (such
389 as CPU usage), as well as how to hook up your IDE to debug running applications
390 on the emulator. </p>
391
392<p>Attaching a debugger is automated using the Eclipse plugin,
393 but you can configure other IDEs to listen on a debugging port to receive debugging
Scott Mainbd13c242009-04-21 19:09:24 -0700394 information:</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395<ol>
Scott Mainbd13c242009-04-21 19:09:24 -0700396 <li><strong>Start the <a href="{@docRoot}guide/developing/tools/ddms.html">Dalvik Debug Monitor
397 Server (DDMS)</a> tool, </strong> which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 acts as a port forwarding service between your IDE and the emulator.</li>
399 <li><strong>Set
400 optional debugging configurations on
Scott Mainbd13c242009-04-21 19:09:24 -0700401 your emulator</strong>, such as blocking application startup for an Activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 until a debugger is attached. Note that many of these debugging options
403 can be used without DDMS, such as displaying CPU usage or screen refresh
404 rate on the emulator.</li>
Scott Mainbd13c242009-04-21 19:09:24 -0700405 <li><strong>Configure your IDE to attach to port 8700 for debugging.</strong> Read
406 about <a href="{@docRoot}guide/developing/debug-tasks.html#ide-debug-port">
407 Configuring Your IDE to Attach to the Debugging Port</a>. </li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408</ol>