Download BIM493 – Mobile Programming 1 Android Environment SDK

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
6.10.2016
BIM493 – Mobile Programming 1
Android Environment SDK
Hazırlayan
Öğr. Gör. Özgür ÖZŞEN
Mobile Programming 1 - Android Environment SDK
Development Environment
•
•
•
•
•
Android applications are usually created using the Java programming language.
Your Java project must import various Android Libraries (such as android.jar,
maps.jar, etc ) to gain the functionality needed to work inside the Android OS.
Even the simplest of Android apps is composed of several elements such as: userdefined classes, android jars, third-party libraries, XML files defining the UIs or
views, multimedia resources, data assets such as disk files, external arrays and
strings, databases, and finally a Manifest summarizing the ‘anatomy’ and
permissions requested by the app.
The package(s) holding the raw app components are given to the compiler to
obtain a single signed and deployable Android Package (an .apk file).
Like in Java, apk files are the byte-code version of the app that finally will be
‘executed’ by interpretation inside a Dalvik or ART Virtual Machine.
1
6.10.2016
Mobile Programming 1 - Android Environment SDK
Dalvik vs ART
•
•
•
•
•
•
•
Currently Android versions use the Dalvik virtual machine. The latest Android versions
introduced a new runtime the Android RunTime.
With Android 4.4, Google introduced the Android RunTime (ART) as optional runtime for
Android 4.4. It is uses as default runtime for all Android versions after 4.4.
ART uses Ahead Of Time compilation which can improve app performance. ART also has
tighter install-time verification than Dalvik.
During the deployment process of an application on an Android device, the application code
is translated into machine code. This results in approx. 30% larger compile code, but allows
faster execution from the beginning of the application.
This also saves battery life, as the compilation is only done once, during the first start of the
application.
The dex2oat tool takes the .dex file created by the Android tool change and compiles that
into an Executable and Linkable Format (ELF file). This file contains the dex code, compiled
native code and meta-data. Keeping the .dex code allows that existing tools still work.
The garbage collection in ART has been optimized to reduce times in which the application
freezes.
Mobile Programming 1 - Android Environment SDK
Development Environment
•
•
•
•
•
•
•
Creating, organizing and managing the components of an Android app is better done using a
‘friendly’ workbench.
The Google development team focus their future development on Android Studio, so this is
currently the best development environment for Android applications. If you still prefer the
Eclipse IDE as development environment for Android you should install Eclipse ADT tooling.
The ADT plugin extends Eclipse so you can easily reach the tools of the SDK through the use
of menus, perspectives and icons seamlessly integrated in the Eclipse’s IDE.
Eclipse IDE allows you to create and debug your Java code, and manage the various
resources that normally are used in the making of an Android app.
Google provides an IDE called Android Studio as the preferred development environment for
creating Android applications. This IDE is based on the IntelliJ IDE.
The Android tools provide specialized editors for Android specific files. Most of Android's
configuration files are based on XML. In this case these editors allow you to switch between
the XML representation of the file and a structured user interface for entering the data.
The SDK contains tools needed to transfer, profile, emulate, observe, and debug your
applications which could run into any virtual or physical Android device.
2
6.10.2016
Mobile Programming 1 - Android Environment SDK
Typical Layout of the Eclipse IDE for Android Development
Mobile Programming 1 - Android Environment SDK
Android Environment SDK
3
6.10.2016
Mobile Programming 1 - Android Environment SDK
Install SDK: Windows, Mac, Linux
•
We assume you have already installed the Java JDK and Eclipse IDE in your
computer
•
Java JDK is available at:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
•
Eclipse IDE for Java EE Developers is available at:
http://www.eclipse.org/downloads/
Mobile Programming 1 - Android Environment SDK
Development Environment = Eclipse + ADT + SDK
•
•
•
•
•
•
•
•
•
•
•
•
•
Aside Note:
SDKs are named after dessert items. Available
versions at the time of writing are:
1.5 Cupcake,
1.6 Donut,
2.1 Eclair,
2.2 Froyo,
2.3 Gingerbread [1],
3.x Honeycomb,
4.x Ice Cream Sandwich
4.3 Jelly Bean
4.4 KitKat
5.0 Lollipop
6.0 Marshmallow
7.0 Nougat
4
6.10.2016
Mobile Programming 1 - Android Environment SDK
Setup
Users Wanting to Update an
Older Android Workbench
If you are currently using the
Android SDK, you just need to
update to the latest tools or
platform using the already
installed Android SDK and AVD
Manager.
1. Click on the
SDK
Manager icon.
2. You will see a form similar to
the one on the right.
3. Select the Packages you want
to install and wait until they
are setup in your machine.
Mobile Programming 1 - Android Environment SDK
Reading the Android SDK Documentation (Cont’d)
The Android Software
Development Kit (Android
SDK) contains the
necessary tools to create,
compile and package
Android applications. Most
of these tools are command
line based. The primary way
to develop Android
applications is based on the
Java programming
language.
5
6.10.2016
Mobile Programming 1 - Android Environment SDK
Android Debug Bridge (adb)
• The Android SDK contains the Android debug bridge (adb),
which is a tool that allows you to connect to a virtual or real
Android device, for the purpose of managing the device or
debugging your application.
Mobile Programming 1 - Android Environment SDK
Gradle and the Android plug-in for Gradle
The Android tooling uses
Gradle as build system. The
Android team provides a
Gradle plug-in for build
Android applications which is
entered in the build.gradle
file in the top root of the
Android project. It typically
looks like the following,
please note that the version
might be different in your
case.
6
6.10.2016
Mobile Programming 1 - Android Environment SDK
How to develop Android applications
• Android applications are primarily written in the Java
programming language.
• During development the developer creates the Android
specific configuration files and writes the application logic in
the Java programming language.
• The Android tooling converts these application files,
transparently to the user, into an Android application. When
developers trigger the deployment in their IDE, the whole
Android application is compiled, packaged, deployed and
started.
Mobile Programming 1 - Android Environment SDK
Conversion process from source code to Android Application
• The Java source files are converted to Java class files by the
Java compiler.
• The Android SDK contains a tool called dx which converts Java
class files into a .dex (Dalvik Executable) file. All class files of
the application are placed in this .dex file. During this
conversion process redundant information in the class files
are optimized in the .dex file.
• For example, if the same String is found in different class files,
the .dex file contains only one reference of this String.
7
6.10.2016
Mobile Programming 1 - Android Environment SDK
Conversion process from source code to Android Application
• These .dex files are therefore much smaller in size than the
corresponding class files.
• The .dex file and the resources of an Android project, e.g., the
images and XML files, are packed into an .apk (Android
Package) file. The program aapt (Android Asset Packaging
Tool) performs this step.
• The resulting .apk file contains all necessary data to run the
Android application and can be deployed to an Android device
via the adb tool.
Mobile Programming 1 - Android Environment SDK
Conversion process from source code to Android Application
8
6.10.2016
Mobile Programming 1 - Android Environment SDK
Conversion process from source code to Android Application
Mobile Programming 1 - Android Environment SDK
Download Android Studio
Download Android
Studio from the Android
Studio website.
The download comes in
two flavors, SDK Tools
only and Android Studio
Packages.
You want to download
the Android Studio
Package for your
operation system.
9
6.10.2016
Mobile Programming 1 - Android Environment SDK
Installation of Android Studio
• Installation for Windows is simple, just lauch the .exe you
downloaded. On Max OSX drag and drop Android Studio into
the Applications folder.
• On Linux unpack the downloaded ZIP file into an appropriate
location for your applications. To To launch Android Studio,
navigate to the android-studio/bin/ directory in a terminal and
execute studio.sh.
Mobile Programming 1 - Android Environment SDK
Configration
The first time you start Android Studio you can select if you want
to import your setting from an existing installation.
10
6.10.2016
Mobile Programming 1 - Android Environment SDK
Configration
Afterwards click through the setup guide.
Mobile Programming 1 - Android Environment SDK
Configration
Once you reach the last page, press the Finish button.
11
6.10.2016
Mobile Programming 1 - Android Environment SDK
Android SDK Manager
• Using the Android SDK manager
– The Android SDK Manager allows you to install specific versions of the Android API.
– The Android SDK Manager allows you to install and delete Android packages.
• Open the Android SDK manager in Android Studio
– Select Tools → Android → SDK Manager or the SDK Manager icon in the toolbar of
Android Studio to open the Android SDK manager.
Mobile Programming 1 - Android Environment SDK
Install selected Android version or library
In the Android SDK
manager select the
version of Android you
would like to develop for
from the tree and press
the Install button.
The following screenshot
shows the selection for
the API 18 version of
Android.
The SDK Platforms tab is used to install API versions, which the SDK Tools is used to install the
development tools.
12
6.10.2016
Mobile Programming 1 - Android Environment SDK
Install support library
The support library allows you to
use functionality provided by
higher Android releases in lower
Android versions.
In the Android SDK Manager select
Extras and install the Android
Support Repository. The Android
Support Library is for the usage of
the Eclipse ADT tooling.
Android currently has several
versions of the library, the v4, v7
and v13 version which are valid as
of the respective API level of
Android.
For example, the support library v7 works as of Android devices with version API 7. Higher versions of
the support library require also the lower versions to work. For example, support library v7 requires the
v4 library.
Mobile Programming 1 - Android Environment SDK
Exploring the Core Android Application Framework
• Which file comprises the Android framework?
– android.jar
13
6.10.2016
Mobile Programming 1 - Android Environment SDK
Important Packages in the Android SDK
Top-Level Package Name
Description
android.*
Android application fundamentals
dalvik.*
Dalvik virtual machine support classes
java.*
Core classes and familiar generic utilities
javax.*
Encryption support
junit.*
Unit-testing support
org.apache.http.*
HTTP protocol support
org.json
JSON support
org.w3c.dom
W3C DOM XML and HTML support
org.xml.sax.*
SAX support for XML
org.xmlpull.*
High-performance XML pull parsing
Mobile Programming 1 - Android Environment SDK
Popular Third-Party Android APIs
• Available outside the core Android SDK
• Installed separately
• Some are from Google, and others are from device
manufacturers and other providers
14
6.10.2016
Mobile Programming 1 - Android Environment SDK
Popular Third-Party Android APIs (Cont’d)
•
•
•
•
•
•
Android Support Library
Google AdMob Ads SDK (com.google.ads.*)
Google Analytics App Tracking SDK (com.google.analytics.tracking.android.*)
Android Cloud Messaging for Android (GCM) (com.google.android.gcm)
Google Play Services (com.google.*)
Google Play APK Expansion Library
(com.google.android.vending.expansion.*)
Google Play Billing Library (com.android.vending.billing)
Google Play Licensing Library (com.google.android.vending.licensing.*)
Mobile Programming 1 - Android Environment SDK
Android emulator and Android Virtual Device
• Helpful tool used for designing and developing Android
applications
• Runs on your computer
• Behaves like a real device
• Load Android applications into the emulator to test and debug
them
• Generic device, not tied to any one specific configuration
• Valuable but should not be used as a replacement for testing
on actual target devices
15
6.10.2016
Mobile Programming 1 - Android Environment SDK
Android emulator and Android Virtual Device
• The Android tooling contains an Android device emulator. This
emulator can be used to run an Android Virtual Device (AVD), which
emulates a real Android phone.
• AVDs allow you to test your Android applications on different
Android versions and configurations without access to the real
hardware. Even if you have a real Android device available, you
should get familiar with the creation and usage of AVDs. Virtual
devices give you the possibility to test your application for selected
Android versions and a specific configurations.
• You can define multiple AVDs with different configurations and start
them in parallel. This allows you to test different device
configurations at once.
Mobile Programming 1 - Android Environment SDK
Google vs. Android AVD
• During the creation of an AVD you decide if you want to create an
Android device or a Google device.
• An AVD created for Android contains the programs from the Android
Open Source Project. An AVD created for the Google API's contains
additional Google specific code.
• AVDs created for the Google API allow you to test applications which
use Google Play services, e.g., the new Google maps API or the new
location services.
16
6.10.2016
Mobile Programming 1 - Android Environment SDK
Speed Optimization with the Intel System Image
• It is possible to run an AVD with an image based on the ARM
CPU architecture or based on the Intel CPI architecture.
• An Android virtual device which uses the Intel system image is
much faster in execution on Intel / AMD hardware compared to
the ARM based system image. This is because the emulator
does not need to translate the ARM CPU instructions to the
Intel / AMD CPU on your computer.
• The Intel image for an API can be installed via the Android SDK
Manager. In Android Studio this happens automatically if you
create an device. If is possible to configure this via the package
details.
Mobile Programming 1 - Android Environment SDK
Speed Optimization with the Intel System Image
17
6.10.2016
Mobile Programming 1 - Android Environment SDK
Creating an Android Virtual Device (AVD)
An AVD allows you to
simulated devices and
SDKs. To create a virtual
unit follow the next
steps:
1. Click on the AVD Manager
2. Click Create Virtual Device
button below. The Virtual
Device Configration dialog
appears.
Mobile Programming 1 - Android Environment SDK
Creating an Android Virtual Device (AVD)
An AVD allows you to
simulated devices and
SDKs. To create a virtual
unit follow the next
steps:
3. Select Category and name of
the AVD, such as “4,95 inch
Nexus" and then click Next
button.
4. Choose a system image (such
as “Marshmallow, API Level
23”).
18
6.10.2016
Mobile Programming 1 - Android Environment SDK
Creating an Android Virtual Device (AVD)
5. Choose how the graphics
should be rendered in the
emulator.
If
you
select Hardware the AVD uses
your computer’s graphics card
for faster rendering, and
otherwise select Software The
AVD emulate the graphics in
software, use this to work
around issues with your
computer’s graphic card.
6. And then click Finish
Mobile Programming 1 - Android Environment SDK
Creating an Android Virtual Device (AVD)
Some examples:
19
6.10.2016
Mobile Programming 1 - Android Environment SDK
Testing the Emulator
Select yout new entry and press the Play button.
Mobile Programming 1 - Android Environment SDK
Testing the Emulator
After a while your application should
start on the virtual device.
20
6.10.2016
Mobile Programming 1 - Android Environment SDK
You can use the fastest emulator: Genymotion
First, install Virtual Box for Genymotion
Mobile Programming 1 - Android Environment SDK
Install Genymotion
https://cloud.genymotion.com/page/launchpad/download/
21
6.10.2016
Mobile Programming 1 - Android Environment SDK
Genymotion Eclipse plugin
http://plugins.genymotion.com/eclipse
Mobile Programming 1 - Android Environment SDK
GenyMotion – Create a Virtual Device
22
6.10.2016
Mobile Programming 1 - Android Environment SDK
GenyMotion – Start VM
Mobile Programming 1 - Android Environment SDK
GenyMotion – Play with VM
23
6.10.2016
Mobile Programming 1 - Android Environment SDK
Create a new Android Project
•
•
Press the Start a new Android Studio project link to get started. Alternatively you can
select the File → New Project... entry from the menu.
Use the following data of input for your project. Project location and package name
are derived from your input. If you want another package name, press the small Edit
hyperlink.
Property
Value
Application name
Test App
Company Domain
ceng.anadolu.edu
Package Name
edu.anadolu.ceng.testapp
API (Minimum, Target,
Compile with)
Latest
Template
Blank Activity
Mobile Programming 1 - Android Environment SDK
Create a New Android Project
Creating an Android Project
To create a new project:
1. Start Eclipse
2. Select File > New > Project.
3. Select Android > Android Project,
and click Next.
4. Enter Application Name: HelloWorld.
5. Project Name: HelloWorldActivity.
6. Package Name:
bim493.helloworldactivity
7. Minimum Required SDK: API11 .
8. Target SDK: API 16.
9. Click Finish.
24
6.10.2016
Mobile Programming 1 - Android Environment SDK
Create an Android Project
Press the Next button and ensure
that you have enabled the Create
custom launcher icon and Create
activity checkboxes.
Mobile Programming 1 - Android Environment SDK
Create an Android Project
On the wizard page for the launcher
icon, create an application icon of
your choice. This screenshot shows
an example for a possible result.
25
6.10.2016
Mobile Programming 1 - Android Environment SDK
Create an Android Project
Press the Next button and select the
BlankActivity template. Press the
Next button to proceed.
Mobile Programming 1 - Android Environment SDK
Create an Android Project
Enter the following data in the dialog
for the template. The selection is
depicted in this screenshot.
26
6.10.2016
Mobile Programming 1 - Android Environment SDK
Create an Android Project
Enter the following data in the dialog
for the template. The selection is
depicted
in
this
screenshot.
Press the Finish button. The wizard
may prompt you to install the
support library. If so, select to install
it.
Mobile Programming 1 - Android Environment SDK
Create an Android Project
If you have not yet done so far,
create and start an Android virtual
device (AVD). The Android version
you select should fit to the minimum
API version of your Android
application.
After startup you see the welcome
screen of your AVD as depicted in
this screenshot.
27
6.10.2016
Mobile Programming 1 - Android Environment SDK
Create an Android Project
Once you AVD is ready, unlock your
emulator.
Mobile Programming 1 - Android Environment SDK
Start the Application
Select your Android project, right click on it,
and select Run-As → Android ApplicaƟon.
28
6.10.2016
Mobile Programming 1 - Android Environment SDK
Start the Application
This starts your application on the AVD. The
started application is a very simple application
which just shows the Hello, world! String.
Mobile Programming 1 - Android Environment SDK
Questions?
29