Flutter 发布APK时进行代码/资源混淆的坑
Flutter 发布APK时进行代码/资源混淆的坑
1. 关键点
proguard是Java的代码混淆工具,但是当用第三方库的时候,必须要告诉proguard不要检查,因为第三方库里往往有些不会用到的类,没有正确引用,
此时必须设置-dontwarn,比如(-dontwarn io.flutter.**),对第三方库进行warning忽略,否则编译不通过
2.在发布Flutter APK时,如需代码混淆,那么需要在/app/build.gradle中加入如下语句
1 ...
2
3 //这里对签名配置文件进行读取
4 def keystorePropertiesFile = rootProject.file("key.properties")
5 def keystoreProperties = new Properties()
6 keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
7
8 ...
9
10 android {
11
12 ...
13
14 //这里是签名配置,
15 signingConfigs {
16 release {
17 keyAlias keystoreProperties['keyAlias']
18 keyPassword keystoreProperties['keyPassword']
19 storeFile file(keystoreProperties['storeFile'])
20 storePassword keystoreProperties['storePassword']
21 }
22 }
23
24 //发布配置
25 buildTypes {
26 release {
27 signingConfig signingConfigs.release
28
29 minifyEnabled true //资源压缩设置
30 useProguard true //代码压缩设置
31
32 //读取代码压缩配置文件
33 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34 }
35 }
36
37 ...
38
39 }
那么在 "proguard-rules.pro" 文件中,我们就要加入 -dontwarn 语句对第三方库进行报错屏蔽,否则proguard的代码混淆无法进行
示例:
1 #Flutter Wrapper
2 -dontwarn io.flutter.**
3 -keep class io.flutter.app.** { *; }
4 -keep class io.flutter.plugin.** { *; }
5 -keep class io.flutter.util.** { *; }
6 -keep class io.flutter.view.** { *; }
7 -keep class io.flutter.** { *; }
8 -keep class io.flutter.plugins.** { *; }
其中,-keep指定了代码混淆指定的包


浙公网安备 33010602011771号