dependency management & gradle

23
Dependency management & Gradle Petar Šegina [email protected] Milan Jovanović [email protected] Dejan Novak [email protected] http://croz.net

Upload: phamminh

Post on 27-Dec-2016

257 views

Category:

Documents


1 download

TRANSCRIPT

Page 2: Dependency management & Gradle

2

Primal issue

● Let’s build a command line app!

● Read in a positive number

● Print out the prime factors of this number

● Print out the next prime number

Page 3: Dependency management & Gradle

3

A possible solution

● We need the following methods– int readPositiveNumber()

– int nextPrime(int n)

– List<Integer> primeFactors(int n)

● Sanity check– Create a new module named javatest

– Write out “Hello world!”

Page 4: Dependency management & Gradle

4

There must be a better way

● You probably aren’t the first to encounter a problem

● Others probably wrote a solution to your problem

● Even more people probably used that solution

Page 5: Dependency management & Gradle

5

Libraries to the rescue

● Apache Commons Math– https://commons.apache.org/proper/commons-math/

● Download the JAR– Where does the JAR go?

– How do we use it?

– Read the docs!

– Downsides?

Page 6: Dependency management & Gradle

6

Dependency hell

● We imported one library– What about the sources and documentation?

● What if our library depended on a different library?– What if that library depended on a library?

● …

● What happens when the version changes?

Page 7: Dependency management & Gradle

7

Dependency management

● What if we could just declare– The library we need

– The version we need

● And have it all taken care of for us?

● Maven, Gradle, Ivy● NPM, Composer, Bundler, PIP

Page 8: Dependency management & Gradle

8

The benefits

● Dependency versions match● Clean VCS repository● Relevant data fetched automatically

– Sources

– Documentation

Page 9: Dependency management & Gradle

9

How do I do it?

● https://search.maven.org/● Add the dependency information to build.gradle

– compile 'org.apache.commons:commons-math3:3.6.1'

● Remove the old JAR● Rebuild and run!● Project size is drastically reduced!

Page 10: Dependency management & Gradle

10

How is the project built?

● The plain Java way– java -cp lib -d bin `find -name '*.java’

– java -cp lib:bin Main

● Can we do better?

Page 11: Dependency management & Gradle

11

Gradle

● Declare your build script as a set of applied plugins and related tasks

● The build script is a program– Written in Groovy

● Easily write your own tasks● Easily hook into other tasks

Page 12: Dependency management & Gradle

12

Groovy?

● http://www.groovy-lang.org/ ● Great for building DSLs

– http://docs.groovy-lang.org/docs/latest/html/documentation/core-domain-specific-languages.html

● Can be used for Android development as well!● Grails – a Groovy web framework

– https://grails.org/

Page 13: Dependency management & Gradle

13

Ok, but how do I run things?

Page 14: Dependency management & Gradle

14

gradlew

● A project-local gradle installation● Portable● Downloads the appropriate Gradle and runs it● List all modules with

– ./gradlew projects

● https://docs.gradle.org/current/userguide/gradle_wrapper.html

Page 15: Dependency management & Gradle

15

apply plugin: ‘java’

● ./gradlew :javatest:tasks● assemble & build● clean● jar● https://github.com/musketyr/gradle-fatjar-plugin● check & test

Page 16: Dependency management & Gradle

16

Customizing tasks

task preTest << {

println("We're about to test!")

}

task postTest << {

println("We're done testing!")

}

check.dependsOn preTest

check.finalizedBy postTest

https://docs.gradle.org/current/userguide/more_about_tasks.html

Page 17: Dependency management & Gradle

17

apply plugin: ‘com.android.application’

● clean, assemble, build● install, uninstall● check, test, lint

Page 18: Dependency management & Gradle

18

Build types, product flavors and build variants

● Product flavor → a variant of the code● Built type → how the application is built● Build variant → Product flavor + Build type

● https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/build-system-concepts

Page 19: Dependency management & Gradle

19

Static analysis

● PMD● Checkstyle● Findbugs● Lint

● http://vincentbrison.com/2014/07/19/how-to-improve-quality-and-syntax-of-your-android-code/

Page 20: Dependency management & Gradle

20

Useful plugins and libraries (1)

● Gradle versions– https://github.com/ben-manes/gradle-versions-plugin

● Android Drawable Resizer– https://github.com/Forsety/android-drawable-resizer

● Android Iconify– https://github.com/JoanZapata/android-iconify

● Retrofit– https://square.github.io/retrofit/

Page 21: Dependency management & Gradle

21

Useful plugins and libraries (2)

● storm-gen– https://github.com/turbomanage/storm-gen

● Glide– https://github.com/bumptech/glide

● Butterknife– https://jakewharton.github.io/butterknife/

● Stetho– https://facebook.github.io/stetho/

Page 22: Dependency management & Gradle

22

Useful plugins and libraries (3)

● Mosby MVP– https://github.com/sockeqwe/mosby

– http://hannesdorfmann.com/mosby/

● Dagger– https://square.github.io/dagger/

● RxJava & RxAndroid– https://github.com/ReactiveX/RxJava

– https://github.com/ReactiveX/RxAndroid

Page 23: Dependency management & Gradle

23

Homework

1. Use an external dependency!

2. Publish your own library! (with static analysis)

3. Write an app with a Free and a Paid variant!