690 likes | 841 Views
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
E N D
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 • The unit is not intended as a comprehensive treatment of these topics
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?
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
This section starts with some information taken from Wikipedia, starting on the following overhead
APK (file format)From Wikipedia, the free encyclopedia • Androidapplication 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]
APK files are ZIP file formatted packages based on the JAR file format, with .apkfile extensions. The MIME type associated with APK files is application/vnd.android.package-archive.[5]
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.apkin 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
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
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
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
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
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
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)