一、在Android studio 中有两种的方式可以进行打包
(1)第一种方式
build->generate signed apk
而后选择要进行打包的项目,next 进行选择签名文件,填写密码 next
在下面打包有两种选择,一种是release 一种是debug ,在debug的状态下是不会进行签名和混淆的,
只有选择release的状态下才会进行签名和混淆
点击finish即可签名打包成功
(2)第二种方式
在build variants 里面将要打包的项目进行选择 debug 或是 release
而后在项目的build.gradle文件中进行配置文件位置,密码等
- signingConfigs {
- release {
- storeFile file("签名文件位置")
- keyAlias " "
- keyPassword " 密码 "
- storePassword " 密码 "
- }
- }
生成的apk文件在:android studio工程的存储路径下 app/build/outputs/apk 中
二、在android studio中进行混淆
在eclipse中的是 proguard.cfg 文件 而在android studio中使用的是proguard-rules.pro ,不过直接拿过来用也是可以的
只要在build.gradle里面进行配置下即可,如下是在build.gradle中的配置
- buildTypes {
- debug{//测试下的配置
- minifyEnabled false
- shrinkResources false
- signingConfig signingConfigs.release
- }
- release {//正式的配置
- minifyEnabled true // false表示不混淆,true表示混淆
- shrinkResources true //移除无用的resource文件
- //如果用eclipse里面的proguard.cfg,直接替换 proguard-rules.pro 即可
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- signingConfig signingConfigs.release
- }
- }
以下是整个的build.gradle的配置文件
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 21
- buildToolsVersion '21.1.2'
- defaultConfig {
- applicationId "com.**.**"
- minSdkVersion 8
- targetSdkVersion 21
- versionCode 1
- versionName "1.0"
- }
- packagingOptions {
- exclude 'META-INF/DEPENDENCIES'
- exclude 'META-INF/LICENSE.txt'
- exclude 'META-INF/license.txt'
- exclude 'META-INF/NOTICE.txt'
- exclude 'META-INF/notice.txt'
- exclude 'META-INF/ASL2.0'
- exclude 'META-INF/NOTICE'
- exclude 'META-INF/LICENSE'
- exclude '!META-INF/MANIFEST.MF'
- exclude 'META-INF/MANIFEST.MF'
- }
- sourceSets.main {
- jniLibs.srcDirs = ['libs']
- }
- productFlavors {
- }
- /**
- * 签名设置
- */
- signingConfigs {
- release {
- storeFile file("**")
- keyAlias "**"
- keyPassword "**"
- storePassword "**"
- }
- }
- buildTypes {
- debug{
- minifyEnabled false
- shrinkResources false
- signingConfig signingConfigs.release
- }
- release {
- minifyEnabled true
- shrinkResources true
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- signingConfig signingConfigs.release
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- compile 'com.umeng.analytics:analytics:latest.integration'
- compile files('libs/httpclient-android-4.3.3.jar')
- compile files('libs/httpmime-4.3.5.jar')
- compile files('libs/jpush-sdk-release1.7.3.jar')
- compile files('libs/mframework.jar')
- compile files('libs/nineoldandroids-2.4.0.jar')
- compile files('libs/pinyin4j-2.5.0.jar')
- compile files('libs/ShareSDK-Core-2.5.9.jar')
- compile files('libs/ShareSDK-QZone-2.5.9.jar')
- compile files('libs/ShareSDK-SinaWeibo-2.5.9.jar')
- compile files('libs/ShareSDK-TencentWeibo-2.5.9.jar')
- compile files('libs/ShareSDK-Wechat-2.5.9.jar')
- compile files('libs/ShareSDK-Wechat-Core-2.5.9.jar')
- compile files('libs/ShareSDK-Wechat-Favorite-2.5.9.jar')
- compile files('libs/ShareSDK-Wechat-Moments-2.5.9.jar')
- compile files('libs/umeng-analytics-v5.6.4.jar')
- compile files('libs/swipemenulistview.jar')
- compile files('libs/xstream-1.4.7.jar')
- compile project(':**')
- compile project(':wheel')
- }
- compile 'com.umeng.analytics:analytics:latest.integration'
以下是混淆文件 proguard-rules.pro
- -optimizationpasses 5
- -dontusemixedcaseclassnames
- -dontskipnonpubliclibraryclasses
- -dontpreverify
- -verbose
- -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
- -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.app.backup.BackupAgentHelper
- -keep public class * extends android.preference.Preference
- -keep public class com.android.vending.licensing.ILicensingService
- -keepclasseswithmembernames class * {
- native <methods>;
- }
- -keepclasseswithmembers class * {
- public <init>(android.content.Context, android.util.AttributeSet);
- }
- -keepclasseswithmembers class * {
- public <init>(android.content.Context, android.util.AttributeSet, int);
- }
- -keepclassmembers enum * {
- public static **[] values();
- public static ** valueOf(java.lang.String);
- }
- -keep class * implements android.os.Parcelable {
- public static final android.os.Parcelable$Creator *;
- }
- -dontwarn com.sina.**
- -keep class com.sina.** { *;}
- -dontwarn com.handmark.**
- -keep class com.handmark.** { *;}
- -dontwarn com.viewpagerindicator.**
- -keep class com.viewpagerindicator.** { *;}
- -dontwarn cn.jpush.**
- -keep class cn.jpush.** { *; }
- -keepattributes Signature
- -keep class sun.misc.Unsafe { *; }
- -keep class com.google.gson.examples.android.model.** { *; }
- -dontwarn com.ant.**
- -keep class com.ant.** { *; }
- -dontwarn com.nineoldandroids.**
- -keep class com.nineoldandroids.** { *; }
- -dontwarn m.framework.**
- -keep class m.framework.** { *; }
- -dontwarn org.apache.http.entity.mime.**
- -keep class org.apache.http.entity.mime.** { *; }
- -dontwarn com.handmark.pulltorefresh.library.**
- -keep class com.handmark.pulltorefresh.library.** { *; }
- -dontwarn com.loopj.android.http.**
- -keep class com.loopj.android.http.** { *; }
- -dontwarn org.apache.http.**
- -keep class org.apache.http.** { *; }
- -dontwarn org.apache.http.**
- -keep class org.apache.http.** { *; }
- -dontwarn com.nostra13.universalimageloader.**
- -keep class com.nostra13.universalimageloader.** { *; }
- #-libraryjars libs/android-support-v4.jar
- -dontwarn android.support.v4.**
- -keep class android.support.v4.** { *; }
- -keep interface android.support.v4.app.** { *; }
- -keep public class * extends android.support.v4.**
- -keep public class * extends android.app.Fragment
- -keep class com.google.gson.JsonObject { *; }
- -keep class com.badlogic.** { *; }
- -keep class * implements com.badlogic.gdx.utils.Json*
- -keep class com.google.** { *; }
- -keep class com.google.** {
- <fields>;
- <methods>;
- }
- -keepclassmembers class * extends java.io.Serializable {
- static final long serialVersionUID;
- private static final java.io.ObjectStreamField[] serialPersistentFields;
- private void writeObject(java.io.ObjectOutputStream);
- private void readObject(java.io.ObjectInputStream);
- java.lang.Object writeReplace();
- java.lang.Object readResolve();
- }
- # Gson specific classes
- -keep class sun.misc.Unsafe {
- <fields>;
- <methods>;
- }
- # -keep class com.google.gson.stream.** { *; }
- # Application classes that will be serialized/deserialized over Gson
- -keep class com.**.** { //当项目中有用到泛型的时候,要让项目中的module对象不进行混淆
- <fields>;
- <methods>;
- }
- -keepclassmembers class * {
- public <init> (org.json.JSONObject);
- }
- -keep public class com.**.**.R$*{
- public static final int *;
- }
- -dontwarn u.aly.**
- -keep class u.aly.** { *; }
配置完后,之间像,签名打包进行打包即可
三、多渠道打包
在项目的 androidmanifest.xml 中添加
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
而后在项目的build.gradle中添加即可
//多渠道打包 productFlavors { LianXiang {} Baidu {} AnZhi{} F360{} xiaomi {} MeiZu{} WanDouJia{} QQYingYongBao{} productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } }
通过 build->generate signed apk 进行打包,即可得到多个渠道的apk包