720 likes | 852 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 a comprehensive treatment of these topics
The unit is intended to answer the questions that might come to the mind of a programmer who is seeing app development for the first time • It includes this simple example as a way of getting familiar with apps: • How do you change MyFirstApp, the “Hello World” app so that it will display a different message?
The outline for this unit is given on the following overhead • The outline corresponds to parts of the project as they can be found in the Project Explorer in Eclipse • At first sight the outline will probably not mean much • The point is that various parts of the app will be found through the explorer, shown, and briefly explained
Outline • 3.1 The Project Explorer • 3.2 /src/com.example.myfirstapp/ Main_Activity.java • 3.3 /res/layout/activity_main.xml • 3.4 /res/values/strings.xml • 3.5 /gen/com.example.myfirstapp/R.java • 3.6 Other Things in the Project Explorer • 3.7 Changing the App
The screenshot on the following overhead shows the hello world app as it was shown in the previous overheads • The point of interest at this moment is the Project Explorer • It ison the left hand side of the screen, and in this screen shot, various folders have been expanded
You will always want the explorer open in order to navigate through the parts of a project • If, by chance, the explorer has disappeared from view in your copy of Eclipse, you can always restore it • Take these options in the menu: • Window, Show View, Project Explorer
In the screen shot on the following overhead, the MyFirstApp folder has been expanded to show the items underneath it • In it you will find the MainActivity.java source file for the app • By double clicking on it, you can open it in the editor • This is what is showing in the editor in the screen shot
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 • Note that in the code, reference is made to a class name R • This is one of the parts that will be shown shortly
In some programming languages, the layout, or visual appearance of an application is defined within its source code • A significant feature of Android development is that the layout is defined separately from the code • We saw the Java code for MyFirstApp in MainActivity.java in the previous section
The screenshot on the following overhead shows the layout of the app • You open thiswhen you double click on activity_main.xml under /res/layout in the Project Explorer • This is the view of the layout that you get when the Graphical Layout tab has been selected at the bottom of the editor
The screen shot on the following overhead shows the actual source code for the layout • You see this when the activity_main.xml tab has been selected at the bottom of the editor screen • The layout is defined in XML—not in Java code
On the following overhead the graphical layout is shown again • When you are in this view, this is what you also see: • Between the explorer and the layout is a palette of graphical tools and components for creating visual layouts for apps • Developing in Eclipse involves becoming familiar with these graphical tools
On the following overhead the source file is shown again • The layout file includes layout syntax • The syntax isn’t of particular interest at the moment • However, it’s worth noting that some of the keywords are descriptive enough to give some idea of what’s going on
In addition to giving information about the project explorer, the example is illustrating various things about Android apps • To repeat: • The Java code for the app is saved in one file • The layout is defined in XML and saved in another file • You need to be able to find each, so you can work with them
Consider the XML source code for the layout of the app one more time • Look in the following screen shot and you will not find the string “Hello World!” which the app produces as output • Strictly speaking, “Hello World!” is the output of the app, but it is not part of the layout
What represents the string “Hello World!” in the layout? • It is this: • android:text="@string/hello_world"
This refers to a resource belonging to the app which is defined elsewhere in the environment • Not only are the Java and XML files saved separately in an Android app • The resources are also saved separately
The screenshot on the following overhead shows the Project Explorer open to show /res/values/strings.xml • The editor 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)
This is where you find the string “Hello World!” • This string is a resource of the app • The Java source code or XML layout code for an app displays a resource by reference • As noted earlier, in the layout file, this is how “Hello World!” is referred to: • android:text="@string/hello_world"
The resource, a string, is defined in strings.xml, separate from the Java and XML code • This separation of resources from code is a significant element of Android development • It will be explained in greater detail in following sets of overheads
The screenshot on the following overhead shows the project explorer open to the file R.java in the editor • R.java is an auto-generated file the results when an app is successfully compiled • R.java is kind of a global container associated with the app
The onCreate() method in MainActivity.java contained the following line of code: • setContentView(R.layout.activity_main);
It’s too soon for an elaborate explanation • However, this much is already apparent: • The Java source code refers to the XML layout for the app through the R.java file • Different aspects of an app are stored in different files • R.java establishes some names by which XML components are accessible in Java code
A closer look shows that R.java contains final declarations • These declarations represent the constants associated with the app • It’s also apparent that hexadecimal values are being used • A discussion of how to use R.java when developing an app will come later
There are many other things that can be found through the project explorer • We will not consider the majority of them • A few more will be mentioned briefly • It is not necessary to understand these things as much more than vocabulary at the moment
/gen/com.exmple.myfirstapp/BuildConfig.java • If you double click on BuildConfig.java in the Project Explorer you see what’s shown on the following overhead • This is an auto-generated file • It is stored along side R.java
Building is roughly synonymous with compiling • An app that has been brought to the point of being runnable will have a build configuration • It is possible to create a build configuration step-by-step • It is also possible to let the build configuration come into existence by default • That’s what we’ll do for the time being
Android 4.x and Android Private Libraries • The screenshot on the following overhead shows a subset of what you see when you double click on Android 4.x in the Project Explorer, on the left • (The file R.java is still showing in the editor) • The Android 4.x 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
/bin/res/MyFirstApp.apk • 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]