gpt4 book ai didi

java - 如何为proguard编写规则?

转载 作者:行者123 更新时间:2023-11-29 19:09:15 27 4
gpt4 key购买 nike

我需要在我的项目中实现proguard

我的默认 proguard 设置是这样的

android {
...///

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

....////
}

然后我做了一些小改动

android {
...///

release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

....////
}

这是我的 proguard-ruler.pro 文件 https://ideone.com/ccPvPv

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/Shahar/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
#and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#If you not sure about proguard process so uncomment next line
#-dontobfuscate

# view res/layout/design_layout_snackbar_include.xml #generated:18
#-keep class android.support.design.internal.SnackbarContentLayout { <init>
(...); }

# view res/layout/activity_main.xml #generated:11
#-keep class android.support.design.widget.AppBarLayout { <init>(...); }

# view AndroidManifest.xml #generated:19
#-keep class ru.jollydroid.athdemo.MainActivity { <init>(...); }

#

# Proguard configuration for Jackson 2.x (fasterxml package instead of
codehaus package)
#-keep class com.fasterxml.jackson.databind.ObjectMapper {
# public <methods>;
# protected <methods>;
#}
#-keep class com.fasterxml.jackson.databind.ObjectWriter {
# public ** writeValueAsString(**);
#}
#-keepnames class com.fasterxml.jackson.** { *; }


#-----------------
#-keepnames com.fasterxml.jackson.databind.** { *; }
#
#-keepnames com.squareup.okhttp.** { *; }
#
#-keepnames cryptix.util.test.** { *; }
#
#-keepnames jp.wasabeef.recyclerview.animators.** { *; }
#
#-keepnames cryptix.util.gui.** { *; }
#
#-keepnames ui.activities.** { *; }
#
#-keepnames ui.adapters.** { *; }
#
#-keepnames ui.fragments.** { *; }
#
#-keepnames webServices.controllers.** { *; }
#-------------------------

#Was worked
#-----------------
#-dontwarn com.fasterxml.jackson.databind.**
#
#-dontwarn com.squareup.okhttp.**
#
#-dontwarn cryptix.util.test.**
#
#-dontwarn jp.wasabeef.recyclerview.animators.**
#
#-dontwarn cryptix.util.gui.**
#
# -dontwarn ui.activities.**
#
#-dontwarn ui.adapters.**
#
#-dontwarn ui.fragments.**
#
#-dontwarn webServices.controllers.**
#-------------------------

#-dontwarn java.awt.**
#-dontwarn java.beans.Beans
#-dontwarn javax.security.**
#-keep class javamail.** {*;}
#-keep class javax.mail.** {*;}
#-keep class javax.activation.** {*;}
#-keep class com.sun.mail.dsn.** {*;}
#-keep class com.sun.mail.handlers.** {*;}
#-keep class com.sun.mail.smtp.** {*;}
#-keep class com.sun.mail.util.** {*;}
#-keep class mailcap.** {*;}
#-keep class mimetypes.** {*;}
#-keep class myjava.awt.datatransfer.** {*;}
#-keep class org.apache.harmony.awt.** {*;}
#-keep class org.apache.harmony.misc.** {*;}

# Proguard configuration for Jackson 2.x (fasterxml package instead of
codehaus
package)


#-keep class com.fasterxml.jackson.annotation.** { *; }
#
#-dontwarn com.fasterxml.jackson.databind.**
#
#-keepclassmembers class com.myapp.models.** { *; }
#
#-keepattributes SourceFile,LineNumberTable
#-keep class com.parse.*{ *; }
#-keep class android.content.res.Xm.ResourceParser.** { *; }
#-keep class com.googlecode.** { *; }
#-dontwarn com.parse.**
#-dontwarn com.squareup.picasso.**
#-keepclasseswithmembernames class * {
# native <methods>;
#}

我尝试了很多不同的方法来为库制定规则,但都不起作用

如果我在 Release模式下尝试构建 apk,就会得到输出。

https://ideone.com/Lg8tOT

据我所知,我遇到这个问题是因为我需要为外部库添加规则。

我看到了这个资源

https://github.com/krschultz/android-proguard-snippets

但这对我没有帮助...

我不是 proguard 的高手,所以请告诉我如何添加这个规则?

如果我理解正确,我需要编写我的自定义规则,但是该怎么做呢?

欢迎提问

提前致谢

编辑

进行更改 https://gist.github.com/albinmathew/c4436f8371c9c41461ab

现在gradle看起来像

buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}

我的proguard-rules.pro

https://ideone.com/kxsCEh

我现在的错误

https://ideone.com/JhCE3o

最佳答案

shr​​inkResources true 将压缩您的图像(pngjpeg 等)

minifyEnabled true 会混淆你的代码。

这两条规则会缩小你的代码,apk 会更轻。

由于启用了minify,您需要添加一些规则,您将这些规则添加到proguard-rules.pro 中以保持类不被混淆,因为某些方法需要可见使用。

例如,您有一个 Firebase 使用的类 User。该类的方法和字段需要可见才能发挥作用。 Firebase 会将响应转换为该类并且需要可见。

#-keep class .User.** {*;}

一些规则由库定义,您需要添加它们才能构建项目。

可以使用 -dontwarn 规则避免警告。

-dontwarn cryptix.**

关于java - 如何为proguard编写规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45976225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com