android, gradle & dependecies

Download Android, Gradle & Dependecies

Post on 08-Jan-2017

2.432 views

Embed Size (px)

TRANSCRIPT

Android, Gradle & DependenciesWrite Less, build more!

*

About medipo Souza

dipo Souza has over 3 years experience in android development with Degree in Sistemas e Mdias Digitais by Federal University of Cear (UFC) and Specialization in Desenvolvimento de Sistemas para Dispositivos Mveis 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.

*

Whats Gradle?Whats Dependency?Without Gradle..With Gradle!Android Plugin for GradleGradle EnvironmentGradle ConfigsSigning apkOutput apk file nameDeploy dependenciesGenerate sources and javadoc jarOthers configsScheduleGradle Dependencies ScopeGradle Terminal CommandsPopular DependenciesAndroid SupportGoogleSquareCommunityCommunity Java 8 backport

ObjetivoWhats Gradle?

Gra.. what???

Build Automation Tool Can declare and execute tasks to compile, test, packageOpen Source, Multi-language, Multi-platform and Multi-projectCurrent 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) serverFully Programmable Builds using Groovy syntaxRobust Dependency ManagementExtensible with Plugins

Federal University of Cear

ObjetivoWhats DependencyCool, but.. dependency???

Its 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 SonatypeLocal folders.extra\android\m2repositoryextras\google\m2repository

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 dont 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.

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.apkRun a script when your APK is exported, like automatically upload the APK to Play Store beta testing.

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 higherProvides Android-specific elements in gradle build filesAndroid plugin for Gradle also runs independent of Android StudioGradle build system can be updated independently of Android StudioAllow 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 syntaxFaster builder processNew features like NDK support

Federal University of Cear

Gradle Environment

A DSL Language with Groovy syntaxhttps://docs.gradle.org/current/dsl/Files build.gradleProject level: Classpath, Repository, common definitions and tasks.Module level: Plugin configuration, dependencies, definitions and tasks for specific module.File gradle.propertiesCommonly 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

Federal University of Cear

*

Gradle ConfigAutomatically signing apk in buildandroid { ... signingConfigs { releaseSign { storeFile file(System.getProperty("user.home") + "/.gradle/myKeys.keystore") storePassword keyStorePassword keyAlias "myAppAlias" keyPassword myAppAliasPassword } } buildTypes { release { signingConfig signingConfigs.releaseSign } } ...

Federal University of Cear

*

Gradle ConfigOutput Apk with custom file namebuildTypes { 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 } } }}

Federal University of Cear

*

Gradle ConfigDeploy dependencyapply 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" } }}

Federal University of Cear

*

Gradle ConfigGenerate sources and javadoc jar when deploy dependencytask 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}

Federal University of Cear

*

Gradle ConfigAvoid error with duplicated files inside dependenciesandroid { 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 { }}

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'}

Federal University of Cear

*

Gradle Terminal Commands

To build, install and run gradlew installDebugRun jUnit tests gradlew test --continueRun instrumentations tests gradlew connectedAndroidTestShow a graph with module used dependencies by taskgradlew androidDependenciesDeploy dependencygradlew uploadArchives

Federal University of Cear

*

Popular Dependencies

Federal University of Cear

*

Popular DependenciesAndroid Support LibraryAppCompatAdd 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'

Federal University of Cear

*

Popular DependenciesAndroid Support LibraryMultidexProvides 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'

Federal University of Cear

*