您的位置:首页 > 移动开发 > Android开发

android studio 将引用第三方jar和.so的项目打包jar并混淆

2017-11-20 18:55 537 查看


android studio 将引用第三方jar和.so的项目打包jar并混淆


公司项目要打包jar,项目比较特殊引用了第三方jar和.so,在网上也找了很多文章查看,基本找不到有引用第三方.so的,无奈之下只有自己研究,在这里分享一下成果。。。

简单的module打包jar

引用了第三方的jar打包jar

引用了第三方的.so打包jar

打包jar混淆

简单的module打包jar

1.新建一个工程,然后在工程里新建一个module。







2.到工程App的build中要加入librarydemo 这个依赖model: 



dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile project(':mylibrary')
}
1
2
3
4
5
6
7
3.修改classes.jar 导出到library的build/libs/下,并修改名字为mysdk.jar ( 名字改成你想要的名字)。 



//Copy类型
task makeJar(type: Copy) {
//删除存在的
delete 'build/libs/mysdk.jar'
//设置拷贝的文件
from('build/intermediates/bundles/release/')
//打进jar包后的文件目录
into('build/libs/')
//将classes.jar放入build/libs/目录下
//include ,exclude参数来设置过滤
//(我们只关心classes.jar这个文件)
include('classes.jar')
//重命名
rename ('classes.jar', 'mysdk.jar')
}

makeJar.dependsOn(build)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4.打包jar,双击makejar,(假如编译时提示不是内部命令,则要去配置gradle的环境变量) 



5.报错提示:lintOptions { 

abortOnError false 

}时,在build里面添加提示的代码即可 






生成的jar包在你的library的build下的libs下,复制出来就可以使用了。打出来的jar只有源代码的.class 文件,不包含资源文件,由于没有打包资源文件的需求,这里就没有去研究

引用了第三方的jar打包jar

1.基本流程跟上面一样,区别在于在新建的module下的libs里面添加你要引用的第三方库,根据你的需求编写代码。最后执行上面的4.打包jar 



注:这时候一样会生成一个jar包,但是这个jar包里面会拥有你引用的第三方jar的部分文件,在新的工程下引用会报错(提示找不到jar里面的class,把你引用的第三方的jar也同时放进新的工程里面又会报文件重复), 



2.引用第三方的jar,在执行上面的4.打包jar时,要先设置project structure,再执行上面的4.打包jar,打包出来的jar就没有引用第三方jar的部分文件。 





3.使用时把你引用的第三方的jar和打包的jar同时放进新的工程里面使用就可以了 



引用了第三方的.so打包jar

跟引用了第三方的jar打包jar类似,跟app开发一样,把.so包放到相应的位置,配置使用即可 



打包jar混淆

1.在build里面把minifyEnabled设置为true



2.编写proguard-rules.pro文件的混淆代码 



###########################AndroidStudio自带的混淆配置

# 表示混淆时不使用大小写混合类名
-dontusemixedcaseclassnames

# 表示不跳过library中的非public的类
-dontskipnonpubliclibraryclasses

# 打印混淆的详细信息
-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 #指定不混淆所有的JNI方法
-keepclasseswithmembernames class * {
native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans #所有View的子类及其子类的get、set方法都不进行混淆
-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
# 不混淆Activity中参数类型为View的所有方法
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations # 不混淆Enum类型的指定方法
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

# 不混淆Parcelable和它的子类,还有Creator成员变量
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}

# 不混淆R类里及其所有内部static类中的所有static变量字段
-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>(...);
}

#################################以下是自己添加的混淆协议

#忽略警告
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
#-dontshrink
#保护泛型
-keepattributes Signature

#################################以下是自己添加的不要混淆类
-keep class jar.com.mylibrary.DESUtils{
public *;
}

-keep class jar.com.mylibrary.PinYinUtils{
public *;
}
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
3.混淆后的结果 



如有需求把几个jar包打包成一个jar包,请参考http://www.cnblogs.com/xqxacm/p/5893400.html

jar打包参考文章http://blog.csdn.net/lsyz0021/article/details/53107595

未完。。。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: