ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
Android development with Gradle
description
Transcript of Android development with Gradle
![Page 1: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/1.jpg)
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
Gradle for Android
Kenneth Kousen@kenkousen
![Page 3: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/3.jpg)
Upcoming Book
Gradle for AndroidO'Reilly
Part of the Gradle series
![Page 5: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/5.jpg)
SDK Bundle
https://developer.android.com/sdk/index.html
Eclipse + ADT toolsAndroid SDK toolsAndroid Platform toolsLatest Android SDK + emulator
![Page 6: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/6.jpg)
Eclipse ADT
Does not support Gradle directly
Generate gradle build filemaps default structure to standard layoutno longer needed → can import project
![Page 7: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/7.jpg)
Gradle
Why Gradle?
![Page 8: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/8.jpg)
Gradle
Because reasons
![Page 9: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/9.jpg)
Gradle
Because reasons
(This is a Groovy conference.You already know why)
![Page 10: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/10.jpg)
Android Studiohttps://developer.android.com/sdk/installing/studio.html
Early Access Preview
![Page 11: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/11.jpg)
Android Studiohttps://developer.android.com/sdk/installing/studio.html
Early Access PreviewBased on IntelliJ IDEA
![Page 12: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/12.jpg)
Android Studiohttps://developer.android.com/sdk/installing/studio.html
Early Access PreviewBased on IntelliJ IDEAUses Gradle for builds
![Page 13: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/13.jpg)
Android Studiohttps://developer.android.com/sdk/installing/studio.html
Early Access PreviewBased on IntelliJ IDEAUses Gradle for builds
Beta → some bugs and unsupported features, butpretty solid so far
![Page 14: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/14.jpg)
Android Studio
Actually, latest version* is very useful
*As of SpringOne2GX 2014, version 0.8.9
![Page 15: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/15.jpg)
Creating an application
Must select unique package namecom.example.myapp
(com.example reserved for samples)
Used in Google Play store
![Page 16: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/16.jpg)
Creating an application
Choose min SDK levelChoose target SDK level
![Page 17: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/17.jpg)
Manifest
AndroidManifest.xml<uses-sdk … /> // old style
<application>
<activity>... </activity>
…</application>
![Page 18: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/18.jpg)
Change for Gradle
Now minSdkVersion, targetSdkVersionset in build.gradle
Overrides AndroidManifest.xml, so best to leave it out
![Page 19: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/19.jpg)
Manifest
All activities must be declaredList permissionsIntent filtersServicesContent providers…
![Page 20: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/20.jpg)
Activities
Represents a screenExtend android.app.Activity
Full of callback methods
![Page 21: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/21.jpg)
Activities
Each activity has an XML layoutactivity_main.xml
activity_welcome.xml
XML tags with many attributes
![Page 22: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/22.jpg)
Activities
Callback methods:onCreate, onDestroy
onStart, onStop
onPause, onResume
… many others …
![Page 23: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/23.jpg)
res
Resources folder contains subfoldersdrawablelayoutmenuvalues...
![Page 24: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/24.jpg)
Providing resourceshttps://developer.android.com/guide/topics/resources/providing-resources.html
Specially named subdirectoriesvalues
Configuration qualifiersvalues-v11values-sw720dp-land
![Page 25: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/25.jpg)
values
keys and values → layer of indirectionstrings.xml:
<string name="hello_world">Hello world!</string>
![Page 26: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/26.jpg)
Accessing resources
XML → compiled into R.java: full of public inner classes (!)
(Button) findViewById(R.id.hello_button)
![Page 27: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/27.jpg)
Basics
Android plugin for GradleAdded via buildScript
Lots of customization
![Page 28: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/28.jpg)
Basicsbuildscript {
repositories { mavenCentral() } // or jcenter()
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
}
}
apply plugin: 'com.android.application'
![Page 29: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/29.jpg)
Properties in build.gradle
android {
versionCode …versionName …
}
or even in gradle.properties
![Page 30: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/30.jpg)
Multiproject Builds
By default, apps in AS are multiproject buildsbuild.gradle
settings.gradle
Can add additional libraries, other modules, and more
![Page 31: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/31.jpg)
Build Types
Two default build types:debug
release
![Page 32: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/32.jpg)
Configuring Build Types
Use buildTypes section of build.gradlebuildTypes {
release { … }
debug { … }
}
Can also add custom build types that way
![Page 33: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/33.jpg)
Configuring Build Types
Each build type defines a source setsrc/main/ …src/debug/ …src/release/ …
src/androidTest/… (discussed below)
![Page 34: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/34.jpg)
Configuring Build Types
Resources in build type source setsreplace their counterparts in main
Java classes conflict, howeverDefine class in each, or just in main
![Page 35: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/35.jpg)
Generating a Release
Can't assemble a release untilyou can sign it
![Page 36: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/36.jpg)
Signing Your App
Use Java's keytool to generate cert
![Page 37: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/37.jpg)
![Page 38: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/38.jpg)
Signing Your App
Add signing block to build.gradlesigningConfigs {
release {
storeFile file('ICNDB.keystore')
keyAlias 'ICNDB'
storePassword 'gradle_rules'
keyPassword 'carlos_ray_aka_chuck'
}
}
![Page 39: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/39.jpg)
Signing Your App
Passwords don't have to be in build file- Can use system properties- Can prompt user- Can use external files
See docs for suggestions
![Page 40: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/40.jpg)
Signing Your App
Add signingConfig to build type config buildTypes {
release {
// …
signingConfig signingConfigs.release
}
}
![Page 41: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/41.jpg)
Signing Your App
The signingReport task shows details
![Page 42: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/42.jpg)
Signing Your App
Invoke assembleRelease taskResulting apk in build/outputs/apk folder
![Page 43: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/43.jpg)
Testing
Use androidTest source setsrc/androidTest/java
![Page 44: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/44.jpg)
Testing
Use androidTest source setsrc/androidTest/java
compileAndroidTest dependencies
![Page 45: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/45.jpg)
Testing
Use androidTest source setsrc/androidTest/java
androidTestCompile dependencies
Run connectedCheck task
![Page 46: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/46.jpg)
Flavors and Variants
buildTypesdebug, release
flavors → different versions of same appfree, paid
![Page 47: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/47.jpg)
Flavors and Variants
Each flavor generates an apk
Variants combine buildTypes and flavorsfree+debug, free+release,paid+debug, paid+release
![Page 48: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/48.jpg)
Multiple Flavors
See flavors sampleSee basicMultiFlavors sample
flavorDimensions …productFlavors { … }
![Page 49: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/49.jpg)
REST
Instead of JSON and Apache HTTP:Spring for Android
http://projects.spring.io/spring-android/
RestTemplate classMap classes to JSON structure
![Page 50: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/50.jpg)
References
Android new build systemhttp://tools.android.com/tech-docs/new-build-system http://tools.android.com/tech-docs/new-build-system/user-guide
Developer's Guide section on Gradlehttps://developer.android.com/sdk/installing/studio-build.html
Android Developers on G+https://plus.google.com/+AndroidDevelopers/posts
![Page 51: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/51.jpg)
References
Xavier Ducrohet on G+https://plus.google.com/+XavierDucrohet/posts
adt-dev on Google Groupshttps://groups.google.com/forum/#!forum/adt-dev
![Page 52: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/52.jpg)
The Ultimate Android OS
Spock Testing Shouldn't
Data run on Android?
![Page 53: Android development with Gradle](https://reader034.fdocuments.us/reader034/viewer/2022052218/557d5f3ad8b42aba3d8b4f18/html5/thumbnails/53.jpg)
The Ultimate Android OS