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

Android Development with Gradle

Android Development with Gradle

@ 2014 Google I/O Extended Taipei

Shaka Huang

June 25, 2014
Tweet

More Decks by Shaka Huang

Other Decks in Programming

Transcript

  1. Hello ! I’m Shaka • Co-Organizer @ GDG Taipei •

    Consultant @ SleepNOVA, Inc. • CTO @ SmarTapper Co., Ltd. http:/ /about.me/shakalaca
  2. What is gradle ? Gradle is build automation evolved. Gradle

    can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else. ! Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. Powered by a Groovy DSL and packed with innovation, Gradle provides a declarative way to describe all kinds of builds through sensible defaults. Gradle is quickly becoming the build system of choice for many open source projects, leading edge enterprises and legacy automation challenges. - http:/ /www.gradle.org/
  3. .. short version An open source build system that combines

    the power of Ant & Maven but easy to use
  4. Why (google wants to change the build system to) gradle

    ? • Simplify and unify project dependency management in IDE & CLI • Think about Eclipse & ant (ci server) • Android Studio is coming ! BETA
  5. Should I use it ? • For small project •

    Easy to convert • For large project • Worth trying if it can simplify the configurations (dependency) • For project with variants • Time for better management tool
  6. How gradle works ? • Lifecycle • Initialisation • Configuration

    • Execution • Build scripts • build.gradle • settings.gradle (optional) • gradle.properties (optional) http://www.gradle.org/docs/current/userguide/build_lifecycle.html
  7. Command • gradle <task name> • tasks • clean •

    assemble • assembleRelease • assembleDebug • aR / asD • check • build (assemble + check)
  8. Project structure • <project_dir>/ • gradle wrapper • configuration files

    • *.gradle / *.properties • app • library • lib1, lib2 …
  9. gradle wrapper • Helper for project building • Compile your

    code anywhere • Easy to install • Just run “gradle wrapper” under project directory
  10. <project>/ gradle.properties • Gradle specific settings • org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError

    - Dfile.encoding=UTF-8 • org.gradle.parallel=true • Project wide variables • var_name_you_want_to_use=value • use with project.var_name_you_want_to_use http://www.gradle.org/docs/current/userguide/build_environment.html
  11. Android packages ! • com.android.support:support-v4:19.1.0 • com.android.support:support-v13:19.1.0 • com.android.support:appcompat-v7:19.1.0 •

    com.android.support:gridlayout-v7:19.1.0 • com.android.support:mediarouter-v7:19.1.0 • com.google.android.gms:play-services:4.4.52 • com.android.support:support-annotations:19.1.0
  12. defaultConfig • versionCode • versionName • minSdkVersion • targetSdkVersion •

    applicationId (old: packageName) • signingConfig • proguardFile • proguardFiles • testApplicationId (old: testPackageName) • testInstrumentationR unner
  13. Build types • default 2 types: debug & release •

    for debugging & signing • custom type • custom.initWith(buildTypes.debug)
  14. Build types • debuggable • jniDebugBuild • renderscriptDebugBuild • renderscriptOptimLevel

    • applicationIdSuffix (old: packageNameSuffix) • versionNameSuffix • signingConfig • zipAlign • runProguard • proguardFile • proguardFiles
  15. lint options • change severity • options: http:/ /tools.android.com/tips/lint-checks •

    ex: fatal 'NewApi', ‘InlineApi' • error/warning/ignore • reports • textReport/xmlReport/htmlReport (boolean) • textOutput/xmlOutput/htmlOutput (file) http:/ /tools.android.com/tech-docs/new-build-system/user-guide#TOC-Lint-support
  16. Other options • packagingOptions
 { exclude ‘META-INF/LICENSE.txt' 
 exclude ‘META-INF/NOTICE.txt’

    } • aaptOptions
 { useAaptPngCruncher = true } • compileOptions
 { sourceCompatibility JavaVersion.VERSION_1_7
 targetCompatibility JavaVersion.VERSION_1_7 }
  17. Happy building • Write build.gradle & settings.gradle in project root

    directory • Write build.gradle in app / library project directory • Build: gradle assemble • <app>/build/outputs/apk/
  18. Build variant • Creating different versions of same application •

    free/paid • multi-apk • Build Variant = Build Type + Product Flavor
  19. Product flavor • Defines a customised version of application build

    by the project • Free / Paid • Amazon / Play Store / blah store • Same type as defaultConfig
  20. Build • gradle assemblePaid • gradle aPD / gradle aPR

    • gradle assembleFree • gradle aFD / gradle aFR
  21. 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 • Tool • http:/ /gradleplease.appspot.com/ • [email protected]:nenick/android-gradle-template.git
  22. Or follow me @ • G+ • http:/ /google.com/profiles/shakalaca •

    Blog • http:/ /23pin.logdown.com • Taipei GDG (G+ Community) • https:/ /plus.google.com/communities/ 100566773212437391191
  23. Why & How ? • Time is money • Use

    aar instead of re-compiling everything • aar: Binary distribution of an Android Library Project • http:/ /tools.android.com/tech-docs/new- build-system/aar-format
  24. How • deploy library • ./gradlew deploy • use local

    repository • ./gradlew as -PuseRepo