65
Android 3: Exploring Apps and the Development Environment Kirk Scott 1

Android 3: Exploring Apps and the Development Environment

Embed Size (px)

DESCRIPTION

Android 3: Exploring Apps and the Development Environment. Kirk Scott. 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 - PowerPoint PPT Presentation

Citation preview

1

Android 3: Exploring Apps and the Development Environment

Kirk Scott

2

3

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 not intended as a comprehensive treatment of these topics

4

• 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?

5

• The outline for this unit is shown on the following overheads

• As you will see, this outline corresponds to parts of the project as they would appear in the Project Explorer in Eclipse

6

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

7

• 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

8

3.1 The Project Explorer

9

• 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

10

11

• 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

12

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

13

14

• 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

15

• 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

16

3.2 /src/com.example.myfirstapp/Main_Activity.java

17

• 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

18

• 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

19

20

3.3 /gen/com.example.myfirstapp/BuildConfig.java

21

• 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 the

time being the details aren’t important

22

• 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

• In any case, an app that has been brought to the point of being runnable will have a build configuration

23

24

3.4 /gen/com.example.myfirstapp/R.java

25

• 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

26

• Note that it contains final declarations, the declarations of constants associated with the app

• It’s also apparent that hexadecimal values are being used

• It will be of interest later to see how to make use of the constants defined in the file R

27

28

3.5 Android 4.3 and Android Private Libraries

29

• The screenshot on the following overhead 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• In effect, what you’re seeing is the set of API packages

available when creating Android apps

30

31

3.6 /bin/res/AndroidManifest.xml

• The screenshot on the following overhead 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

32

33

3.7 /bin/res/MyFirstApp.apk

34

• This section starts with some information taken from Wikipedia, starting on the following overhead

35

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]

36

• 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.package-archive.[5]

37

• 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

38

39

3.8 /res/layout/activity_main.xml

40

• 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

41

42

• 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

43

• 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

44

45

• 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

46

3.9 /res/menu/main.xml

47

• 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

48

49

3.10 /res/values/strings.xml

50

• 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)

51

52

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

53

• 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>

54

• 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 is a significant feature of Android development that will be dwelled on in following sets of overheads

55

3.11 Grand Finale

56

• 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

57

58

• 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

59

60

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

61

• 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

62

• 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

63

• 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?

64

• 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

65

The End