首先我们打包时会有一些需求,
一个是混淆,一个是打包去掉无用的资源,这些都是打包前需要提前设置好的,
在你运行的项目下build.gradle文件中

 signingConfigs{        release{            def Properties  localProps=new Properties()            localProps.load(new FileInputStream(file('../local.properties')))//读取本地配置文件            def Properties  keyProps=new Properties()            //读取自定义的配置文件,这里的(keystore.props.file)是自定义配置文件的绝对路径            assert localProps['keystore.props.file'];            keyProps.load(new FileInputStream(file(localProps['keystore.props.file'])))            storeFile file(keyProps["storeFile"])            keyAlias keyProps["keyAlias"]            storePassword keyProps["storePssword"]            keyPassword keyProps["keyPassword"]        }    }    buildTypes {        release {//release版              minifyEnabled true//混淆打包            shrinkResources true//去掉不用资源            signingConfig signingConfigs.release            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }

local.properties文件;

## This file is automatically generated by Android Studio.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must *NOT* be checked into Version Control Systems,# as it contains information specific to your local configuration.## Location of the SDK. This is only used by Gradle.# For customization when using a Version Control System, please read the# header note.#Fri Feb 26 12:57:36 CST 2016sdk.dir=D\:\\YAOWork\\adt-bundle-windows-x86_64-20131030\\sdkndk.dir=D\:\\android-ndk-r10keystore.props.file=E\:\\studio\\aaaaa\\工程名\\自定义配置文件名.properties

自定义配置文件:

storeFile=keystore.jksstorePssword=12345678keyAlias=aliaskeyPassword=12345678

设置完这些就可以打包了,步骤如下:
1.打开build选择Generate signed APK

2.选择你所要打包的项目

3.弹出这样的对话框,如果有签名文件可以选择existing没有就选择创建一个新的签名文件

4.只需填写一下几个就行

5.到这就可以选择你说需要的版本了,我这里debug选择的是默认的

6.打完包会在与你项目同级目录下存在一个相应的apk包

注:当使用混淆打包时可能会出现一个问题

Error:Execution failed for task :transformClassesAndResourcesWithProguardForRelease'.> java.io.IOException: Please correct the above warnings first.

这个问题是在说你在混淆打包的时候有些类有可能找不着,所以会包错,和warning
解决的办法:
找到报warning的类都给他在你的proguard-rules.pro混淆器文件中
添加代码-dontwarn 包名+类名.**给取消掉warning就可以了,如果你不想那个类被混淆,那就添加代码:-keep class 包名+类名{*;}或者包名.**{*;}
附带一张混淆文件内容:

# Add project specific ProGuard rules here.# By default, the flags in this file are appended to flags specified# in D:\03.DevSoft\Android\android-sdk_r24.3.4/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the proguardFiles# directive in build.gradle.## For more details, see#   http://developer.android.com/guide/developing/tools/proguard.html# Add any project specific keep options here:# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {#   public *;#}-dontwarn org.dom4j.**-dontwarn org.bouncycastle.**-dontwarn org.apache.commons.logging.impl.**-keep class com.ObjBlockCipherParam{ *; }-keep class org.apache.commons.logging.impl.**{*;}

推荐其他文章地址:
1.http://www.jayfeng.com/2015/11/07/Android%E6%89%93%E5%8C%85%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B/
2.https://www.thoughtworks.com/cn/insights/blog/signing-open-source-android-apps-without-disclosing-passwords
3.http://stackoverflow.com/questions/25310157/gradle-android-studio-signing-with-a-properties-file

更多相关文章

  1. Android平台如何确定deconfig及dtsi的总结
  2. android jar包
  3. static 和 visibility hidden 的区别
  4. [Android从头再来]Android(安卓)JNI相关开发
  5. Android(安卓)OOM ,回收布局文件中ImageView占用的内存.Bitmap O
  6. android分包原理--MultiDex
  7. Android(安卓)6.0 读写SD卡权限问题(续)
  8. [Android(安卓)SQLite]数据存储与访问 - 内部存储
  9. 常用到的Android命令(持续更新)

随机推荐

  1. Android(安卓)不依赖activity的全局悬浮
  2. 修改android桌面图标默认大小
  3. android 笔记 --- Android(安卓)Bitmap
  4. android 整合 maven 环境
  5. Android(安卓)API 实验记录(一)
  6. Android(安卓)EditText 代码实现键盘弹出
  7. Android(安卓)Interface Definition Lang
  8. Android(安卓)之 EditText属性用法介绍
  9. android:screenOrientation 强制屏幕方向
  10. Android(安卓)JUnit单元测试