androidstudio打包混淆
⑴ android 不混淆可以打包吗
可以。
混淆打包流程:
1.在proguard-project.txt文件中添加不需要混淆的类和第三方的jar包
这个是保持自己包中不需要混淆的类,如果有些类调用了jni也不需要混淆,不然会出错。还有如果项目中有其他项目作为library引入,那这些项目的一些类也不能混淆。
这个是保持项目中的第三方jar不混淆
另外加上上面几句话,不然会在控制台中报warning警告
⑵ 如何使用Android Studio打包混淆的Jar
使用AS打包混淆Jar包,网络一下,一片一片的,但是很多都是零零散散的写得不是很详细或是直接拷贝,按照他们的教程测试总不是很顺利,所以这里我就把我个人学习AS打包混淆Jar的成果总结出来,希望对大家有帮助。个人觉得写得还是比较详细的
使用gradle混淆打包Jar
使用AS开发项目,引入第三方库是非常方便的,我们只需要在build.gradle中配置一行代码就可以轻松引入我们需要的开发库。那么gradle可以帮我们混淆打包Jar吗?答案是当然可以!
那么我们如何打包Jar呢?其实我们在编译项目的时候,AS已经帮我们在目录build/intermediates/bundles/release/classes.jar打好了Jar。那么我们需要做的就是把Jar进行混淆的工作了。这里以个人项目bannerDemo为例,混淆步骤如下:
在你的library的build.gradle文件中加入如下代码:
task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
// 未混淆的jar路径
injars 'build/intermediates/bundles/release/classes.jar'
// 混淆后的jar输出路径
outjars 'build/outputs/cocolove2-banner-1.1.0.jar'
// 混淆协议
configuration 'proguard-rules.pro'}
配置混淆协议
1.我们先把AS自带的协议配置进来中文注释,笔者添加
# This is a configuration file for ProGuard.# http://proguard.sourceforge.net/index.html#manual/usage.html## Starting with version 2.2 of the Android plugin for Gradle, these files are no longer used. Newer# versions are distributed with the plugin and unpacked at build time. Files in this directory are# no longer maintained.#表示混淆时不使用大小写混合类名-dontusemixedcaseclassnames#表示不跳过library中的非public的类-#打印混淆的详细信息-verbose# Optimization is turned off by default. Dex does not like code run# through the ProGuard optimize and preverify steps (and performs some# of these optimizations on its own).-dontoptimize##表示不进行校验,这个校验作用 在java平台上的-dontpreverify# Note that if you want to enable optimization, you cannot just# include optimization flags in your own project configuration file;# instead you will need to point to the# "proguard-android-optimize.txt" file instead of this one from your# project.properties file.-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService-keep public class com.android.vending.licensing.ILicensingService# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native-keepclasseswithmembernames class * {
native <methods>;
}# keep setters in Views so that animations can still work.# see http://proguard.sourceforge.net/manual/examples.html#beans-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}# We want to keep methods in Activity that could be used in the XML attribute onClick-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}# The support library contains references to newer platform versions.# Don't warn about those in case this app is linking against an older# platform version. We know about them, and they are safe.-dontwarn android.support.**# Understand the @Keep support annotation.-keep class android.support.annotation.Keep-keep @android.support.annotation.Keep class * {*;}-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}
2.AS自带的配置文档还是不够的,我们还需要加入如下配置
这里只展示基本操作,在实际开发中可能需要更多依赖,要根据具体情况引入自己需要的依赖包
#下面代码中的xx是指我个人的配置路径,涉及个人信息,这里以xx代替
#引入依赖包rt.jar(jdk路径)
-libraryjars /xxx/xx/xx/jdk1.8.0_77.jdk/Contents/Home/jre/lib/rt.jar
#引入依赖包android.jar(android SDK路径)
-libraryjars /xx/xx/xx/Android/sdk/platforms/android-24/android.jar
#如果用到Appcompat包,需要引入
-libraryjars /xxx/xxx/xx/xxx/MyApplication/library-banner/build/intermediates/exploded-aar/com.android.support/appcompat-v7/24.1.1/jars/classes.jar
-libraryjars /xx/xx/xx/xx/MyApplication/library-banner/build/intermediates/exploded-aar/com.android.support/support-v4/24.1.1/jars/classes.jar
#忽略警告
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
#保护泛型
-keepattributes Signature
3.加入自己不想混淆的配置根据实际需求配置
-keep class com.cocolove2.library_banner.view.**{*;}
在命令行执行命令混淆Jar,提示BUILD SUCCESFUL表示成功!
//mac./gradlew makeJar//windowsgradlew makeJar
示例展示
我这里以混淆library-banner库为例
- #log提示缺少依赖Jar,或者路径不对
- 解决办法:乖乖的引入缺少的依赖jar和修改路径
- #提示如下异常[INFO] java.io.IOException: Can't read [D:Program
- FilesJavajdk1.8.0_91jrelib t.jar] (Can't process class [com/oracle/net/Sdp$1.class] (Unsupported class version number
- [52.0] (maximum 51.0, Java 1.7)))
- 解决办法:
- 下载最新proguard(支持Java 8的版本),然后将下载的文件解压。
- 将andorid sdk/tools/proguard/lib中的jar包,替换为刚下载解压文件中的lib包。
proguard5.2.1下载地址
1.首先我们要看看下我们的buildTool的配置,如下图:
混淆报错解决办法个人遇到的
阅读