dependency management & gradle
TRANSCRIPT
Dependency management&
Gradle
Petar Šegina [email protected] Jovanović [email protected] Novak [email protected]
http://croz.net
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
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!”
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
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?
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?
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
8
The benefits
● Dependency versions match● Clean VCS repository● Relevant data fetched automatically
– Sources
– Documentation
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!
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?
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
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/
13
Ok, but how do I run things?
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
15
apply plugin: ‘java’
● ./gradlew :javatest:tasks● assemble & build● clean● jar● https://github.com/musketyr/gradle-fatjar-plugin● check & test
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
17
apply plugin: ‘com.android.application’
● clean, assemble, build● install, uninstall● check, test, lint
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
19
Static analysis
● PMD● Checkstyle● Findbugs● Lint
● http://vincentbrison.com/2014/07/19/how-to-improve-quality-and-syntax-of-your-android-code/
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/
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/
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
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!