Android 5 Lollipop Development Tools - SDJUG · • The Bad News for Eclipse Users: You may be...

Preview:

Citation preview

Copyright (c) 2015 Servin Corp

Android 5 Lollipop Development Tools

Norman McEntire norman.mcentire@servin.com

1

Copyright (c) 2015 Servin Corp

Opening Remarks• Welcome!

• Thank you for attending!

• My promise to you

• Provide a solid introduction to Android 5 Lollipop Development Tools

• Android Studio

2

Copyright (c) 2015 Servin Corp

About Myself• Norman McEntire

• norman.mcentire@servin.com

• BS/MS Computer Engineering

• USC - University of South Carolina

• 30+ Years Computer Engineering Experience

• Hardware Engineering (chips, boards, systems)

• Software Engineering (drivers, systems software, mobile apps)

• Current Software Focus: Android/Java, iOS 8/ObjC/Swift, IoT, Linux

3

Copyright (c) 2015 Servin Corp

How To Take This Course• Option 1 - Corporate Training

• Contact me at norman.mcentire@servin.com to schedule this Android course at your corporation

• Option 2 - UCSD Extension Course

• http://extension.ucsd.edu/studyarea/index.cfm?vAction=singleCourse&vCourse=CSE-41145&vsacategoryid=166&vStudyAreaID=14

4

Copyright (c) 2015 Servin Corp

My Assumptions About You

• You are in one of two broadly defined groups

• Group 1. Experienced Android Software Developer

• You have been using Eclipse ADT

• Group 2. New to Android Software Development

• No experience with Eclipse ADT

• Common to both groups: Learn Android Studio!

5

Copyright (c) 2015 Servin Corp

Agenda

• Android 5 Development Tool Options

• Migrating from Eclipse ADT to Android Studio

• Using Android Studio

• Using Android Studio Tools

6

Copyright (c) 2015 Servin Corp

Android 5 Development Tool Options

7

Copyright (c) 2015 Servin Corp

Android 5 Development Tools Options

• You have three major options

• Option 1. Start Using Android Studio

• The official Android 5 IDE

• Option 2. Continue to use Eclipse ADT for now

• Migrate to Android Studio when you can

• Option 3. Use the command-line (no IDE)8

Copyright (c) 2015 Servin Corp

Why Android Studio

9

Copyright (c) 2015 Servin Corp

System Requirements• Linux (e.g. Ubuntu 12.04)

• GNOME or KDE, glibc 2.11 or higher

• JDK 7

• Mac OS X

• 10.8.5 or higher

• JDK 7

• NOTE: Run Android Studio with JDK 1.6 for optimized rendering, but use JDK 7 for building Android code

• Windows

• Windows 7, 8

• JDK 7

10

Copyright (c) 2015 Servin Corp

Android Studio Downloads

11

Copyright (c) 2015 Servin Corp

Eclipse IDE and Android Studio IDE

• The Good News About Android Studio

• It is highly integrated with the Android Java build environment

• Example 1: GUI layout much easier (see multiple layouts at same time)

• Example 2: Build Variants (build “free" and “paid” version of app at same time)

• The Bad News for Eclipse Users: You may be using Eclipse for other software development projects (Android, C/C++, PHP, etc) and now you have “yet another IDE” to learn

12

Copyright (c) 2015 Servin Corp

Exporting Your Eclipse Project for Import to

Android Studio

13

Copyright (c) 2015 Servin Corp

Steps To Migrate Your Eclipse Project To Android Studio

• Step 1 [In Eclipse]. Export your project

• Step 2 [In Studio]. Close all open projects

• Step 3 [In Studio]. Select Import Non-Android Studio Project

14

Copyright (c) 2015 Servin Corp

To Create an Archive File In Eclipse

15

Copyright (c) 2015 Servin Corp

Archive File In Eclipse

16

Copyright (c) 2015 Servin Corp

Migrating From Eclipse ADT to Android Studio

17

Copyright (c) 2015 Servin Corp

Migrating To Android Studio

18

Copyright (c) 2015 Servin Corp

Steps To Migrate Your Eclipse Project To Android Studio

• Step 1 [In Eclipse]. Export your project

• Step 2 [In Studio]. Close all open projects

• Step 3 [In Studio]. Select Import Non-Android Studio Project

19

Copyright (c) 2015 Servin Corp

Demo Migrating from Eclipse ADT

Project to Android Studio

20

Copyright (c) 2015 Servin Corp

Android Studio 1.1 Startup

21

Copyright (c) 2015 Servin Corp

Android Studio Startup

22

Copyright (c) 2015 Servin Corp

Import Non-Android Studio Project

23

Copyright (c) 2015 Servin Corp

Select Project To Import

24

Copyright (c) 2015 Servin Corp

Cannot Directly Import From Zip File

25

Copyright (c) 2015 Servin Corp

Unzip File, Then Import

26

Copyright (c) 2015 Servin Corp

Import Destination Directory

27

Copyright (c) 2015 Servin Corp

Import Project from Eclipse ADT

28

Copyright (c) 2015 Servin Corp

Import In Progress

29

Note: Notice use of Gradle to build project.

Copyright (c) 2015 Servin Corp

Results of Import

30

Copyright (c) 2015 Servin Corp

Build and Run In Android Studio

31

Copyright (c) 2015 Servin Corp

Device Chooser

32

Copyright (c) 2015 Servin Corp

Android DDMS

33

Copyright (c) 2015 Servin Corp

Screen Capture

34

Copyright (c) 2015 Servin Corp

Captured Screen

35

Copyright (c) 2015 Servin Corp

Summary

• Built and Ran Android 5 App using Eclipse

• Exported from Eclipse to Android Studio

• Built and Ran Android 5 App using Android Studio

36

Copyright (c) 2015 Servin Corp

Demo Import Eclipse ADT Project

that has .jar files in the project

37

Copyright (c) 2015 Servin Corp

Eclipse ADT Project with .jar file (HelloJar.jar)

38

Copyright (c) 2015 Servin Corp

Export Project from Eclipse (Select Archive File)

39

Copyright (c) 2015 Servin Corp

Archive File (HelloAndroid5WithJar)

40

Copyright (c) 2015 Servin Corp

Import Into Android Studio

41

Copyright (c) 2015 Servin Corp

Select Directory

42

Copyright (c) 2015 Servin Corp

Destination Directory

43

Copyright (c) 2015 Servin Corp

“ADT project importer can identify some .jar files”

44

Copyright (c) 2015 Servin Corp

Gradle Configuration

45

Copyright (c) 2015 Servin Corp

build.gradle(Module: app)

46

Copyright (c) 2015 Servin Corp

Moved Files During Import

47

Copyright (c) 2015 Servin Corp

Results Shown In Android Studio

48

Copyright (c) 2015 Servin Corp

Using Android Studio

49

Copyright (c) 2015 Servin Corp

Android Studio Startup

50

Copyright (c) 2015 Servin Corp

New Project

51

Copyright (c) 2015 Servin Corp

Select Form Factors

52

Copyright (c) 2015 Servin Corp

Add Activity

53

Copyright (c) 2015 Servin Corp

Options for New File (Use Defaults)

54

Copyright (c) 2015 Servin Corp

Getting Project Ready (Takes a few moments)

55

Copyright (c) 2015 Servin Corp

Project Ready for Editing

56

Copyright (c) 2015 Servin Corp

Editing XML Layout

57

Copyright (c) 2015 Servin Corp

Editing MainActivity.java

58

Copyright (c) 2015 Servin Corp

Tracking status of the build

59

Copyright (c) 2015 Servin Corp

Device Chooser

60

Copyright (c) 2015 Servin Corp

Screen Capture

61

Copyright (c) 2015 Servin Corp

Android Studio Skills

62

Copyright (c) 2015 Servin Corp

Android Studio Skill Code Completion

• Just start typing and Android Studio will show you choices

• Just press ENTER to complete a given choice

• Also press TAB go to next parameter

• Example

• Toast.makeText(this,”hi”,Toast.LENGTH_LONG).show()

63

Copyright (c) 2015 Servin Corp

Android Studio Skill SmartType Code Completion• When you get to certain locations in your code,

press Shift+Control+Spacebar to see choices based on context

• Example

• StringBuffer sb = new Shift+Control+SpaceBar

• Calendar now = new GregorianCalendar(Shift+Control+SpaceBar)

64

Copyright (c) 2015 Servin Corp

Android Studio Skill Navigate in Current File

• To navigate in the current file

• Option 1 [Menu]. Navigate, File Structure

• Then select member you want to navigate to

• Option 2 [Kbd]. Command+F12

• Then select member you want to navigate to

65

Copyright (c) 2015 Servin Corp

Android Studio Skill Quickly View Definition

• To view a quick definition of a class

• Step 1. Highlight a given class name

• Step 2 [Menu]. View, Quick Definition

66

Copyright (c) 2015 Servin Corp

Android Studio Skill Quickly View Documentation• To quickly view documentation of a class

• Step 1. Highlight a given class name

• Step 2.

• Option 1 [Menu]. View, Quick Documentation

• Option 2 [Kbd]. F1

67

Copyright (c) 2015 Servin Corp

Android Studio Skill Navigate to Declaration

• To navigate to the declaration of a class, variable, or method

• Step 1. Position cursor on name

• Step 2.

• Option 1 [Menu]. Navigate, Declaration

• Option 2 [Kbd]. Command+b

68

Copyright (c) 2015 Servin Corp

Android Studio Skill Refactor a Name

• To refactor a name of a class, method, or variable

• Step 1. Position cursor on name

• Step 2.

• Option 1 [Menu]. Refactor, Rename

• Option 2 [Kbd]. Shift+F6

69

Copyright (c) 2015 Servin Corp

Android Studio Skill Display Override Methods

• To display list of methods you can override in the base class

• Option 1 [Menu]. Code, Override Methods

• Option 2 [Kbd]. Control+o

• NOTE: You can also “just start typing the name”

70

Copyright (c) 2015 Servin Corp

Android Studio Skill Display Methods of Interface

• To display list of methods you can implement in an interface

• Option 1 [Menu]. Code, Implement Methods

• Option 2 [Kbd]. Control+i

71

Copyright (c) 2015 Servin Corp

Android Studio Skill Add New File To Project

• To add a new file to your project

• Step 1. Click on “folder” where you want to create the file (e.g. java, res, etc)

• Option 1 [Menu]. File, New

• Option 2 [MacKbd]. Command+n

• Option 2 [WinKbd]. Alt+Insert

72

Copyright (c) 2015 Servin Corp

Results of Adding Demo Class

73

Copyright (c) 2015 Servin Corp

Android Studio Skill Generate Code

• To generate code for a class (e.g. constructor, getter/setter, etc.)

• Step 1. Click on location where you want the code generated

• Option 1 [Menu]. Code, Generate

• Option 2 [MacKbd]. Command+n

• Option 2 [WinKbd]. Alt+Insert74

Copyright (c) 2015 Servin Corp

Results of Code Generation

75

Copyright (c) 2015 Servin Corp

Using Android Studio Templates

76

Copyright (c) 2015 Servin Corp

No Activity

77

Copyright (c) 2015 Servin Corp

No Activity

78

Copyright (c) 2015 Servin Corp

New Project

79

Copyright (c) 2015 Servin Corp

Select Form Factor

80

Copyright (c) 2015 Servin Corp

(Optional) Add Activity

81

Copyright (c) 2015 Servin Corp

Result

82

Copyright (c) 2015 Servin Corp

AndroidManifest.xml

83

Copyright (c) 2015 Servin Corp

Blank Activity

84

Copyright (c) 2015 Servin Corp

Blank Activity

85

Copyright (c) 2015 Servin Corp

Blank Activity

86

Copyright (c) 2015 Servin Corp

AndroidManifest.xml

87

Copyright (c) 2015 Servin Corp88

Copyright (c) 2015 Servin Corp89

Copyright (c) 2015 Servin Corp

Blank Activitywith Fragment

90

Copyright (c) 2015 Servin Corp

Blank Activity

91

Copyright (c) 2015 Servin Corp

AndroidManifest.xml

92

Copyright (c) 2015 Servin Corp

MainActivity.java - 1

93

Copyright (c) 2015 Servin Corp

MainActivity.java - 2

94

Copyright (c) 2015 Servin Corp

activity_main.xml

95

Copyright (c) 2015 Servin Corp

fragment_activity_main.xml

96

Copyright (c) 2015 Servin Corp

Fullscreen Activity

97

Copyright (c) 2015 Servin Corp

Fullscreen Activity

98

Copyright (c) 2015 Servin Corp

Result

99

Copyright (c) 2015 Servin Corp

Google Maps Activity

100

Copyright (c) 2015 Servin Corp

Google Maps Activity

101

Copyright (c) 2015 Servin Corp

Google Play Services Activity

102

Copyright (c) 2015 Servin Corp

Google Play Services Activity

103

Copyright (c) 2015 Servin Corp

Login Activity

104

Copyright (c) 2015 Servin Corp

Login Activity

105

Copyright (c) 2015 Servin Corp

Master/Detail Flow

106

Copyright (c) 2015 Servin Corp

Master/Detail Flow

107

Copyright (c) 2015 Servin Corp

Navigation Draw Activity

108

Copyright (c) 2015 Servin Corp

Navigation Draw Activity

109

Copyright (c) 2015 Servin Corp

Settings Activity

110

Copyright (c) 2015 Servin Corp

Settings Activity

111

Copyright (c) 2015 Servin Corp

Tabbed Activity

112

Copyright (c) 2015 Servin Corp

Tabbed Activity

113

Copyright (c) 2015 Servin Corp

Gradle Build Scripts

114

Copyright (c) 2015 Servin Corp

Gradle Build Scripts

115

Copyright (c) 2015 Servin Corp

build.grade (Project)

116

Copyright (c) 2015 Servin Corp

build.grade (app)

117

Copyright (c) 2015 Servin Corp

gradle-wrapper.properties

118

Copyright (c) 2015 Servin Corp

gradle-wrapper.properties

119

Copyright (c) 2015 Servin Corp

Android Studio DDMS Options

120

Copyright (c) 2015 Servin Corp

Screen Capture

121

Copyright (c) 2015 Servin Corp

Screen Recorder

122

Copyright (c) 2015 Servin Corp

System Information

123

Copyright (c) 2015 Servin Corp

System Information Activity Manager State

124

Copyright (c) 2015 Servin Corp

System Information Package Information

125

Copyright (c) 2015 Servin Corp

System Information Memory Usage

126

Copyright (c) 2015 Servin Corp

System Information Memory Usage Over Time

127

Copyright (c) 2015 Servin Corp

System Information Graphics State

128

Copyright (c) 2015 Servin Corp

Memory Monitor

129

Copyright (c) 2015 Servin Corp

Android Studio Dynamic Layout

130

Copyright (c) 2015 Servin Corp

Dynamic Layout• Three Options

• Design View

• Drag from Palette to Preview or Component Tree

• Text View

• Preview

131

Copyright (c) 2015 Servin Corp

Dynamic Design Preview Choices

• You can preview all of the following

• Different Device Images

• Different Display Densities

• Different UI Modes

• Different Android Versions

132

Copyright (c) 2015 Servin Corp

Motivation For Dynamic Layout

133

Copyright (c) 2015 Servin Corp

Design View

134

Copyright (c) 2015 Servin Corp

Text View

135

Copyright (c) 2015 Servin Corp

Dynamic Preview based on API Version

136

Copyright (c) 2015 Servin Corp

Dynamic Preview based on Screen Size

137

Copyright (c) 2015 Servin Corp

Keeping Android Studio Up-To-Date

138

Copyright (c) 2015 Servin Corp

Four Update Channels

139

Copyright (c) 2015 Servin Corp

Easy Access to Android Code Samples via

GitHub

140

Copyright (c) 2015 Servin Corp

Open Project From GitHub Code Samples

141

Copyright (c) 2015 Servin Corp

Code Samples from GitHub Description

142

Copyright (c) 2015 Servin Corp

Code Samples from GitHub Preview

143

Copyright (c) 2015 Servin Corp

Code Samples from GitHub Import Sample

144

Copyright (c) 2015 Servin Corp

Possible Error When Importing Sample Projects

145

Copyright (c) 2015 Servin Corp

Installing Build Tools License Agreement

146

Copyright (c) 2015 Servin Corp

Installing Build Tools Installing Requested Components

147

Copyright (c) 2015 Servin Corp

Android Device Monitor

148

Copyright (c) 2015 Servin Corp

Starting Android Device Monitor From Android Studio

149

Copyright (c) 2015 Servin Corp

Android Device Monitor

150

Copyright (c) 2015 Servin Corp

Android SDK Manager

151

Copyright (c) 2015 Servin Corp

Starting Android SDK Manager From Android Studio

152

Copyright (c) 2015 Servin Corp

Android SDK Manager

153

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager

154

Copyright (c) 2015 Servin Corp

Starting Android Virtual Device (AVD) Manager From Android Studio

155

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - Create Device

156

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - Choose Device

157

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - System Image

158

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - Verify Configuration

159

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - Your Virtual Device

160

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - Startup

161

Copyright (c) 2015 Servin Corp

Android Virtual Device (AVD) Manager - Startup

162

Copyright (c) 2015 Servin Corp

Code Running on Emulator

163

Copyright (c) 2015 Servin Corp

Android Build System

• Previous Android Build System

• Eclipse ADT with ANT

• New Build System

• Android Studio with Gradle

164

Copyright (c) 2015 Servin Corp

Two Options for the Build System

• Option 1.

• Use features of Android Studio without any focus on the underlying build system

• Create single APK

• Option 2.

• Customize the build system

• Create multiple APKs using same project

• Reuse code and resources across project sets

165

Copyright (c) 2015 Servin Corp

Gradle Build Script• The build files are called build.gradle

• They are plain text files

• They use the Groovy syntax to configure the build

• With elements provided by the Android plugin for Gradle

• In most cases, you only need to edit build files at the module level (see next slide)

166

Copyright (c) 2015 Servin Corp

Normally only edit build.gradle at Module Level

167

Copyright (c) 2015 Servin Corp

Key Point: The Android Build System enables you to customize build WITHOUT modification to app source files!

168

Copyright (c) 2015 Servin Corp

The "apply" line includes Android-Specific Elements

169

Copyright (c) 2015 Servin Corp

The android Element configures all Android-specific build options

170

Copyright (c) 2015 Servin Corp

compileSdkVersion

171

Copyright (c) 2015 Servin Corp

buildToolsVersion (always higher than compileSdkVersion)

172

Copyright (c) 2015 Servin Corp

defaultConfig (override settings in AndroidManifest.xml)

(apply to all build variants)

173

Copyright (c) 2015 Servin Corp

buildTypes (how to build and package app)

174

Note: By default, build system defines two types: debug and release

Copyright (c) 2015 Servin Corp

dependencies

175

Copyright (c) 2015 Servin Corp

Important! Making changes to build system

requires a sync! (see next slide)

176

Copyright (c) 2015 Servin Corp

Sync Now

177

Copyright (c) 2015 Servin Corp

applicationId

• Use applicationId to uniquely identify application packages for publishing

178

Copyright (c) 2015 Servin Corp

applicationId

179

Key Point: applicationIdspecified inbuild.grade,and NOT in

AndroidManifest.xml

Copyright (c) 2015 Servin Corp

Build Variants Demo

180

Copyright (c) 2015 Servin Corp

Build Variants Demo• This demo will show how to create to build

variants from a single project

• A Free (Limited Featured) Version

• A Paid (Fully Featured) Version

• Both versions will have the same MainActivity

• Each version will have a different SecondActivity

181

Copyright (c) 2015 Servin Corp

Step 1. Define Two Product Flavors

182

Copyright (c) 2015 Servin Corp

Step 2. Make sure grade performs a sync

183

Copyright (c) 2015 Servin Corp

Step 3. Select the “freeDebug”

184

Copyright (c) 2015 Servin Corp

Step 4. Add Directories for “free” Product Flavor

185

Copyright (c) 2015 Servin Corp

Step 5a. Right-Click on java, select New, Activity, Blank Activity

186

Copyright (c) 2015 Servin Corp

Step 5b. Add SecondActivity for “free” Product Flavor

187

Copyright (c) 2015 Servin Corp

Directory Structure for “free” Product Flavor

188

Copyright (c) 2015 Servin Corp

Selecting Different Product Flavors - freeDebug and paidDebug

189

Copyright (c) 2015 Servin Corp

Running In Demo Mode

190

Copyright (c) 2015 Servin Corp

Adding a .jar file to your Android Studio Project

191

Copyright (c) 2015 Servin Corp

Step 1. Select Project View

192

Copyright (c) 2015 Servin Corp

Step 2. Drag/Drop .jar file into libs directory

193

Copyright (c) 2015 Servin Corp

Step 3. Right-click on .jar file, select Add as Library

194

Copyright (c) 2015 Servin Corp

Step 4. Confirm Results

195

Copyright (c) 2015 Servin Corp

Step 5. Confirm gradle

196

Copyright (c) 2015 Servin Corp

Step 5. Confirm In MainActivity

197

Copyright (c) 2015 Servin Corp

Thank You!

198