Transcript
Page 1: Android, Gradle & Dependecies

Android, Gradle & Dependencies

Write Less, build more!

Page 2: Android, Gradle & Dependecies

About me

Édipo Souza

Édipo Souza has over 3 years experience in android development with Degree in Sistemas e Mídias Digitais by Federal University of Ceará (UFC) and Specialization in Desenvolvimento de Sistemas para Dispositivos Móveis by Faculdade Sete de Setembro (FA7). He has certifications Oracle Certified Associate Java SE 8 Programmer and Programming in HTML5 with JavaScript and CSS3 Specialist.

Page 3: Android, Gradle & Dependecies

● What’s Gradle?● What’s Dependency?● Without Gradle..● With Gradle!● Android Plugin for Gradle● Gradle Environment● Gradle Configs

○ Signing apk○ Output apk file name○ Deploy dependencies○ Generate sources and javadoc jar○ Others configs

Schedule● Gradle Dependencies Scope● Gradle Terminal Commands● Popular Dependencies

○ Android Support○ Google○ Square○ Community○ Community Java 8 backport

Page 4: Android, Gradle & Dependecies

Federal University of Ceará

ObjetivoWhat’s Gradle?Gra.. what???

● Build Automation Tool

● Can declare and execute tasks to compile, test, package

● Open Source, Multi-language, Multi-platform and Multi-project

● Current version 2.6 (August, 2015)

● Android Studio 1.3 comes with gradle 2.4 by default.

○ Can be updated by gradle-wrapper.properties in gradle folder of your project.

● Great integration from IDE to through Continuous Integration (CI) server

● Fully Programmable Builds using Groovy syntax

● Robust Dependency Management

● Extensible with Plugins

Page 5: Android, Gradle & Dependecies

Federal University of Ceará

ObjetivoWhat’s DependencyCool, but.. dependency???

● It’s that you know as library, whether a jar file or a library project module.

● If you project need some external sources, that is a dependency.

● Referenced in format group:name:version.

('com.google.code.gson:gson:2.3.1')

● Dependency can be located locally or remotely.

● Remote most popular dependencies are in JCenter or MavenCentral.

● Local dependencies are:

○ Local installed repository manager like Artifactory, Nexus Sonatype

○ Local folders.

■ extra\android\m2repository

■ extras\google\m2repository

Page 6: Android, Gradle & Dependecies

Federal University of Ceará

ObjetivoWithout Gradle...● We search on google for a jar file, download and put it on libs folder.

● Push the jar files to our remote repository, making it grow up.

● Have some painkiller when us taken a project that use a old jar version

(what we don’t know) and when we put the latest jar version, it broken.

● We have to make some tasks common, like signing apk, manually.

● Need to have differents projects for specific app variant like demo or full

version.

● Have to live with tons of terrible xml to automate anything.

Page 7: Android, Gradle & Dependecies

Federal University of Ceará

ObjetivoWith Gradle!

After hate it, you will love it!

● We just need to indicate the reference of the dependence we want to use

in format group:name:version.

● We can specify the dependency version or ‘+’ for latest version.

● Gradle search the dependency, download and let it ready to use.

● With scripted build process, we can customize to add new features.

● Build variants make easy to configure an Internal, Demo, Free, Paid build.

● Export APKs with custom filename like app-version.apk● Run a script when your APK is exported, like automatically upload the APK

to Play Store beta testing.

Page 8: Android, Gradle & Dependecies

Federal University of Ceará

ObjetivoAndroid Plugin for Gradle● Current version 1.3.0 (July, 2015)

● Requires Gradle 2.2.1 or higher and Build Tools 21.1.1 or higher

● Provides Android-specific elements in gradle build files

● Android plugin for Gradle also runs independent of Android Studio

● Gradle build system can be updated independently of Android Studio

● Allow override some manifest entries like: applicationId,

minSdkVersion, targetSdkVersion, versionCode, versionName and

others.

● The experimental plugin in version 0.2 are integrated in version 1.3.0.

○ Support to new syntax

○ Faster builder process

○ New features like NDK support

Page 9: Android, Gradle & Dependecies

Federal University of Ceará

Gradle Environment● A DSL Language with Groovy syntax

○ https://docs.gradle.org/current/dsl/

● Files build.gradle○ Project level: Classpath, Repository, common definitions and tasks.

○ Module level: Plugin configuration, dependencies, definitions and tasks

for specific module.

● File gradle.properties○ Commonly used to store definitions like credentials and internal urls.

○ In project root folder they are visible only that project.

○ In user home .gradle folder they can be used by all yours project.

○ Declared in format: localRepoUrl=http://136.166.96.240/artifactory/repo

Page 10: Android, Gradle & Dependecies

Federal University of Ceará

Gradle ConfigAutomatically signing apk in build

android {

...

signingConfigs {

releaseSign {

storeFile file(System.getProperty("user.home") + "/.gradle/myKeys.keystore")

storePassword keyStorePassword

keyAlias "myAppAlias"

keyPassword myAppAliasPassword

}

}

buildTypes {

release {

signingConfig signingConfigs.releaseSign

}

}

...

Page 11: Android, Gradle & Dependecies

Federal University of Ceará

Gradle ConfigOutput Apk with custom file name

buildTypes {

release {

minifyEnabled false

applicationVariants.all { variant ->

variant.outputs.each { output ->

def date = new Date()

def formattedDate = date.format('yyyy-MM-dd')

def namePattern = "MyApp_v" + versionName + "_" + formattedDate

def outFile = new File(output.outputFile.parent, namePattern + ".apk")

if (outFile.exists())

outFile.delete()

output.outputFile = outFile

}

}

}

}

Page 12: Android, Gradle & Dependecies

Federal University of Ceará

Gradle ConfigDeploy dependency

apply plugin: 'com.android.library'

apply plugin: 'maven'

uploadArchives {

repositories {

mavenDeployer {

repository(url: repositoryUrl) {

authentication(userName: deployUser, password: deployPassword)

}

snapshotRepository(url: uri(repositorySnapshotUrl))

pom.version = "1.2.3.4"//-SNAPSHOT"

pom.artifactId = "MyDependencyName"

pom.groupId = "com.ediposouza"

}

}

}

Page 13: Android, Gradle & Dependecies

Federal University of Ceará

Gradle ConfigGenerate sources and javadoc jar when deploy dependency

task sourcesJar(type: Jar) {

from android.sourceSets.main.java.srcDirs

classifier = 'sources'

}

task javadoc(type: Javadoc) {

source = android.sourceSets.main.java.srcDirs

classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

}

task javadocJar(type: Jar, dependsOn: javadoc) {

classifier = 'javadoc'

from javadoc.destinationDir

}

artifacts {

archives javadocJar

archives sourcesJar

}

Page 14: Android, Gradle & Dependecies

Federal University of Ceará

Gradle ConfigAvoid error with duplicated files inside dependencies

android {

packagingOptions.excludes = ['NOTICE.txt', 'LICENSE.txt']

}

Avoid error with different versions of same dependenciesconfigurations.all {

resolutionStrategy.force 'com.android.support:support-annotations:23.0.0'

}

Define flavors to create variants of same appproductFlavors {

demo {

applicationId "com.ediposouza.myapp.demo"

}

full {

}

}

Page 15: Android, Gradle & Dependecies

Federal University of Ceará

Gradle Dependencies Scopedependencies {

final PLAY_SERVICE_VERSION = '7.8.0'

//compile: Include dependency in apk for all variants compile "com.google.android.gms:play-services-base:$PLAY_SERVICE_VERSION"

//demoCompile: Variant of compile specific to 'demo' flavor demoCompile "com.google.android.gms:play-services-ads:$PLAY_SERVICE_VERSION"

//fullDebugCompile: Variant of compile specific to 'debugFull' variant fullDebugCompile 'com.squareup.retrofit:retrofit-mock:1.9.0'

//provided: Don't include dependency in apk, just use it in compile-time provided 'javax.annotation:jsr250-api:1.0'

//testCompile: Include dependency in 'java-only' test build testCompile 'junit:junit:4.12'

//androidTestCompile: Include dependency in 'instrumentation' test build androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'

}

Page 16: Android, Gradle & Dependecies

Federal University of Ceará

Gradle Terminal Commands● To build, install and run

gradlew installDebug

● Run jUnit tests gradlew test --continue

● Run instrumentations tests gradlew connectedAndroidTest

● Show a graph with module used dependencies by taskgradlew androidDependencies

● Deploy dependencygradlew uploadArchives

Page 17: Android, Gradle & Dependecies

Federal University of Ceará

Popular Dependencies

Page 18: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesAndroid Support Library

● AppCompatAdd support for things like ActionBar, ToolBar and Material Design. Also includes dependency of

Support-v4 to backport many new apis like Fragment, Rich Notification, LocalBroadcast,

ViewPager, DrawerLayout and others, to android api 4 or higher.

● CardViewProvides a Card widget with support to elevation and shadows., commonly used in material

design implementations.

● DesignAdds support for material design components like Navigation Drawers, Floating Action Buttons

(FAB), Snackbars, TextInputLayout, and patterns for apps with material design implementations.

compile 'com.android.support:appcompat-v7:23.0.0'

compile 'com.android.support:cardview-v7:23.0.0'

compile 'com.android.support:design:23.0.0'

Page 19: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesAndroid Support Library

● MultidexProvides support for building apps with more than 65536 methods.

● PaletteProvides a helper class to extract prominent colors from an image.

● PercentAdds new widgets PercentFrameLayout and PercentRelativeLayout layout variants with support

to use of percent dimentions.

● RecyclerViewAdds support to a new ListView widget that can display a limited window of large dataset more

efficiently, can easily use custom animations and allow horizontal scroll.

compile 'com.android.support:multidex:1.0.1'

compile 'com.android.support:recycleview-v7:23.0.0'

compile 'com.android.support:percent:23.0.0'

compile 'com.android.support:palette-v7:23.0.0'

Page 20: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesAndroid Support Library

● Support-AnnotationsProvides annotations that allows you to add metadata annotations to your apps.

○ Resource Type@ColorRes, @BoolRes, @IdRes, @StringRes, @DrawableRes, @InterpolatorRes

○ Value Constraints@Size, @IntRange, @FloatRange

public void setAlpha(@IntRange(from=0,to=255) int alpha) { … }public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) { … }

○ Threading Annotations@UiThread, @MainThread, @WorkerThread, @BinderThread

@WorkerThread protected abstract Result doInBackground(Params... params);

@MainThread protected void onProgressUpdate(Progress... values) { … }

compile 'com.android.support:support-annotations:23.0.0'

Page 21: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesAndroid Support Library

● Support-Annotations

○ Permissions@RequirePermission

@RequiresPermission(anyOf = {

Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION})

public abstract Location getLastKnownLocation(String provider);

○ TypeDef@IntDef, @StringDef

public static final int NAVIGATION_MODE_STANDARD = 0;

@Retention(RetentionPolicy.SOURCE)

@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})

public @interface NavigationMode {}

public abstract void setNavigationMode(@NavigationMode int mode);

Page 22: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesGoogle

● Play Services - https://developers.google.com/android/guides/overviewProvides some apis from google services that are android version independent.

After version 6.5 play service are distributed algo in granular mode, which allows you to only

compile the apis that will be used.

This reduce the apk file size and avoid 65k dex method limit.

compile 'com.google.android.gms:play-services:7.8.0'

compile 'com.google.android.gms:play-services-XYZ:7.8.0'● Google+● Google Account Login● Google Actions, Base Client● Google App Indexing● Google App Invites● Google Analytics● Google Cast

● Google Cloud Messaging● Google Drive● Google Fit● Google Location, Activity

Recognition, and Places● Google Maps● Google Mobile Ads

● Google Nearby● Google Panorama Viewer● Google Play Game● Google Wallet● SafetyNet● Android Wear● Mobile Vision

Page 23: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesGoogle● Glide

○ Open Source Image Loading and Caching with image transformation and GIF support.Glide.with(context) //can also be activity or fragment for lifecycle

integration

.load(url)

.centerCrop()

.placeholder(R.drawable.loading_placeholder)

.crossFade()

.into(imageView);

● Gson○ Make easy work with JSON through serialization for Java Objects representation.

Gson gson = new Gson();

Foo foo = new Foo();

String json = gson.toJson(new Foo());

foo = gson.fromJson(json, foo.getClass());

compile 'com.github.bumptech.glide:glide:3.6.1'

https://github.com/bumptech/glide

https://github.com/google/gson

compile 'com.google.gson:gson:2.3.1'

Page 24: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesGoogle● Guava

○ Google Java utilities library with severals APIs to easy work with collections, caching,

primitives support, concurrency libraries, common annotations, string processing, I/O.

* Allows you use Unsigned Primitive

* Calculate Hashing codes

* Math and Reflections helper classes.

● Truth○ Assertion/Proposition framework to make more easy write tests. It can be used in place of

jUnit assertions or alongside it.assertThat(foo).isEmpty()

assertThat(someInt).isEqualTo(5);

assertThat(aString).contains("lah");

assertThat(foo).isNotNull();

compile 'com.google.guava:guava:19.0-rc1'

https://github.com/google/guava

http://google.github.io/truth

assertThat(someCollection).contains("a"); assertThat(someCollection).containsAllOf("a", "b").inOrder();assertThat(someCollection).containsNoneOf("q", "r", "s");assertThat(aMap).doesNotContainEntry("foo", "Bar");assertThat(aMap).containsKey("foo");

compile 'com.google.truth:truth:0.27'

Page 25: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesSquare● Picasso

○ Open Source Image Loading and Caching with image transformation.Picasso.with(context)

.load(url)

.centerCrop()

.placeholder(R.drawable.loading_placeholder)

.into(imageView);

● Retrofit○ Type-safe REST client that turns a REST API into a java interface.

compile 'com.squareup.picasso:picasso:2.5.2'

http://square.github.io/picasso

compile 'com.squareup.retrofit:retrofit:1.9.0'

http://square.github.io/retrofit

public interface GitHubApi {

@GET("/users/{user}/repos")

List<Repo> all(@Path("user") String

user);

}

RestAdapter restAdapter = new RestAdapter.Builder()

.setEndpoint("https://api.github.com").build();

GitHubApi service =

restAdapter.create(GitHubApi.class);

List<Repo> repos = service.all("octocat");

Page 26: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesSquare● Otto

○ Enhanced guava Event Bus designed to decouple different parts of your application while

still allowing them to communicate efficiently.

Create the bus instance in a singleton way: Bus bus = new Bus();@Subscribe public void answerAvailable(AnswerAvailableEvent event) { … }

bus.register(this);

bus.post(new AnswerAvailableEvent(42));

● OkHttp○ Designed to make easy and fast network communication. By default it GZIP shrinks

download sizes and make caching of responses to avoid repetitive calls.OkHttpClient okHttpClient = new OkHttpClient();

Request request = new Request.Builder().url(url).build();

Response response = okHttpClient.newCall(request).execute();

compile 'com.squareup:otto:1.3.8'

http://square.github.io/otto

compile 'com.squareup.okhttp:okhttp:2.5.0'

http://square.github.io/okhttp

Page 27: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesJake Wharton● ButterKnife

○ View injections to avoid android boilerplates. It make the findViewById and set somes

listeners for you through some annotations.@Bind(R.id.title) TextView title;

In onCreate call: ButterKnife.bind(this);

In fragments or ViewHolders: ButterKnife.bind(this, view);@OnClick(R.id.submit) public void submit() { … }

● Byteunits○ Utility classes for converting between granularities of SI and IEC byte units and bit units.

long perception = BinaryByteUnit.TEBIBYTES.toBytes(2);

long usable = DecimalByteUnit.TERABYTES.toBytes(2);

long lost = BinaryByteUnit.BYTES.toGibibytes(perception - usable);

String humanReadable = BinaryByteUnit.format(bytes)

compile 'com.jakewharton:butterknife:7.0.1'

http://jakewharton.github.io/butterknife

compile 'com.jakewharton.byteunits:byteunits:0.9.1'

https://github.com/JakeWharton/byteunits

Page 28: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesJake Wharton● Hugo

○ Annotation-triggered method call logging for your debug builds.apply plugin: 'com.jakewharton.hugo'

○ The use is just about put annotation.@DebugLog

public String getName(String first, String last) {

SystemClock.sleep(15); // Don't ever really do this!

return first + " " + last;

}

○ Log output.V/Example: ⇢ getName(first="Jake", last="Wharton")

V/Example: ⇠ getName [16ms] = "Jake Wharton"

classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'

https://github.com/JakeWharton/hugo

“You can keep the annotations in code because in release build nothing is done by plugin!”

Page 29: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesCommunity

● AndroidViewAnimations○ Easy way to animate views. There are about 60 animations grouped in 8 categories, some

animations are Flash, Shake, Bounce, Tada, FadeIn, FadeOut, SlideInLeft, SlideInRight.YoYo.with(Techniques.Bounce) .duration(700) .playOn(view);

● Calligraphy○ Use custom fonts in a easy way directly on xml layout. Example with

Assets/fonts/MyFont.ttf.@Override protected void attachBaseContext(Context newBase) {

super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));

}

<TextView android:id="@+id/textView"

fontPath="fonts/MyFont.ttf"/>

compile 'com.daimajia.androidanimations:library:1.1.3@aar'

compile 'com.daimajia.easing:library:1.0.1@aar'

compile 'com.nineoldandroids:library:2.4.0'

https://github.com/daimajia/AndroidViewAnimations

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

https://github.com/chrisjenx/Calligraphy

Page 30: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesCommunity

● IcePick○ Eliminates the boilerplate of saving and restoring instance state.

class ExampleActivity extends Activity {

@State String username; // This will be automatically saved and restored

@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Icepick.restoreInstanceState(this, savedInstanceState);

}

@Override public void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

Icepick.saveInstanceState(this, outState);

}

maven {url "https://clojars.org/repo/"}

compile 'frankiesardo:icepick:3.1.0'

provided 'frankiesardo:icepick-processor:3.1.0'

https://github.com/frankiesardo/icepick

Page 31: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesCommunity - Java 8 backport

● RetroLambda○ Requires installed JDK8.

○ Let you run some Java 8 code:

* lambda expressions

* Method references

* Default Methods (early support)

myButton.setOnClickListener(e-> Toast.makeText(this,"Hi",

Toast.LENGTH_LONG).show());

listView.setOnItemClickListener((parent, view, position, id) -> {

Todo todo = adapter.getItem(position);

});

listView.setOnItemClickListener(this::onItemClick);

classpath 'me.tatarka:gradle-retrolambda:3.2.2'

apply plugin: 'me.tatarka.retrolambda'

compileOptions {

sourceCompatibility

JavaVersion.VERSION_1_8

targetCompatibility

JavaVersion.VERSION_1_8

}

https://github.com/orfjackal/retrolambda

Page 32: Android, Gradle & Dependecies

Federal University of Ceará

Popular DependenciesCommunity - Java 8 backport

● StreamSupport○ Backport Java 8 java.util.function (functional interfaces) and java.util.stream (streams).○ Java 8 Streams library backport

○ Java 8 CompletableFuture backport

○ Java 8 Parallel array operations backport

● ThreeTenABP○ Adaptation of the JSR-310, or in others words, Java 8 java.time.* package backport.

In Application Class:@Override public void onCreate() {

super.onCreate();

AndroidThreeTen.init(this);

}

Not in Maven, download and put jar file in libs folder. =s

https://github.com/orfjackal/retrolambda

compile 'com.jakewharton.threetenabp:threetenabp:1.2.0'

https://github.com/JakeWharton/ThreeTenABP

Instant inicio = Instant.now();doSomeThings();Instant fim = Instant.now(); Duration duration = Duration.between(inicio, fim);long durationInMiliseconnds = duracao.toMillis();

○ Java 8 Functional interfaces backport

○ Further java.util.concurrent enhancements

○ Includes Java 8 goodies (Optional, StringJoiner, ...)

Page 33: Android, Gradle & Dependecies

Cool Links:● https://gradle.org/

● https://tools.android.com/tech-docs/new-build-system

● https://tools.android.com/tech-docs/unit-testing-support

● https://tools.android.com/tech-docs/support-annotations

● https://developer.android.com/sdk/installing/studio-build.html

● https://github.com/codepath/android_guides/wiki/Must-Have-Libraries

● http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google

● https://square.github.io/

● https://github.com/JakeWharton

Questions?


Top Related