{"id":1651,"date":"2015-03-20T06:42:19","date_gmt":"2015-03-20T13:42:19","guid":{"rendered":"http:\/\/wiebe-elsinga.com\/blog\/?p=1651"},"modified":"2015-03-20T13:42:38","modified_gmt":"2015-03-20T20:42:38","slug":"archive-addition-android-artifacts-with-gradle","status":"publish","type":"post","link":"http:\/\/wiebe-elsinga.com\/blog\/archive-addition-android-artifacts-with-gradle\/","title":{"rendered":"Archive addition Android artifacts with Gradle."},"content":{"rendered":"<div class=\"wpsso-pinterest-pin-it-image\" style=\"display:none !important;\">\n<\/div><!-- .wpsso-pinterest-pin-it-image -->\n\n<p>When building Android applications or libraries common practice is to save your artifacts to a local file storage or repo.<\/p>\n<p>Beside your APK there are some additional artifacts you want\/need to save, and you want gradle to do this. Most common ones are Javadoc, your source files and perhaps your <a href=\"http:\/\/proguard.sourceforge.net\/\">proguard<\/a> generated files like the mapping file.<\/p>\n<p>I naturally wanted a Gradle task to handle this. So lets have a look as some tasks you may want to use.<br \/>\n<\/p>\n<h3>Adding Javadoc archive tasks<\/h3>\n<p>The following will add tasks to generate Javadocs for each build type and assemble it into a jar archive .<\/p>\n<div class=\"codecolorer-container groovy blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;height:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/><\/div><\/td><td><div class=\"groovy codecolorer\">android.<span class=\"me1\">applicationVariants<\/span>.<span class=\"me1\">all<\/span> <span class=\"br0\">&#123;<\/span> variant <span class=\"sy0\">-&gt;<\/span><br \/>\n&nbsp; &nbsp; project.<span class=\"me1\">task<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;${variant.name.capitalize()}Javadoc&quot;<\/span>, type: Javadoc<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.google.de\/search?q=site%3Agroovy.codehaus.org\/%20new\"><span class=\"kw2\">new<\/span><\/a> <a href=\"http:\/\/www.google.de\/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F\"><span class=\"kw3\">File<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;$project.buildDir\/javadoc\/$variant.name&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; source <span class=\"sy0\">=<\/span> variant.<span class=\"me1\">javaCompile<\/span>.<span class=\"me1\">source<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; ext.<span class=\"me1\">androidJar<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;${project.android.sdkDirectory}\/platforms\/${project.android.compileSdkVersion}\/android.jar&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; classpath <span class=\"sy0\">=<\/span> project.<span class=\"me1\">files<\/span><span class=\"br0\">&#40;<\/span>variant.<span class=\"me1\">javaCompile<\/span>.<span class=\"me1\">classpath<\/span>.<span class=\"me1\">files<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">+<\/span> project.<span class=\"me1\">files<\/span><span class=\"br0\">&#40;<\/span>ext.<span class=\"me1\">androidJar<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; options <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linksOffline<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;http:\/\/d.android.com\/reference&quot;<\/span>, <span class=\"st0\">&quot;${project.android.sdkDirectory}\/docs\/reference&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; links<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMemberLevel<span class=\"br0\">&#40;<\/span>JavadocMemberLevel.<a href=\"http:\/\/www.google.de\/search?q=site%3Agroovy.codehaus.org\/%20package\"><span class=\"kw2\">PACKAGE<\/span><\/a><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docEncoding <span class=\"sy0\">=<\/span> <span class=\"st0\">'UTF-8'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encoding <span class=\"sy0\">=<\/span> <span class=\"st0\">'UTF-8'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charSet <span class=\"sy0\">=<\/span> <span class=\"st0\">'UTF-8'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exclude <span class=\"st0\">'**\/BuildConfig.java'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exclude <span class=\"st0\">'**\/R.java'<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; project.<span class=\"me1\">task<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;generate${variant.name.capitalize()}JavadocJar&quot;<\/span>, type: Jar, dependsOn: <span class=\"st0\">&quot;${variant.name.capitalize()}Javadoc&quot;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; classifier <span class=\"st0\">'javadoc'<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; description <span class=\"sy0\">=<\/span> <span class=\"st0\">'Assembles a jar archive containing the generated Javadoc API documentation of $variant.name.'<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.google.de\/search?q=site%3Agroovy.codehaus.org\/%20new\"><span class=\"kw2\">new<\/span><\/a> <a href=\"http:\/\/www.google.de\/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F\"><span class=\"kw3\">File<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;$project.buildDir\/libs\/&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exclude <span class=\"st0\">'**\/BuildConfig.class'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exclude <span class=\"st0\">'**\/R.class'<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; from <span class=\"st0\">&quot;$project.buildDir\/javadoc\/$variant.name&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>When added to your <code class=\"codecolorer bash blackboard\"><span class=\"bash\">build.gradle<\/span><\/code> file, generate your Javadoc jar archive from the command line with <code class=\"codecolorer bash blackboard\"><span class=\"bash\">gradle generateReleaseJavadocJar<\/span><\/code>.<br \/>\n<\/p>\n<h3>Adding Source archive tasks<\/h3>\n<p>The following will add tasks to assemble a jar archive containing the Java sources.<\/p>\n<div class=\"codecolorer-container groovy blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/><\/div><\/td><td><div class=\"groovy codecolorer\">android.<span class=\"me1\">applicationVariants<\/span>.<span class=\"me1\">all<\/span> <span class=\"br0\">&#123;<\/span> variant <span class=\"sy0\">-&gt;<\/span><br \/>\n&nbsp; &nbsp; project.<span class=\"me1\">task<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;generate${variant.name.capitalize()}SourcesJar&quot;<\/span>, type: Jar<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; classifier <span class=\"sy0\">=<\/span> <span class=\"st0\">'sources'<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; description <span class=\"sy0\">=<\/span> <span class=\"st0\">'Assembles a jar archive containing the main sources of $variant.name..'<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.google.de\/search?q=site%3Agroovy.codehaus.org\/%20new\"><span class=\"kw2\">new<\/span><\/a> <a href=\"http:\/\/www.google.de\/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F\"><span class=\"kw3\">File<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;$project.buildDir\/libs\/&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ exclude generated files<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exclude <span class=\"st0\">'**\/BuildConfig.java'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exclude <span class=\"st0\">'**\/R.java'<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; from variant.<span class=\"me1\">javaCompile<\/span>.<span class=\"me1\">source<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>When added to your <code class=\"codecolorer bash blackboard\"><span class=\"bash\">build.gradle<\/span><\/code> file, generate your Sources jar archive from the command line with <code class=\"codecolorer bash blackboard\"><span class=\"bash\">gradle generateReleaseSourcesJar<\/span><\/code>.<br \/>\n<\/p>\n<h3>Adding Proguard archive tasks<\/h3>\n<p>The following will add tasks to assemble a zip archive containing the generated proguard files.<\/p>\n<div class=\"codecolorer-container groovy blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/><\/div><\/td><td><div class=\"groovy codecolorer\">android.<span class=\"me1\">applicationVariants<\/span>.<span class=\"me1\">all<\/span> <span class=\"br0\">&#123;<\/span> variant <span class=\"sy0\">-&gt;<\/span><br \/>\n&nbsp; &nbsp; project.<span class=\"me1\">task<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;generate${variant.name.capitalize()}ProguardFilesJar&quot;<\/span>, type: Zip<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; classifier <span class=\"st0\">'proguard'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; description <span class=\"sy0\">=<\/span> <span class=\"st0\">'Assembles a jar archive containing the Proguard files of $variant.name..'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; destinationDir <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.google.de\/search?q=site%3Agroovy.codehaus.org\/%20new\"><span class=\"kw2\">new<\/span><\/a> <a href=\"http:\/\/www.google.de\/search?as_q=File&amp;num=100&amp;hl=en&amp;as_occt=url&amp;as_sitesearch=java.sun.com%2Fj2se%2F1%2E5%2E0%2Fdocs%2Fapi%2F\"><span class=\"kw3\">File<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;$project.buildDir\/libs\/&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; from <span class=\"st0\">&quot;$project.buildDir\/outputs\/mapping&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> &nbsp; &nbsp;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>When added to your <code class=\"codecolorer bash blackboard\"><span class=\"bash\">build.gradle<\/span><\/code> file, generate your Proguard zip archive from the command line with <code class=\"codecolorer bash blackboard\"><span class=\"bash\">gradle generateReleaseProguardFilesJar<\/span><\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When building Android applications or libraries common practice is to save your artifacts to a local file storage or repo. Beside your APK there are some additional artifacts you want\/need to save, and you want gradle to do this. Most common ones are Javadoc, your source files and perhaps your proguard generated files like the mapping file. I naturally wanted a Gradle task to handle this. So lets have a look as some tasks you may want to use. Adding Javadoc archive tasks The following will add tasks to generate Javadocs for each build type and assemble it into a jar archive . 123456789101112131415161718192021222324252627282930313233343536android.applicationVariants.all &#123; variant -&gt; &nbsp; &nbsp; project.task&#40;&quot;${variant.name.capitalize()}Javadoc&quot;, type: Javadoc&#41; &#123; &nbsp; &nbsp; &nbsp; &nbsp; destinationDir = new File&#40;&quot;$project.buildDir\/javadoc\/$variant.name&quot;&#41; &nbsp; &nbsp; &nbsp; &nbsp; source = variant.javaCompile.source &nbsp; &nbsp; &nbsp; &nbsp; ext.androidJar = &quot;${project.android.sdkDirectory}\/platforms\/${project.android.compileSdkVersion}\/android.jar&quot; &nbsp; &nbsp; &nbsp; &nbsp; classpath = project.files&#40;variant.javaCompile.classpath.files&#41; + project.files&#40;ext.androidJar&#41; &nbsp; &nbsp; &nbsp; &nbsp; options &#123; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linksOffline&#40;&quot;http:\/\/d.android.com\/reference&quot;, &quot;${project.android.sdkDirectory}\/docs\/reference&quot;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; links&#40;&quot;http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/&quot;&#41;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMemberLevel&#40;JavadocMemberLevel.PACKAGE&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docEncoding = &#8216;UTF-8&#8217; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encoding = &#8216;UTF-8&#8217; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charSet = &#8216;UTF-8&#8217; &nbsp; &nbsp; &nbsp; &nbsp; &#125; &nbsp; &nbsp; &nbsp; &nbsp; exclude &#8216;**\/BuildConfig.java&#8217; &nbsp; &nbsp; &nbsp; &nbsp; exclude &#8216;**\/R.java&#8217; &nbsp; &nbsp; &#125; &nbsp; &nbsp; project.task&#40;&quot;generate${variant.name.capitalize()}JavadocJar&quot;, type: Jar, dependsOn: &quot;${variant.name.capitalize()}Javadoc&quot;&#41; &#123; &nbsp; &nbsp; &nbsp; &nbsp; classifier &#8216;javadoc&#8217; &nbsp; &nbsp; &nbsp; &nbsp; description = &#8216;Assembles a jar archive containing the generated Javadoc API documentation of $variant.name.&#8217; &nbsp; &nbsp; &nbsp; &nbsp; destinationDir = new File&#40;&quot;$project.buildDir\/libs\/&quot;&#41; &nbsp; &nbsp; &nbsp; &nbsp; exclude &#8216;**\/BuildConfig.class&#8217; &nbsp; &nbsp; &nbsp; &nbsp; exclude &#8216;**\/R.class&#8217; &nbsp; &nbsp; &nbsp; &nbsp; from &quot;$project.buildDir\/javadoc\/$variant.name&quot; &nbsp; &nbsp; &#125; &#125; When added to your build.gradle file, generate your Javadoc jar archive from the command line with gradle generateReleaseJavadocJar. Adding Source archive tasks The following will add tasks to assemble a jar archive containing the Java sources. 123456789101112131415android.applicationVariants.all &#123; variant -&gt; &nbsp; &nbsp; project.task&#40;&quot;generate${variant.name.capitalize()}SourcesJar&quot;, type: Jar&#41; &#123; &nbsp; &nbsp; &nbsp; &nbsp; classifier = &#8216;sources&#8217; &nbsp; &nbsp; &nbsp; &nbsp; description = &#8216;Assembles a jar archive containing the main sources of $variant.name..&#8217; &nbsp; &nbsp; &nbsp; &nbsp; destinationDir = new File&#40;&quot;$project.buildDir\/libs\/&quot;&#41; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ exclude generated files &nbsp; &nbsp; &nbsp; &nbsp; exclude &#8216;**\/BuildConfig.java&#8217; &nbsp; &nbsp; &nbsp; &nbsp; exclude &#8216;**\/R.java&#8217; &nbsp; &nbsp; &nbsp; &nbsp; from variant.javaCompile.source &nbsp; &nbsp; &#125; &#125; When added to your build.gradle file, generate your Sources jar archive from the command line with gradle generateReleaseSourcesJar. Adding Proguard archive tasks The following will add tasks to assemble a zip archive containing the generated proguard files. 12345678910android.applicationVariants.all &#123; variant -&gt; &nbsp; &nbsp; project.task&#40;&quot;generate${variant.name.capitalize()}ProguardFilesJar&quot;, type: Zip&#41; &#123; &nbsp; &nbsp; &nbsp; &nbsp; classifier &#8216;proguard&#8217; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = &#8216;Assembles a jar archive containing the Proguard files of $variant.name..&#8217; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destinationDir = new File&#40;&quot;$project.buildDir\/libs\/&quot;&#41; &nbsp; &nbsp; &nbsp; &nbsp; from &quot;$project.buildDir\/outputs\/mapping&quot; &nbsp; &nbsp; &#125; &nbsp; &nbsp; &#125; When added to your build.gradle file, generate your Proguard zip archive from the command line with gradle generateReleaseProguardFilesJar.<\/p>\n","protected":false},"author":1,"featured_media":1683,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[133,7],"tags":[154,147,148,131],"_links":{"self":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/1651"}],"collection":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media\/1683"}],"wp:attachment":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}