转自:http://www.cnblogs.com/wuya/p/android-studio-gradle-export-jar-assets.html

最近项目在做一个sdk,供别的开发者使用,所以要求导出jar包。

与eclipse不同,android studio 1.0 没提供导出jar包的图形界面。需要结合gradle来生成jar包。

首先 需要设置module应用的gradle插件为 library 代码长这样:

apply plugin: 'com.android.library'

这样,build的时候,android studio 1.0会在 module目录的build/intermediates/bundles/release/ 子目录(这个目录以后版本可能会变)里生成一个名为classes的jar包。
如果你的项目没用到assets等资源文件,那你直接拷贝出去就可以用了。
如果想拷贝到outputs目录的话,在module的build.gradle里添加以下代码:

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)

如果你和我一样,还需要把assets目录打包到jar包的话,请继续往下看。
我用了一个非主流的方式打包assets,应该没什么后遗症,咱们江湖儿女都懂的,hack一下更健康。

其实也没啥神秘的,也不知道算不算hack,就是利用文件依赖来打包assets。代码长这样:

dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs') provided files('src/main/assets') compile 'com.android.support:appcompat-v7:21.0.3' }

关键是第三行代码。还有一点一定要注意,需要在assets新建一个名为assets的目录,在这个子目录里放置你需要的文件。这样才可以哦。
还没完,不知道啥原因,只有minifyEnabled设置为 true才能把assets打包进去。没有去深究,反正我也需要混淆下代码。

好了,android studio 使用gradle 导出jar包,并打包assets目录 ,我说明白了,对吧。

另附 proguard配置:

-libraryjars 'C:\Soft\android\adt\sdk\platforms\android-19\android.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>; } 

更多相关文章

  1. 最全的Android源码目录结构详解
  2. android 代码混淆遭遇conversion to Dalvik format failed with
  3. android客户端从服务器端获取json数据并解析的实现代码
  4. 2017-3-28(React-Native与Android原生代码交互)
  5. Android代码设置EditText只输入数字、字母
  6. android手机开机动画相关代码解析
  7. Android 代码混淆及第三方jar包不被混淆
  8. android 在代码中获取SHA1值
  9. ijkplayer入门之目录介绍及编译

随机推荐

  1. Android(安卓)使用ViewPager实现广告条滚
  2. [学习]创建一个Android工程
  3. Android(安卓)Studio 初体验
  4. Android(安卓)ADT和SDK历史版本离线包下
  5. 制作linux根文件系统
  6. Android(安卓)Jni开发环境搭建
  7. Android(安卓)滑动效果入门篇(一)—— View
  8. androidUI设计之旅 ----布局文件属性解析
  9. Android的通知栏点击后无反应解决办法
  10. Android(安卓)调试桥