Download XAndroid3Exploring

Document related concepts
no text concepts found
Transcript
Android 3: Exploring Apps and
the Development Environment
Kirk Scott
1
2
Introduction
• This unit surveys some of the code aspects of
an Android app
• It also surveys how you can find these aspects
of an app through the Eclipse interface
• The unit is intended as an introduction, not as
a comprehensive treatment of these topics
3
• It is intended to answer the questions that
might spring to the mind of a programmer
who is seeing app development for the first
time
• It is also motivated by this simple goal:
• How could you change MyFirstApp, the “Hello
World” app so that it displayed a different
message?
4
• The outline for this unit is shown on the
following overheads
• Some of the points in the outline contain path
names
• These path names correspond to parts of the
project as they would appear in the Project
Explorer in Eclipse
5
Outline
• 3.1 The Project Explorer
• 3.2 /src/com.example.myfirstapp/
Main_Activity.java
• 3.3 /gen/com.example.myfirstapp/
BuildConfig.java
• 3.4 /gen/com.example.myfirstapp/R.java
• 3.5 Android 4.3 and Android Private Libraries
6
•
•
•
•
•
•
3.6 /bin/res/AndroidManifest.xml
3.7 /bin/res/MyFirstApp.apk
3.8 /res/layout/activity_main.xml
3.9 /res/menu/main.xml
3.10 /res/values/strings.xml
3.11 Grand Finale
7
3.1 The Project Explorer
8
• The screenshot on the following overhead
shows the hello world app as it was shown in
the previous overheads
• The key point of interest at this moment is the
Project Explorer on the left hand side
• As given here, the MyFirstApp folder has been
expanded to show the items immediately
underneath it
9
10
• No matter where’ve you’ve gotten to in
Eclipse, you can always restore the Project
Explorer
• Take these options in the menu:
• Window, Show View, Project Explorer
11
Expanded View of the Project in the
Explorer
• The screenshot on the following overhead
shows the subfolders in the Project Explorer
expanded to show the items in the outline of
this unit
• The following sections of this unit look at
items of interest in these expanded folders
one after the other from top to bottom
12
13
• Once things are expanded, not everything is
visible in the screenshot at the bottom
• If you are following along on your own
machine, depending on the setup, you may
have to scroll down to see some of it
14
• In the screenshot, Android 4.3 hasn’t been
expanded
• It will be dealt with separately
• Future versions of these overheads may
continue to show Android 4.3 even if the
version number has changed, assuming that
nothing else about it that’s being explained
here has changed
15
3.2 /src/com.example.myfirstapp/Main_Activity.java
16
• The screenshot on the overhead following the
next one shows what you see when you
double click on Main_Activity.java in the
Project Explorer
• This is essentially the Java source code for the
app
• Notice that unlike a Java application, there is
no main() method
17
• The app code is slightly reminiscent of applet
code, if you are familiar with that
• The app class extends the Activity class
• Instead of a main() method it has (overrides)
an onCreate() method
• Most of the details mean nothing at this point,
but note that in the code, reference is made
to a class name R
18
19
3.3 /gen/com.example.myfirstapp/BuildConfig.java
20
• If you double click on BuildConfig.java in the
Project Explorer you see what’s shown on the
overhead following the next one
• This is an auto-generated file
• It’s worth knowing that it exists, but for all
practical purposes, we will not be doing
anything with this
21
• In the previous unit it wasn’t necessary to go
through building a configuration step-by-step
• We let the configuration come into existence
by default instead
• The point is that an app that has been brought
to the point of being runnable will have a
build configuration, whether by default or by
design
22
23
3.4 /gen/com.example.myfirstapp/R.java
24
• If you double click on R.java in the Project
Explorer you see what’s shown on the
overhead following the next one
• R.java is another auto-generated file
• It is too soon for details, but it is worth
knowing that R.java is kind of a global
container associated with an app
• In contrast with the build config, we will be
working with R
25
• Note that R contains final declarations, the
declarations of constants associated with the
app
• It’s also apparent that hexadecimal values are
being used
• Our practical interest later on will be to see
how to make use of the constants defined in
the file R in our own app code
26
27
3.5 Android 4.3 and Android Private
Libraries
28
• The screenshot on the overhead following the
next one shows a subset of what you see when
you double click on Android 4.3 in the Project
Explorer, on the left
• (The file R.java is still showing in the editor)
• The Android 4.3 folder is a library which contains
Android packages and if you were to scroll down
further, you would find Java packages
• The Android Private Libraries folder is similar
29
• In effect, what you’re seeing is the set of API
packages available when creating Android
apps
• There is nothing more to say about this with
respect to the Project Explorer
• The Android API is documented on the
developers Web page
• If something is in the API you can use it in your
code
30
31
Preliminary Note to Sections 3.6-3.10
• It is worth noting that the rest of the path names
of things of interest in the Project Explorer
descend from the res folder
• res is short for resource
• Android makes use of Java code, in
MainActivity.java, for example
• An app may also make use of resources of various
kinds
• They are defined separately from the code and
are stored in folders that descend from res
32
3.6 /bin/res/AndroidManifest.xml
33
• The screenshot on the overhead following the
next one shows what you see when you
double click on AndroidManifest.xml in the
Project Explorer
• You may recall that jar files have manifest files
• An Android apk file, the result of building a
project, is effectively a kind of jar file
• Therefore, every completed app will have a
manifest file associated with it
34
• This is another instance where we are relying
on the manifest file that’s created by default
• Recall that there was a sequence of screens in
the wizard for creating My First App
• Somewhere in that process the manifest was
specified
• We will not have a need in this course, but it is
also possible to use a wizard or directly edit a
manifest file to give it special characteristics
35
36
3.7 /bin/res/MyFirstApp.apk
37
• This section starts with some information
taken from Wikipedia, starting on the
following overhead
38
APK (file format)
From Wikipedia, the free encyclopedia
• Android application package file (APK) is the file
format used to distribute and install application
software and middleware onto Google's Android
operating system. To make an APK file, a program
for Android is first compiled, and then all of its
parts are packaged into one file. This holds all of
that program's code (such as .dex files),
resources, assets, certificates, and manifest file.
As is the case with many file formats, APK files
can have any name needed, but must end with
the four character, three letter extension,
.apk.[1][2][3][4]
39
• APK files are ZIP file formatted packages based
on the JAR file format, with .apk file
extensions. The MIME type associated with
APK files is application/vnd.android.packagearchive.[5]
40
• In short, the apk file is a jar file for an Android
app, which packages up the manifest and the
compiled source code for distribution
• The screenshot on the following overhead
shows what you see when you double click on
MyFirstApp.apk in the Project Explorer
• Not surprisingly, it looks more or less like what
you see when you open up a class file in an
editor—binary nonsense
41
42
3.8 /res/layout/activity_main.xml
43
• The screenshot on the following overhead shows
what you see when you double click on
activity_main.xml under /res/layout in the
Project Explorer
• As seen before, this is the layout of the output of
the app as shown in the development
environment
• The layout is developed separately from the code
logic, so it’s important to be able to find
activity_main.xml in order to be able to work
with it
44
45
• Consider the contents of the screenshot again:
• On the left, there is the Eclipse Project
Explorer, with the folders expanded
• In the center, there is a view of the graphical
layout of the app
• This visible graphical layout is defined by the
file activity_main.xml
46
• Between the explorer and the layout is a
palette of graphical tools and components for
creating visual layouts for apps
• At the bottom of the editor are two tabs, one
for Graphical Layout and one simply showing
the name of the file, activity_main.xml
• Clicking on the activity_main.xml tab shows
you the xml source code, as shown on the
following overhead
47
48
•
•
•
•
The layout file includes layout syntax
It also includes lines like this:
android:text="@string/hello_world“
This refers to a resource belonging to the app
which is defined elsewhere in the
environment
• The relationship between resources and
references is an important aspect of app
development
49
3.9 /res/menu/main.xml
50
• The screenshot on the following overhead shows
what you see when you double click on main.xml
under /res/menu in the Project Explorer
• There is nothing of consequence here for the
moment
• It is simply included to bring to your attention the
fact that the layout you’re familiar with is
/res/layout/activity_main.xml, not what you see
here
51
52
3.10 /res/values/strings.xml
53
• The screenshot on the following overhead
shows the Project Explorer scrolled down to
show /res/values/strings.xml
• It also shows what you see when you double
click on strings.xml
• (Note that of the two tabs at the bottom of
the editor screen, you need to be on
strings.xml, not Resources, if you want to see
the XML source code)
54
55
What about strings.xml?
• strings.xml is the last item on this tour of
things to be found in the explorer
• Practically speaking, it’s also the most
significant at this point
• We are finally in the place where we can make
a simple, initial modification to the app
56
• In the original version of the app, this line
appeared in strings.xml:
• <string name="hello_world">Hello
world!</string>
• If you look carefully at the foregoing
screenshot, you’ll see that the line has been
changed to this:
• <string name="hello_world">Good-Bye Cruel
World!</string>
57
• The syntax in strings.xml gives a name to the
reference, “hello_world”
• In between the <> and <>, the value
associated with that reference is given, in one
case Hello World! and in the other case Goodbye Cruel World!
• These different strings are resources that can
be used by an app
58
• The code for the app displays a resource by
reference
• The resource, a string, is defined in
strings.xml, separate from the app code
• This separation of code/logic from
content/resources is a significant feature of
Android development that will be dwelled on
in following sets of overheads
59
3.11 Grand Finale
60
• At this point, if you’ve made changes to
strings.xml, you can go back to the
MainActivity.java, as shown on the following
screenshot
• A change in the output string of the app
requires absolutely no change in the Java
source code
61
62
• From MainActivity.java, you can run your
application, whether on the emulator or on an
attached device
• When you click run, you’ll be prompted to
save the changes to strings.xml if you didn’t
save out of that editor screen
• Ta-Da: The following screenshot shows
success on the emulator
63
64
Summary and Mission
• This is the end of the initial presentation of the
components of an app that can be found in the
Project Explorer
• High points of what can be found there:
• MainActivity.java, the source code
• activity_main.xml, the layout
• strings.xml, the file containing the string
resources for an app
• R.java, the file containing resources as defined in
the Java code for the app
65
• You have two missions, neither of which are
graded homework:
• 1. Create a new Android project and modify it so
that its output is not “Hello World”
• This should work, and should consist essentially
of changing strings.xml
• The point of this mission is obviously not the
importance of the change
• The point is finding strings.xml and reinforcing
what relationship it has with the app code
66
• 2. Things related to this were mentioned only
in passing in this set of overheads, but it’s not
too soon for you to conduct a small
experiment in preparation for coming
attractions
• Using the palette of graphical tools for
activity_main.xml, drag and drop some new
item into the layout for an app
67
• Note the contents of R.java before building
the project
• Then try building the project and consider two
things:
• A. Do you get error messages?
• If so, what are they, and what do they imply?
• B. Were there any changes in R.java?
68
• The point of the second mission is not
necessarily for you to have a firm grasp on
what’s going on
• The point is just to have you find
activity_main.xml, fiddle with the graphical
tools palette, and then find R.java in the
explorer
69
The End
70