Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Studio Walkthrough

4cb35be1ef26ed4fb0dd2914ee8a4873?s=47 Shaka Huang
January 14, 2015

Android Studio Walkthrough

GDG Taipei regular meeting, Android Studio & Gradle 101

Reference: https://github.com/shakalaca/learning_gradle_android

4cb35be1ef26ed4fb0dd2914ee8a4873?s=128

Shaka Huang

January 14, 2015
Tweet

Transcript

  1. 安桌椅⼯工作室攻略 shakalaca @ GDG

  2. Log.i(“Shaka”, “01134”) • Co-organizer @ GDG Taipei • Consultant @

    SleepNOVA, Inc. • CTO @ SmarTapper Co., Ltd. 2 http://about.me/shakalaca
  3. Outline • Introduction • interface / keymap / plugin •

    Android tools • ddms / wizard / resource editor • Gradle plugin • 101 • migration from eclipse project 3
  4. Introduction

  5. 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
  6. 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
  7. 7

  8. 8

  9. 9

  10. 10

  11. 11

  12. 12

  13. https://developer.android.com/sdk/installing/studio-tips.html Programming key commands 13

  14. 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. 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
  16. https://developer.android.com/sdk/installing/studio-tips.html Editor key commands 16

  17. 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
  18. 18

  19. Help -> Productivity Guide 19

  20. 20

  21. Android Tools

  22. 22

  23. 23

  24. 24

  25. 25

  26. 26

  27. 27

  28. 28

  29. 29

  30. 30

  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. Other features .. • Resources preview in source code editor

    • color / icon / string .. • Powerful autocomplete • use with Tab key • Build variants 37
  38. Gradle plugin

  39. Inside the project • project • modules • build files

    • build files • plugin • dependencies • tasks 39
  40. Inside the project • project • modules • build files

    • build files • plugin • dependencies • tasks 40
  41. Inside the project • project • modules • build files

    • build files • plugin • dependencies • tasks 41
  42. Modules • android application module • android library module •

    app engine module • java module 42
  43. Build configurations • variants • manifest • signing • proguard

    • testing 43
  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 … 44
  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 … 45
  46. 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
  47. Project build file <project_rootdir>/build.gradle 47 buildscript { repositories { jcenter()

    } dependencies { classpath 'com.android.tools.build:gradle:1.0.0' } }
  48. Project build file <project_rootdir>/settings.gradle 48 include ':app' include ':library1' include

    ':library2' project(':library1').projectDir = new File('/other/places/on/the/computer')
  49. 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') } } }
  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' • 50
  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' • 51
  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' • 52
  53. 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
  54. 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/
  55. 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 } }
  56. 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
  57. 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 } } }
  58. 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 } } }
  59. 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
  60. Building variant • gradle assemble fullRelease • gradle assemble demoDebug

    60
  61. Build in Android Studio 61

  62. 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 } } }
  63. Migration from Eclipse

  64. 64

  65. 65

  66. 66

  67. 67

  68. 68

  69. 69

  70. 70

  71. 71

  72. 72

  73. Tips for migration • organise your project layout 73

  74. Tips for migration • organise your project layout • organise

    your module layout 74
  75. 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'] } } }
  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 76
  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 77
  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 78
  79. 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
  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 80
  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 81
  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 82
  83. 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
  84. 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
  85. Q & A

  86. 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
  87. 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