标签:
最近项目在做一个sdk,供别的开发者使用,所以要求导出jar包。
与eclipse不同,android studio 1.0 没提供导出jar包的图形界面。需要结合gradle来生成jar包。
首先 需要设置module应用的gradle插件为 library 代码长这样:
|
1
|
<code class="hljs">apply plugin: ‘com.android.library‘</code> |
这样,build的时候,android studio 1.0会在 module目录的build/intermediates/bundles/release/ 子目录(这个目录以后版本可能会变)里生成一个名为classes的jar包。
如果你的项目没用到assets等资源文件,那你直接拷贝出去就可以用了。
如果想拷贝到outputs目录的话,在module的build.gradle里添加以下代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<code class="hljs">task clearJar(type: Delete) { delete ‘build/outputs/yourname.jar‘}task makeJar(type: Copy) { from(‘build/intermediates/bundles/release/‘) into(‘build/outputs/‘) include(‘classes.jar‘) rename (‘classes.jar‘, ‘yourname.jar‘)}makeJar.dependsOn(clearJar, build)</code> |
如果你和我一样,还需要把assets目录打包到jar包的话,请继续往下看。
我用了一个非主流的方式打包assets,应该没什么后遗症,咱们江湖儿女都懂的,hack一下更健康。
其实也没啥神秘的,也不知道算不算hack,就是利用文件依赖来打包assets。代码长这样:
|
1
2
3
4
5
|
<code class="hljs">dependencies { compile fileTree(include: [‘*.jar‘], dir: ‘libs‘) provided files(‘src/main/assets‘) compile ‘com.android.support:appcompat-v7:21.0.3‘}</code> |
关键是第三行代码。还有一点一定要注意,需要在assets新建一个名为assets的目录,在这个子目录里放置你需要的文件。这样才可以哦。
还没完,不知道啥原因,只有minifyEnabled设置为 true才能把assets打包进去。没有去深究,反正我也需要混淆下代码。
好了,android studio 使用gradle 导出jar包,并打包assets目录 ,我说明白了,对吧。
另附 proguard配置:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<code class="hljs">-libraryjars ‘C:Softandroidadtsdkplatformsandroid-19android.jar‘-optimizations !code/simplification/arithmetic-allowaccessmodification-repackageclasses ‘‘-keepattributes *Annotation*-dontpreverify-dontwarn android.support.**-keep public class * extends android.app.Activity-keep public class * extends android.app.Application-keep public class * extends android.app.Service-keep public class * extends android.content.BroadcastReceiver-keep public class * extends android.content.ContentProvider-keep public class * extends android.view.View { public <init>(android.content.Context); public <init>(android.content.Context,android.util.AttributeSet); public <init>(android.content.Context,android.util.AttributeSet,int); public void set*(...);}-keepclasseswithmembers class * { public <init>(android.content.Context,android.util.AttributeSet);}-keepclasseswithmembers class * { public <init>(android.content.Context,android.util.AttributeSet,int);}-keepclassmembers class * extends android.content.Context { public void *(android.view.View); public void *(android.view.MenuItem);}-keepclassmembers class * extends android.os.Parcelable { static ** CREATOR;}-keepclassmembers class **.R$* { public static <fields>;}-keepclassmembers class * { @android.webkit.JavascriptInterface <methods>;}</methods></fields></init></init></init></init></init></code> |
结伴旅游,一个免费的交友网站:www.jieberu.com
推推族,免费得门票,游景区:www.tuituizu.com
android studio 使用gradle 导出jar包,并打包assets目录
标签:
原文地址:http://www.cnblogs.com/rabbit-bunny/p/4226949.html