TP 1
TP 1
TP 1
Après avoir installé avec succès l'IDE Android Studio, vous créez, à partir d'un modèle, un
nouveau projet Android pour l'application 'Hello World'. Cette application simple affiche la
chaîne "Hello World" sur l'écran du périphérique virtuel et physique d'Android.
Tâches à réaliser :
Android Studio is Google's IDE for Android apps. Android Studio gives you an advanced
code editor and a set of app templates. In addition, it contains tools for development,
debugging, testing, and performance that make it faster and easier to develop apps. You can
test your apps with a large range of preconfigured emulators or on your own mobile device,
and build production APKs for publication.
Note: Android Studio is continually being improved. For the latest information on system
requirements and installation instructions, refer to the documentation at
developer.android.com.
You may need to install the Java Development Kit - Java 7 or better.
1/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
Android Studio is available for Windows, Mac, and Linux computers. The installation is
similar for all platforms. The are only some minor differences.
2/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
Open a terminal window and enter Type java -version again to verify that installation
has been successful.
Set the JAVA_HOME environment variable to the installation directory of the JDK.
For Windows:
Navigate to the Android developers siteand follow the instructions to download and install
Android Studio.
3/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
Troubleshooting: If you run into problems with your installation, check the latest
documentation, programming forums,or get help from the instructor.
In this task, you will implement the "Hello World" app to verify that Android studio is
correctly installed and learn the basics of developing with Android Studio.
9. If your project requires additional components for your chosen target SDK, Android
Studio will install them automatically. Click Next.
10. Customize the Activity window. Every app needs at least one activity. An activity
represents a single screen with auser interface and Android Studio provides templates
to help you get started. For the Hello World project, choose the simplest template (as
of this writing, the "Empty Activity" project template is the simplest template)
available.
11. It is a common practice to call your main activity MainActivity. This is not a
requirement.
12. Make sure the Generate Layout file box is checked (if visible).
13. Make sure the Backwards Compatibility (App Compat) box is checked.
14. Leave the Layout Name as activity_main. It is customary to name layouts after the
activity they belong to. Accept the defaults and click Finish.
The Android Studio window should look similar to the following diagram:
5/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
You can look at the hierarchy of the files for your app in multiple ways.
1. Click on the Hello World folder to expand the hierarchy of files (1),
2. Click on Project (2).
3. Click on the Android menu (3).
4. Explore the different view options for your project.
In the Project > Android view of your previous task, there are three top-level folders below
your app folder: manifests, java, and res.
This folder contains AndroidManifest.xml. This file describes all of the components of your
Android app and is read by the Android run-time system when your program is executed.
2. Expand the java folder. All your Java language files are organized in this folder. The
java folder contains three subfolders:
com.example.hello.helloworld (or the domain name you have specified):
All the files for a package are in afolder named after the package. For your
Hello World application, there is one package and it only contains
MainActivity.java (the file extension may be omitted in the Project view).
com.example.hello.helloworld(androidTest): This folder is for your
instrumented tests, and starts out with askeleton test file.
com.example.hello.helloworld(test): This folder is for your unit tests and
starts out with an automatically createdskeleton unit test file.
3. Expand the res folder. This folder contains all the resources for your app, including
images, layout files, strings, icons, and styling. It includes these subfolders:
drawable. Store all your app's images in this folder.
layout. Every activity has at least one layout file that describes the UI in XML.
For Hello World, this folder containsactivity_main.xml.
6/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
mipmap. Store your launcher icons in this folder. There is a sub-folder for
each supported screen density. Androiduses the screen density, that is, the
number of pixels per inch to determine the required image resolution. Android
groups all actual screen densities into generalized densities, such as medium
(mdpi), high (hdpi), or extra-extra-extra-high (xxxhdpi). The ic_launcher.png
folder contains the default launcher icons for all the densities supported by
your app.
values. Instead of hardcoding values like strings, dimensions, and colors in
your XML and Java files, it is bestpractice to define them in their respective
values file. This makes it easier to change and be consistent across your app.
4. Expand the values subfolder within the res folder. It includes these subfolders:
colors.xml. Shows the default colors for your chosen theme, and you can add
your own colors or change thembased on your app's requirements.
dimens.xml. Store the sizes of views and objects for different resolutions.
strings.xml. Create resources for all your strings. This makes it easy to
translate them to other languages. styles.xml. All the styles for your app and
theme go here. Styles help give your app a consistent look for all UIelements.
Android Studio uses Gradleas its build system. As you progress through these practicals, you
will learn more about gradle and what you need to build and run your apps.
1. Expand the Gradle Scripts folder. This folder contains all the files needed by the
build system.
2. Look for the build.gradle(Module:app) file. When you are adding app-specific
dependencies, such as using additional libraries, they go into this file.
In this task, you will use the Android Virtual Device (AVD) managerto create a virtual device
or emulator that simulates the configuration for a particular type of Android device.
7/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
Using the AVD Manager, you define the hardware characteristics of a device and its API
level, and save it as a virtual device configuration.
When you start the Android emulator, it reads a specified configuration and creates an
emulated device that behaves exactly like a physical version of that device , but it resides on
your computer .
Why: With virtual devices, you can test your apps on different devices (tablets, phones) with
different API levels to makesure it looks good and works for most users. You do not need to
depend on having a physical device available for app development.
In order to run an emulator on your computer, you have to create a configuration that
describes the virtual device.
1. In Android Studio, select Tools > Android > AVD Manager, or click the AVD
8/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
4. On the System Image screen, from the Recommended tab, choose which version of
the Android system to run on the virtual device. You can select the latest system
image.
There are many more versions available than shown in the Recommended tab. Look
at the x86 Images and OtherImages tabs to see them.
5. If a Download link is visible next to a system image version, it is not installed yet, and
you need to download it. If necessary, click the link to start the download, and click
Finish when it's done.
6. On System Image screen, choose a system image and click Next.
7. Verify your configuration, and click Finish. (If the Your Android Devices AVD
Manager window stays open, you can go ahead and close it.)
In this task, you will finally run your Hello World app.
1. In Android Studio, select Run > Run app or click the Run icon in the toolbar.
2. In the Select Deployment Target window, under Available Emulators, select Nexus
5 API 23 and click OK.
The emulator starts and boots just like a physical device. Depending on the speed of your
computer, this may take a while.
Your app builds, and once the emulator is ready, Android Studio will upload the app to the
emulator and run it.
You should see the Hello World app as shown in the following screenshot.
9/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
10/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
Note: When testing on an emulator, it is a good practice to start it up once, at the very
beginning of your session. Youshould not close the emulator until you are done testing your
app, so that your app doesn't have to go through the boot process again.
Travail à faire
You may notice that not all combinations of devices and system versions work when you run
your app. This is because not all system images can run on all hardware devices.
In this practical, you will add log statements to your app, which are displayed in the logging
window of the Android Monitor.
Why: Log messages are a powerful debugging tool that you can use to check on values,
execution paths, and report exceptions.
1. Click the Android Monitor button at the bottom of Android Studio to open the
Android Monitor.
By default, this opens to the logcat tab, which displays information about your app as it
is running. If you add logstatements to your app, they are printed here as well.You can
also monitor the Memory, CPU, GPU, and Network performance of your app from the
other tabs of the
2. Android Monitor. This can be helpful for debugging and performance tuning your
code.
3. The default log level is Verbose. In the drop-down menu, change the log level to
Debug.
11/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
Log statements that you add to your app code print a message specified by you in the logcat
tab of the Android Monitor.For example:
12/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
1. Open your Hello World app in Android studio, and open MainActivity file.
2. File > Settings > Editor > General >Auto Import . Select all check boxes and set
Insert imports on paste to All. Unambiguous imports are now addedautomatically to
your files. Note the "add unambiguous imports on the fly" option is important for
some Android features such as NumberFormat. If not checked, NumberFormat shows
an error. Click on 'Apply' followed by clicking on the 'Ok' button.
3. In the onCreate method, add the following log statement:
4. If the Android Monitor is not already open, click the Android Monitor tab at the
bottom of Android Studio to open it. (Seescreenshot.)
5. Make sure that the Log level in the Android Monitor logcat is set to Debug or Verbose
(default).
6. Run your app.
Travail à faire
A common use of the Log class is to log Java exceptions when they occur in your program.
There are someuseful methods in the Log class that you can use for this purpose. Use the Log
class documentationto find out what methods you can use to include an exception with a log
message. Then, write code in the MainActivity.java file to trigger and log an exception.
In this task you will find and read the AndroidManifest.xml file for the Hello World app.
Why: As your apps add more functionality and the user experience becomes more engaging
and interactive, theAndroidManifest.xml file contains more and more information.
Travail à faire:
1. Open your Hello World app in Android studio, and in the manifests folder, open
AndroidManifest.xml.
2. Read the file and consider what each line of code indicates. The code below is
annotated to give you some hints.
3. There are many other elements that can be set in the Android Manifest. Explore
theAndroid Manifestdocumentation and learn about additional elements in the
Android Manifest.
Android Studio uses a build system called Gradle. Gradle does incremental builds, which
allows for shorter edit-test cycles.
Gradle site
Configure your build developerdocumentation
Search the internet for "gradle tutorial".
Why: When you add new libraries to your Android project, you may also have to update your
build.gradle file. It's useful toknow where it is and its basic structure.
Travail à faire:
In your project hierarchy, find Gradle Scripts and expand it. There several build.gradle files.
One with directives for your whole project, and one for each app module. The module for
14/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
your app is called "app". In the Project view, it is represented by the app folder at the top-
level of the Project view.
2. Read the file and learn what each line of code indicates.
For a deeper look into Gradle check out the Build System Overviewand Configuring Gradle
Buildsdocumentation.
There are tools to help you shrink your code, remove unnecessary libraries/resource and even
obfuscate your program to prevent unwanted reverse-engineering.
Android Studio itself provides some useful features. Learn more about a valuable open-source
tool called ProGuard.
In this final task, you will run your app on a physical mobile device such as a phone or tablet.
Why: Your users will run your app on physical devices. You should always test your apps on
both virtual and physicaldevices.
If you are using a Linux or Windows OS, you may need to perform additional steps to run on
a hardware device. Check the Using Hardware Devicesdocumentation. On Windows, you
may need to install the appropriate USB driver for your device. See OEM USB Drivers.
To let Android Studio communicate with your device, you must turn on USB Debugging on
your Android device. This is enabled in the Developer options settings of your device. Note
this is not the same as rooting your device.
15/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
On Android 4.2 and higher, the Developer options screen is hidden by default. To show
Developer options and enable USB Debugging:
1. On your device, open Settings > About phone and tap Build number seven times.
2. Return to the previous screen (Settings). Developer options appears at the bottom of
the list. Click Developeroptions.
3. Choose USB Debugging.
Now you can connect your device and run the app from Android Studio.
3. Click the Run button in the toolbar. The Select Deployment Target window
opens with the list of available emulators and connected devices.
4. Select your device, and click OK.
Troubleshooting
Android Studio should install and runs the app on your device.
If you Android Studio does not recognize your device, try the following:
16/21
Faculté Polydisciplinaire Beni Mellal
Module13 :« Programmation Mobile »
Master STRI
Année universitaire : 2019/2020
You may need to install the appropriate USB driver for your device. See the Using
Hardware Devices documentation.
Check the latest documentation, programming forums, or get help from your
instructors.
Travail à faire
Now that you are set up and familiar with the basic development workflow, do the following:
2. Change the greeting to "Happy Birthday to " and someone with a recent birthday.
4. Take a screenshot of your finished app and email it to someone whose birthday you forgot.
17/21