Share
Embed
Android Studio Walkthrough
GDG Taipei regular meeting, Android Studio & Gradle 101
Reference: https://github.com/shakalaca/learning_gradle_android
Shaka Huang
January 14, 2015
Transcript
87 slides
-
Log.i(“Shaka”, “01134”) • Co-organizer @ GDG Taipei • Consultant @
SleepNOVA, Inc. • CTO @ SmarTapper Co., Ltd. 2 http://about.me/shakalaca -
Outline • Introduction • interface / keymap / plugin •
Android tools • ddms / wizard / resource editor • Gradle plugin • 101 • migration from eclipse project 3 -
History • 2012/11/13 ADT Bundle • 2013/05/23 Preview released (0.1.1)
• 2014/06/27 Beta released (0.8.0) • 2014/12/08 1.0 released 5 -
The official Android IDE • Based on IntelliJ IDEA •
Flexible Gradle-based build system • Build variants and multiple apk file generation • Code templates to help you build common app features • Rich layout editor with support for drag and drop theme editing • Lint tools to catch performance, usability, version compatibility, and other problems • ProGuard and app-signing capabilities • Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine 6 http://developer.android.com/tools/studio/index.html -
14 • Column selection • CMD + Shift + 8
• Duplicate line • CMD + D (Mac) • Ctrl + D (Windows/Linux) • Expand shrink selection • Alt + UP or DOWN (Mac) • Ctrl + W or Shift + W (Windows/Linux) Programming key commands -
15 • Override methods • Ctrl + O • Implement
methods • Ctrl + I • Rename • Shift + F6 • Extract method • CMD + Alt + M (Mac) , Ctrl + Alt + M (Windows/Linux) • Extract parameter • CMD + Alt + P (Mac), Ctrl + Alt + P (Windows/Linux) Programming key commands -
17 • Recent files • CMD + E (Mac) •
Ctrl + E (Windows/Linux) • The switcher • Ctrl + Tab • Close other tabs • Hold Alt when hit ‘x’ on tab Editor key commands -
Do not hard coding • in xml file • xmlns:tools=“http://schemas.android.com/tools"
• Add theme/layout/default value in layout editor • tools:context • tools:layout • tools:listitem / listheader / listfooter • android:xxx attributes 31 http://tools.android.com/tech-docs/tools-attributes -
Do not hard coding • in xml file • xmlns:tools=“http://schemas.android.com/tools"
• Add theme/layout/default value in layout editor • tools:context • tools:layout • tools:listitem / listheader / listfooter • android:xxx attributes 32 http://tools.android.com/tech-docs/tools-attributes -
Do not hard coding • in xml file • xmlns:tools=“http://schemas.android.com/tools"
• Add theme/layout/default value in layout editor • tools:context • tools:layout • tools:listitem / listheader / listfooter • android:xxx attributes 33 http://tools.android.com/tech-docs/tools-attributes -
Do not hard coding • in xml file • xmlns:tools=“http://schemas.android.com/tools"
• Add theme/layout/default value in layout editor • tools:context • tools:layout • tools:listitem / listheader / listfooter • android:xxx attributes 34 http://tools.android.com/tech-docs/tools-attributes -
Do not hard coding • in xml file • xmlns:tools=“http://schemas.android.com/tools"
• Add theme/layout/default value in layout editor • tools:context • tools:layout • tools:listitem / listheader / listfooter • android:xxx attributes 35 http://tools.android.com/tech-docs/tools-attributes -
Do not hard coding • in xml file • xmlns:tools=“http://schemas.android.com/tools"
• Add theme/layout/default value in layout editor • tools:context • tools:layout • tools:listitem / listheader / listfooter • android:xxx attributes 36 http://tools.android.com/tech-docs/tools-attributes -
Other features .. • Resources preview in source code editor
• color / icon / string .. • Powerful autocomplete • use with Tab key • Build variants 37 -
Inside the project • project • modules • build files
• build files • plugin • dependencies • tasks 39 -
Inside the project • project • modules • build files
• build files • plugin • dependencies • tasks 40 -
Inside the project • project • modules • build files
• build files • plugin • dependencies • tasks 41 -
Modules • android application module • android library module •
app engine module • java module 42 -
Basic project layout <project_rootdir>/ • build.gradle • settings.gradle • app/
• build.grade • src/main/java … • library1/ • build.gradle • src/main/java … • library2/ • build.gradle • src/main/java … 44 -
Basic project layout <project_rootdir>/ • build.gradle • settings.gradle • app/
• build.grade • src/main/java … • library1/ • build.gradle • src/main/java … • library2/ • build.gradle • src/main/java … 45 -
Basic project layout <project_rootdir>/ • build.gradle • settings.gradle • app/
• build.grade • src/main/java … • library1/ • build.gradle • src/main/java … • library2/ • build.gradle • src/main/java … 46 -
Project build file <project_rootdir>/build.gradle 47 buildscript { repositories { jcenter()
} dependencies { classpath 'com.android.tools.build:gradle:1.0.0' } } -
Project build file <project_rootdir>/settings.gradle 48 include ':app' include ':library1' include
':library2' project(':library1').projectDir = new File('/other/places/on/the/computer') -
Application module <project_rootdir>/app/build.gradle 49 apply plugin: 'com.android.application' dependencies { compile
'com.android.support:support-v4:21.0.3' compile project(‘:library1') compile project(':library2') compile fileTree(dir: 'libs', include: ['*.jar']) } android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { debug { debuggable true versionNameSuffix "-debug" } release { shrinkResources true minifyEnabled true proguardFile getDefaultProguardFile('proguard-android.txt') } } } -
Dependencies • module dependencies • compile project(‘:module’) • local dependencies
• compile files(‘libs/core.lib’) • compile fileTree(dir: 'libs', include: [‘*.jar’]) • remote dependencies • compile 'com.android.support:support-v4:21.0.3' • 50 -
Dependencies • module dependencies • compile project(‘:module’) • local dependencies
• compile files(‘libs/core.lib’) • compile fileTree(dir: 'libs', include: [‘*.jar’]) • remote dependencies • compile 'com.android.support:support-v4:21.0.3' • 51 -
Dependencies • module dependencies • compile project(‘:module’) • local dependencies
• compile files(‘libs/core.lib’) • compile fileTree(dir: 'libs', include: [‘*.jar’]) • remote dependencies • compile 'com.android.support:support-v4:21.0.3' • 52 -
Dependencies • module dependencies • compile project(‘:module’) • local dependencies
• compile files(‘libs/core.lib’) • compile fileTree(dir: 'libs', include: [‘*.jar’]) • remote dependencies • compile 'com.android.support:support-v4:21.0.3' • 53 -
Repo by google • com.android.support:support-v4:21.0.3 • com.android.support:support-v13:21.0.3 • com.android.support:appcompat-v7:21.0.3 •
com.android.support:cardview-v7:21.0.3 • com.android.support:gridlayout-v7:21.0.3 • com.android.support:leanback-v17:21.0.3 • com.android.support:mediarouter-v7:21.0.3 • com.android.support:palette-v7:21.0.3 • com.android.support:recyclerview-v7:21.0.3 • com.android.support:support-annotations:21.0.3 • com.google.android.gms:play-services:6.5.87 54 http://gradleplease.appspot.com/ -
Library module <project_rootdir>/library1/build.gradle 55 apply plugin: 'com.android.library' dependencies { compile
'com.google.android.gms:play-services:6.5.87' } android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { minSdkVersion 14 targetSdkVersion 21 } } -
Tasks • gradle tasks • display all tasks runnable from
root project • gradle assemble • build the project • gradle check • run checks and tests • gradle build • runs both assemble and check • gradle clean • performs the clean 56 -
Variant • productFlavors + buildTypes 57 android { productFlavors {
full { applicationId "com.corner23.android.full" } demo { applicationId "com.corner23.android.demo" } } buildTypes { debug { versionNameSuffix "-debug" } release { minifyEnabled true shrinkResources true } } } -
Variant • productFlavors + buildTypes • fullDebug • fullRelease •
demoDebug • demoRelease 58 android { productFlavors { full { applicationId "com.corner23.android.full" } demo { applicationId "com.corner23.android.demo" } } buildTypes { debug { versionNameSuffix "-debug" } release { minifyEnabled true shrinkResources true } } } -
Variant • additional source directory for each flavor • app/src/demo/java
• app/src/demo/res • .. or build type • app/src/debug/java • app/src/debug/res 59 -
Sign configuration 62 android { signingConfigs { release { storeFile
file("other.keystore") storePassword "android" keyAlias "androiddebugkey" keyPassword "android" } myConfig { storeFile file("other2.keystore") storePassword "android2" keyAlias "androiddebugkey2" keyPassword "android2" } } buildTypes { release { signingConfig signingConfigs.release } foo { signingConfig signingConfigs.myConfig } } } -
Tips for migration • organise your project layout • organise
your module layout 74 -
Tips for migration • organise your project layout • organise
your module layout 75 android { sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } } -
Tips for migration • replace ‘*@@aar’ with correct library module
• use local module • create build.gradle under library module • add library project in settings.gradle • modify build.gradle under app (library) module • use remote module • http://gradleplease.appspot.com/ • check remote dependencies 76 -
Tips for migration • replace ‘*@@aar’ with correct library module
• use local module • create build.gradle under library module • add library project in settings.gradle • modify build.gradle under app (library) module • use remote module • http://gradleplease.appspot.com/ • check remote dependencies 77 -
Tips for migration • replace ‘*@@aar’ with correct library module
• use local module • create build.gradle under library module • add library project in settings.gradle • modify build.gradle under app (library) module • use remote module • http://gradleplease.appspot.com/ • check remote dependencies 78 -
Tips for migration • replace ‘*@@aar’ with correct library module
• use local module • create build.gradle under library module • add library project in settings.gradle • modify build.gradle under app (library) module • use remote module • http://gradleplease.appspot.com/ • check remote dependencies 79 -
Tips for migration • import project to a new directory,
see if everything is all right • modify module settings, see what changed inside the build.gradle • use the imported project as reference, create the build files from scratch for your original project 80 -
Tips for migration • import project to a new directory,
see if everything is all right • modify module settings, see what changed inside the build.gradle • use the imported project as reference, create the build files from scratch for your original project 81 -
Tips for migration • import project to a new directory,
see if everything is all right • modify module settings, see what changed inside the build.gradle • use the imported project as reference, create the build files from scratch for your original project 82 -
Tips for migration • import project to a new directory,
see if everything is all right • modify module settings, see what changed inside the build.gradle • use the imported project as reference, create the build files from scratch for your original project 83 -
Shared library • define path_to_share_library in ~/.gradle/ gradle.properties or <project_rootdir>/
gradle.properties • project(‘:xxx’).projectDir = new File(path_to_share_library) • dependencies in shared library should be defined in project’s settings.gradle also. 84 -
References • Documentation • http://tools.android.com/tech-docs/new-build-system (Guide / Samples) • http://www.gradle.org/documentation
• Forums • https://plus.google.com/u/0/communities/114791428968349268860 • https://groups.google.com/forum/#!forum/adt-dev • http://stackoverflow.com/questions/tagged/gradle • http://stackoverflow.com/questions/tagged/android-studio 86 -
Or follow me @ • G+ • http://google.com/profiles/shakalaca • Blog
• http://23pin.logdown.com • Github • https://github.com/shakalaca/learning_gradle_android • Taipei GDG (G+ Community) • https://plus.google.com/communities/100566773212437391191 87