Android Studio 如何打JAR包
在eclipse中我们知道如何将一个项目导出为jar包,供其它项目使用。
在AS中可以通过修改gradle才处理。
我们新建一个项目MakeJar,在项目中新建一个modle-类型为library
如图:
app为我们的主工程,librarydemo是我们要将其生成jar包的model.
在librarydemo中加入如下代码:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">task makeJar(type: Copy) {
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">delete</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'build/libs/mysdk.jar'</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'build/intermediates/bundles/release/'</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">into</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'build/libs/'</span>)
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">include</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'classes.jar'</span>)
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">rename</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'classes.jar'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mysdk.jar'</span>)
}
makeJar.dependsOn(build)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">
//在终端执行生成JAR包</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">
// gradlew makeJar</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
在Terminal中键入 gradlew makeJar 回车看到如下所示就OK了:
当然在主工程App的build中要加入librarydemo 这个依赖model:
<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">dependencies {
compile fileTree(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">dir</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'libs'</span>, include: [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'*.jar'</span>])
compile <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'com.android.support:appcompat-v7:22.2.0'</span>
compile project(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">':librarydemo'</span>)
}</code>
下面我教大家 , 怎么 提取 .arr文件:
<table style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; border-spacing: 0px; border-collapse: collapse; color: rgb(36, 39, 41); font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; line-height: 16.9px;"><tbody style="margin: 0px; padding: 0px; border: 0px;"><tr style="margin: 0px; padding: 0px; border: 0px;"><td class="votecell" style="margin: 0px; padding: 0px 15px 0px 0px; border: 0px; font-size: 13px; vertical-align: top;"><div class="vote" style="margin: 0px; padding: 0px; border: 0px; text-align: center; min-width: 46px;"><span itemprop="upvoteCount" class="vote-count-post " style="margin: 8px 0px; padding: 0px; border: 0px; font-size: 20px; display: block; color: rgb(106, 115, 124);"><span style="margin: 0px; padding: 0px; border: 0px;">77</span></span><a target=_blank class="vote-down-off" title="这个答案是没有用的" style="margin: 0px auto 10px; padding: 0px; border: 0px; font-size: 1px; color: rgb(0, 119, 204); cursor: pointer; overflow: hidden; display: block; text-indent: -9999em; width: 40px; height: 30px; background-image: url("img/sprites.svg?v=8c1c8cba242e"), none; background-size: initial; background-position: 0px -220px; background-repeat: no-repeat;"><span style="margin: 0px; padding: 0px; border: 0px;">下投票</span></a><span class="vote-accepted-on load-accepted-answer-date" title="The question owner accepted this as the best answer Jan 31 '14 at 16:36." style="margin: 0px auto 10px; padding: 0px; border: 0px; font-size: 1px; overflow: hidden; display: block; text-indent: -9999em; width: 40px; height: 40px; color: rgb(106, 115, 124); background-image: url("img/sprites.svg?v=8c1c8cba242e"), none; background-size: initial; background-position: -40px -270px; background-repeat: no-repeat;"><span style="margin: 0px; padding: 0px; border: 0px;">接受</span></span></div></td><td class="answercell" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; vertical-align: top;"><div class="post-text" itemprop="text" style="margin: 0px 0px 5px; padding: 0px; border: 0px; font-size: 15px; width: 660px; word-wrap: break-word; line-height: 1.3;"><p style="margin-top: 0px; margin-bottom: 1em; padding-top: 0px; padding-bottom: 0px; border: 0px; clear: both;"><span style="margin: 0px; padding: 0px; border: 0px;"><span style="margin: 0px; padding: 0px; border: 0px;">AAR的文件由JAR文件和一些资源文件。</span><span style="margin: 0px; padding: 0px; border: 0px;">下面是转换的步骤:</span></span></p><ol style="margin: 0px 0px 1em 30px; padding: 0px; border: 0px;"><li style="margin: 0px 0px 0.5em; padding: 0px; border: 0px; word-wrap: break-word;"><span style="margin: 0px; padding: 0px; border: 0px;">使用标准的zip提取解压文件AAR</span></li><li style="margin: 0px 0px 0.5em; padding: 0px; border: 0px; word-wrap: break-word;"><span style="margin: 0px; padding: 0px; border: 0px;">找到</span><span style="margin: 0px; padding: 0px; border: 0px;"><span style="margin: 0px; padding: 0px; border: 0px;">classes.jar</span></span><span style="margin: 0px; padding: 0px; border: 0px;">中提取的文件的文件</span></li><li style="margin: 0px; padding: 0px; border: 0px; word-wrap: break-word;"><span style="margin: 0px; padding: 0px; border: 0px;">其重命名为自己的喜好和使用所需jar文件在您的项目</span></li></ol></div></td></tr></tbody></table>
77下投票接受 | AAR的文件由JAR文件和一些资源文件。下面是转换的步骤:
|