summaryrefslogtreecommitdiff
path: root/docs/html/guide/developing/other-ide.jd
blob: 8cdf0b91d7b80e4a807b2a7f90fb246323787a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
page.title=Developing In Other IDEs
@jd:body

<div id="qv-wrapper">
<div id="qv">
  <h2>In this document</h2>
  <ol>
    <li><a href="#CreatingAProject">Creating an Android Project</a></li>
    <li><a href="#Signing">Preparing to Sign Your Application</a></li>
    <li><a href="#Building">Building Your Application</a>
      <ol>
        <li><a href="#DebugMode">Building in debug mode</a></li>
        <li><a href="#ReleaseMode">Building in release mode</a></li>
      </ol>
    </li>
    <li><a href="#Running">Running Your Application</a></li>
    <li><a href="#AttachingADebugger">Attaching a Debugger to Your Application</a></li>
  </ol>

  <h2>See also</h2>
  <ol>
    <li><a href="{@docRoot}guide/developing/tools/othertools.html#android">android Tool</a></li>
    <li><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></li>
    <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a></li>
  </ol>
</div>
</div>

<p>The recommended way to develop an Android application is to use 
<a href="{@docRoot}guide/developing/eclipse-adt.html">Eclipse with the ADT plugin</a>.
The ADT plugin provides editing, building, debugging, and .apk packaging and signing functionality 
integrated right into the IDE.</p>

<p>However, if you'd rather develop your application in another IDE, such as IntelliJ,
or in a basic editor, such as Emacs, you can do that instead. The SDK
includes all the tools you need to set up an Android project, build it, debug it and then
package it for distribution. This document is your guide to using these tools.</p>


<h2 id="EssentialTools">Essential Tools</h2>

<p>When developing in IDEs or editors other than Eclipse, you'll require
familiarity with the following Android SDK tools:</p>

<dl>
  <dt><a href="{@docRoot}guide/developing/tools/othertools.html#android">android</a></dt>
    <dd>To create/update Android projects and to create/move/delete AVDs.</dd>
  <dt><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></dt>
    <dd>To run your Android applications on an emulated Android platform.</dd>
  <dt><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a></dt>
    <dd>To interface with your emulator or connected device (install apps,
    shell the device, issue commands, etc.).
    </dd>
</dl>

<p>In addition to the above tools, included with the SDK, you'll use the following
open source and third-party tools:</p>

<dl>
  <dt>Ant</dt>
    <dd>To compile and build your Android project into an installable .apk file.</dd>
  <dt>Keytool</dt>
    <dd>To generate a keystore and private key, used to sign your .apk file.</dd>
  <dt>Jarsigner (or similar signing tool)</dt>
    <dd>To sign your .apk file with a private key generated by keytool.</dd>
</dl>

<p>In the topics that follow, you'll be introduced to each of these tools as necessary.
For more advanced operations, please read the respective documentation for each tool.</p>


<h2 id="CreatingAProject">Creating an Android Project</h2>

<p>To create an Android project, you must use the <code>android</code> tool. When you create
a new project with <code>android</code>, it will generate a project directory
with some default application files, stub files, configuration files and a build file.</p>


<h3 id="CreatingANewProject">Creating a new Project</h3>

<p>If you're starting a new project, use the <code>android create project</code>
command to generate all the necessary files and folders.</p>

<p>To create a new Android project, open a command-line,
navigate to the <code>tools/</code> directory of your SDK and run:</p>
<pre>
android create project \
--target <em>&lt;targetID&gt;</em> \
--path <em>/path/to/your/project</em> \
--activity <em>&lt;your_activity_name&gt;</em> \
--package <em>&lt;your_package_namespace&gt;</em>
</pre>

<ul>
  <li><code>target</code> is the "build target" for your application. It corresponds
  to an Android platform library (including any add-ons, such as Google APIs) that you would like to
  build your project against. To see a list of available targets and their corresponding IDs, 
  execute: <code>android list targets</code>.</li>
  <li><code>path</code> is the location of your project directory. If the directory does not exist,
  it will be created for you.</li>
  <li><code>activity</code> is the name for your {@link android.app.Activity} class. This class file
  will be created for you inside 
  <code><em>&lt;path_to_your_project&gt;</em>/src/<em>&lt;your_package_namespace_path&gt;</em>/</code>.</li>
  <li><code>package</code> is the package namespace for your project, following the same rules as for
  packages in the Java programming language.</li>
</ul>

<p>Here's an example:</p>
<pre>
android create project \
--target 1 \
--path ./myProject \
--activity MyActivity \
--package com.example.myproject
</pre>

<p>The tool generates the following files and directories:</p>

<ul>
  <li><code>AndroidManifest.xml</code> - The application manifest file, 
  synced to the specified Activity class for the project.</li>
  <li><code>build.xml</code> - Build file for Ant.</li>
  <li><code>default.properties</code> - Properties for the build system. <em>Do not modify 
  this file</em>.</li>
  <li><code>build.properties</code> - Customizable properties for the build system. You can edit this 
  file to overried default build settings used by Ant.</li>
  <li><code>src<em>/your/package/namespace/ActivityName</em>.java</code> - The Activity class 
  you specified during project creation.</li>
  <li><code>bin/</code>  - Output directory for the build script.</li>
  <li><code>gen/</code>  - Holds <code>Ant</code>-generated files, such as <code>R.java</code>. </li>
  <li><code>libs/</code>  - Holds private libraries.</li>
  <li><code>res/</code>  - Holds project resources.</li>
  <li><code>src/</code>  - Holds source code.</li>
  <li><code>tests/</code>  - Holds a duplicate of all-of-the-above, for testing purposes.</li>
</ul>

<p>Once you've created your project, you're ready to begin development.
You can move your project folder wherever you want for development, but keep in mind
that you must use the <a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> 
(adb) &mdash; located in the SDK <code>tools/</code> directory &mdash; to send your application 
to the emulator (discussed later). So you need access between your project solution and 
the <code>tools/</code> folder.</p>

<p class="warning"><strong>Note:</strong> You should refrain from moving the
location of the SDK directory, because this will break the build scripts. (They
will need to be manually updated to reflect the new SDK location before they will
work again.)</p>


<h3 id="UpdatingAProject">Updating a project</h3>

<p>If you're upgrading a project from an older version of the Android SDK or want to create
a new project from existing code, use the
<code>android update project</code> command to update the project to the new development
environment. You can also use this command to revise the build target of an existing project
(with the <code>--target</code> option). The <code>android</code> tool will generate any files and 
folders (listed in the previous section) that are either missing or need to be updated, 
as needed for the Android project.</p>

<p>To update an existing Android project, open a command-line
and navigate to the <code>tools/</code> directory of your SDK. Now run:</p>
<pre>
android update project --target <em>&lt;targetID&gt;</em> --path <em>path/to/your/project/</em>
</pre>

<ul>
  <li><code>target</code> is the "build target" for your application. It corresponds to 
  an Android platform library (including any add-ons, such as Google APIs) that you would 
  like to build your project against. To see a list of available targets and their corresponding IDs, 
  execute: <code>android list targets</code>.</li>
  <li><code>path</code> is the location of your project directory.</li>
</ul>

<p>Here's an example:</p>
<pre>
android update project --target 2 --path ./myProject
</pre>


<h2 id="Signing">Preparing to Sign Your Application</h2>

<p>As you begin developing Android applications, understand that all
Android applications must be digitally signed before the system will install
them on an emulator or device. There are two ways to do this:
with a <em>debug key</em> (for immediate testing on an emulator or development device)
or with a <em>private key</em> (for application distribution).</p>

<p>The Android build tools help you get started by automatically signing your .apk
files with a debug key at build time. This means
that you can compile your application and install it on the emulator without
having to generate your own private key. However, please note that if you intend
to publish your application, you <strong>must</strong> sign the application with your
own private key, rather than the debug key generated by the SDK tools. </p>

<p>Please read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your
Applications</a>, which provides a thorough guide to application signing on Android
and what it means to you as an Android application developer.</p>



<h2 id="Building">Building Your Application</h2>

<p>There are two ways to build your application: one for testing/debugging your application
&mdash; <em>debug mode</em> &mdash; and one for building your final package for release &mdash; 
<em>release mode</em>. As described in the previous
section, your application must be signed before it can be installed on an emulator
or device.</p>

<p>Whether you're building in debug mode or release mode, you
need to use the Ant tool to compile and build your project. This will create the .apk file
that is installed onto the emulator or device. When you build in debug mode, the .apk
file is automatically signed by the SDK tools with a debug key, so it's instantly ready for installation
(but only onto an emulator or attached development device).
When you build in release mode, the .apk file is <em>unsigned</em>, so you must manually
sign it with your own private key, using Keytool and Jarsigner.</p>

<p>It's important that you read and understand 
<a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>, particularly
once you're ready to release your application and share it with end-users. That document describes
the procedure for generating a private key and then using it to sign your .apk file.
If you're just getting started, however, 
you can quickly run your applications on an emulator or your own development device by building in
debug mode.</p>

<p>If you don't have Ant, you can obtain it from the 
<a href="http://ant.apache.org/">Apache Ant home page</a>. Install it and make
sure it is in your executable PATH. Before calling Ant, you need to declare the JAVA_HOME 
environment variable to specify the path to where the JDK is installed.</p>

<p class="note"><strong>Note:</strong> When installing JDK on Windows, the default is to install 
in the "Program Files" directory. This location will cause <code>ant</code> to fail, because of 
the space. To fix the problem, you can specify the JAVA_HOME variable like this: 
<code>set JAVA_HOME=c:\Prora~1\Java\<jdkdir></code>. The easiest solution, however, is to 
install JDK in a non-space directory, for example: <code>c:\java\jdk1.6.0_02</code>.</p>


<h3 id="DebugMode">Building in debug mode</h3>

<p>For immediate application testing and debugging, you can build your application 
in debug mode and immediately install it on an emulator. In debug mode, the build tools automatically
sign your application with a debug key. However, you can (and should) also test your
application in release mode. Debug mode simply allows you to run your application without
manually signing the application.</p>

<p>To build in debug mode:</p>

<ol>
  <li>Open a command-line and navigate to the root of your project directory.</li>
  <li>Use Ant to compile your project in debug mode:
    <pre>ant debug</pre>
    <p>This creates your Android application .apk file inside the project <code>bin/</code>
    directory, named <code><em>&lt;your_DefaultActivity_name&gt;</em>-debug.apk</code>. The file
    is already signed with the debug key.</p>
  </li>
</ol>

<p>Each time you change a source file or resource, you must run Ant 
again in order to package up the latest version of the application.</p>

<p>To install and run your application on an emulator, see the following section
about <a href="#Running">Running Your Application</a>.</p>


<h3 id="ReleaseMode">Building in release mode</h3>

<p>When you're ready to release and distribute your application to end-users, you must build
your application in release mode. Once you have built in release mode, it's a good idea to perform 
additional testing and debugging with the final .apk.</p>

<p>To build in release mode:</p>

<ol>
  <li>Open a command-line and navigate to the root of your project directory.</li>
  <li>Use Ant to compile your project in release mode:
    <pre>ant release</pre>
    <p>This creates your Android application .apk file inside the project <code>bin/</code>
    directory, named <code><em>&lt;your_DefaultActivity_name&gt;</em>.apk</code>.</p>
    <p class="note"><strong>Note:</strong> The .apk file is <em>unsigned</em> at this point.
    You can't install it on an emulator or device until you sign it with your private key.</p>
  </li>
</ol>

<p>Because release mode builds your application unsigned, your next step is to sign
it with your private key, in order to distribute it to end-users. To complete this procedure, 
read <a href="{@docRoot}guide/publishing/app-signing.html">Signing Your Applications</a>.</p>

<p>Once you have signed your application with a private key, you can install it on an 
emulator or device as discussed in the following section about 
<a href="#Running">Running Your Application</a>.
You can also try installing it onto a device from a web server.
Simply upload the signed APK to a web site, then load the .apk URL in your Android web browser to 
download the application and begin installation.
(On your device, be sure you have enabled <em>Settings > Applications > Unknown sources</em>.)</p>


<h2 id="Running">Running Your Application</h2>

<p>Unless you'll be running your application on device hardware, 
you need to launch an emulator upon which you will install your application.
An instance of the Android emulator runs a specific Android platform with specific device configuration
settings. The platform and configuration is defined with an Android Virtual Device (AVD). 
So before you can launch your emulator, you must define an AVD.</p>

<p>If you'll be running your application on device hardware, please read about
<a href="{@docRoot}guide/developing/device.html">Developing On a Device</a> instead.</p>

<ol>
  <li><strong>Create an AVD</strong>
    <ol>
      <li>Open a command-line and navigate to your SDK package's 
      <code>tools/</code> directory.</li>
      <li>First, you need to select a "deployment target." To view available targets, execute:
        <pre>android list targets</pre>
        <p>This will output a list of available Android targets, such as:</p>
<pre>
id:1
    Name: Android 1.1
    Type: platform
    API level: 2
    Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id:2
    Name: Android 1.5
    Type: platform
    API level: 3
    Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
</pre>
        <p>Find the target that matches the Android platform upon which you'd like
        to run your application. Note the integer value of the <code>id</code> &mdash;
        you'll use this in the next step.</p>
      </li>
      <li>Create a new AVD using your selected deployment target:
        <pre>android create avd --name <em>&lt;your_avd_name&gt;</em> --target <em>&lt;targetID&gt;</em></pre>
      <li>Next, you'll be asked whether you'd like to create a custom hardware profile.
      If you respond "yes," you'll be presented with a series of prompts to define various aspects of the
      device hardware (leave entries blank to use default values, which are shown in brackets). Otherwise,
      press return to use all default values ("no" is the default).</li>
      </li>
    </ol>
  </li>

  <li><strong>Launch an emulator</strong></li>
    <p>From your SDK's <code>tools/</code> directory, launch an emulator 
      using an existing AVD (created above):
    <pre>emulator -avd <em>&lt;your_avd_name&gt;</em></pre>
    <p>An instance of the emulator will now launch, running the target and configuration 
      defined by your AVD.</p>
  </li>
  
  <li><strong>Install your application</strong>
    <p>From your SDK's <code>tools/</code> directory, install the .apk on the emulator:
    <pre>adb install <em>/path/to/your/application</em>.apk</pre>
    <p>If there is more than one emulator running, you must specify the emulator upon which to install
    the application, by its serial number, with the <code>-s</code> option. For example:</p>
    <pre>adb -s emulator-5554 install /my/project/path/myapp.apk</pre>
  </li>
  <li><strong>Open your application</strong>
    <p>In the emulator, open the list of available applications to find 
    and open your application.</p>
  </li>
</ol>

<p>If you don't see your application on the emulator. Try restarting the emulator
(with the same AVD). Sometimes when you install an Activity for the
first time, it won't show up in the application launcher or be accessible by other 
applications. This is because the package manager usually examines manifests 
completely only on emulator startup.</p>

<p class="note"><strong>Tip:</strong> If you have only one emulator running, 
you can build your application and install it on the emulator in one simple step. 
Navigate to the root of your project directory and use Ant to compile the project 
with <em>install mode</em>:
<code>ant install</code>. This will build your application, sign it with the debug key, 
and install it on the currently running emulator.
If there is more than one emulator currently running
when using the <code>install</code> command, it will fail &mdash; it can't select between the 
multiple emulators.</p>

<p>For more information on the tools used above, please see the following documents:</p>
<ul>
  <li><a href="{@docRoot}guide/developing/tools/othertools.html#android">android Tool</a></li>
  <li><a href="{@docRoot}guide/developing/tools/emulator.html">Android Emulator</a></li>
  <li><a href="{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> (ADB)</li>
</ul>


<h2 id="AttachingADebugger">Attaching a Debugger to Your Application</h2>

<p>This section describes how to display debug information on the screen (such
    as CPU usage), as well as how to hook up your IDE to debug running applications
    on the emulator. </p>

<p>Attaching a debugger is automated using the Eclipse plugin,
    but you can configure other IDEs to listen on a debugging port to receive debugging
    information:</p>
<ol>
    <li><strong>Start the <a href="{@docRoot}guide/developing/tools/ddms.html">Dalvik Debug Monitor 
      Server (DDMS)</a> tool, </strong> which
        acts as a port forwarding service between your IDE and the emulator.</li>
    <li><strong>Set 
        optional debugging configurations on
        your emulator</strong>, such as blocking application startup for an Activity
        until a debugger is attached. Note that many of these debugging options
        can be used without DDMS, such as displaying CPU usage or screen refresh
        rate on the emulator.</li>
    <li><strong>Configure your IDE to attach to port 8700 for debugging.</strong> Read
        about <a href="{@docRoot}guide/developing/debug-tasks.html#ide-debug-port">
        Configuring Your IDE to Attach to the Debugging Port</a>. </li>
</ol>