Android代码混淆报错

代码混淆方法:
1.添加proguard-project.txt文件至app下目录
2.在该文件中添加要不混淆的代码,Gson、butterknife和eventbus等第三方包,除了将jar包中的包下类不混淆外,还有一些特殊的方法,也要保证不混淆,如下:

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:
#先不混淆一些继承自带控件的view
-keep public class * extends android.app.Fragment  
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.**
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.kingnew.tian.model.** { *; }
-keep class com.kingnew.tian.Cropcategorys.Model.** { *; }


#-optimizationpasses 5          # 指定代码的压缩级别
#-dontusemixedcaseclassnames   # 是否使用大小写混合
#-keepattributes *Annotation*
#-ignorewarnings                # 抑制警告
#-dontskipnonpubliclibraryclasses       # 是否混淆第三方jar
#不混淆Gson
##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
##---------------End: proguard configuration for Gson  ----------
#不混淆butterknife
##---------------Begin: proguard configuration for butterknife  ----------
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-dontwarn butterknife.**

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}
##---------------End: proguard configuration for butterknife  ----------

#不混淆eventbus
##---------------Begin: proguard configuration for eventbus  ----------
-keep class org.greenrobot.eventbus.** { *; }
-dontwarn org.greenrobot.eventbus.**
-keepclassmembers class ** {
    public void onEvent*(**);
    void onEvent*(**);
}
##---------------Begin: proguard configuration for eventbus  ----------

3.在app的build文件中,将minifyEnabled 置为true

buildTypes {
        release {
            minifyEnabled true/*minifyEnabled主要用来控制是否运行混淆的。*/
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }

4.然后在签名打包之后,保证app能够正常运行

ps:可参考该文:https://segmentfault.com/a/1190000002910305
5. 需要做混淆保护的内容
一般四大组件都会默认不被混淆

-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 

自定义组件的类也要设置为不被混淆,否则找不到控件

方案一、直接保护view的子类,因为自定义控件都是继承view

-keep public class * extends android.app.Dialog
-keep public class * extends android.view

方案二、具体保护某个自定义控件

-keep public class com.viewpagerindicator.TitlePageIndicator

被调用的第三方jar包的类等

如果你使用了第三方的包,你需要使用一下配置,让ProGuard知道库中的一些类并不是在所有的API中可用:

-libraryjars   libs/roboguice-2.0.jar 
-dontwarn roboguice.**  

7.需要特别处理的第三方包

    GSON
    butterknife
    eventbus
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中进行打包混淆时,可能会遇到包冲突的报错。这种情况下,需要解决包冲突问题。 首先,查看报错信息中提到的包冲突详情。在你提供的引用中,报错信息中提到了冲突的包路径为"android/support/design/widget/CoordinatorLayout$LayoutParams.class"。 解决包冲突的方法有多种,以下是一种解决方法: 1. 打开项目中的build.gradle文件。 2. 在buildTypes中找到release配置,并将minifyEnabled属性设置为true,表示开启代码混淆。 3. 在proguardFiles中添加proguard-rules.pro文件,该文件用于指定混淆规则。 4. 在proguard-rules.pro文件中添加以下规则,以保持特定的类和接口不被混淆: -keep class okhttp3.** { *; } -keep interface okhttp3.** { *; } -dontwarn okhttp3.** -keep class okio.** { *; } -keep interface okio.** { *; } -dontwarn okio.** -keep class com.hitomi.** { *; } -keep interface com.hitomi.** { *; } -dontwarn com.hitomi.** 5. 重新编译并打包你的应用程序,这次应该不再出现包冲突的报错。 请注意,这只是一种解决包冲突问题的方法之一。在实际操作中,可能需要根据具体情况进行调整和修改。 参考引用: - http://blog.csdn.net/u012246458/article/details/79446784 - build.gradle文件中的代码 - proguard-rules.pro文件中的代码<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [AndroidStudio打包混淆](https://blog.csdn.net/qq_24570087/article/details/80620546)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值