Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Android Studio Walkthrough

Shaka Huang
January 14, 2015

Android Studio Walkthrough

GDG Taipei regular meeting, Android Studio & Gradle 101

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

Shaka Huang

January 14, 2015
Tweet

More Decks by Shaka Huang

Other Decks in Programming

Transcript

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

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

    Android tools • ddms / wizard / resource editor • Gradle plugin • 101 • migration from eclipse project 3
  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
  4. 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
  5. 7

  6. 8

  7. 9

  8. 10

  9. 11

  10. 12

  11. 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
  12. 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
  13. 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
  14. 18

  15. 20

  16. 22

  17. 23

  18. 24

  19. 25

  20. 26

  21. 27

  22. 28

  23. 29

  24. 30

  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. Other features .. • Resources preview in source code editor

    • color / icon / string .. • Powerful autocomplete • use with Tab key • Build variants 37
  32. Inside the project • project • modules • build files

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

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

    • build files • plugin • dependencies • tasks 41
  35. 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
  36. 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
  37. 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
  38. Project build file <project_rootdir>/build.gradle 47 buildscript { repositories { jcenter()

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

    ':library2' project(':library1').projectDir = new File('/other/places/on/the/computer')
  40. 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') } } }
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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/
  46. 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 } }
  47. 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
  48. 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 } } }
  49. 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 } } }
  50. 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
  51. 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 } } }
  52. 64

  53. 65

  54. 66

  55. 67

  56. 68

  57. 69

  58. 70

  59. 71

  60. 72

  61. 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'] } } }
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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