【问题标题】:What am I doing wrong in proguard rules?我在proguard规则中做错了什么?
【发布时间】:2019-02-15 12:17:54
【问题描述】:

我一直在努力完成我的应用程序中的工作。一切都已设置并正常工作,但是一旦我想设置proguard rules,我的应用程序总是崩溃。我一一检查了每个库,并使用了他们在proguard-rules.pro 文件中提供的所有信息。 然而,我无法让它发挥作用。

我总共有 4 个活动,根据这些规则,其中只有一个会崩溃。我可以进出其他3个。

我启用了调试,因为我了解到如果应用程序在此模式下工作,则意味着它受到保护,当我将其删除并将其上传到 Play 商店时,它将受到反编译保护。如果我错了,请警告我。

这是我的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        multiDexEnabled true
        applicationId "com.example.android.rippleblockexplorer"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            shrinkResources true
        }

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

}

dependencies {
    testImplementation 'junit:junit:4.12'

    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.android.support:design:27.1.1'
    implementation "org.java-websocket:Java-WebSocket:1.3.8"
    implementation 'com.github.curioustechizen.android-ago:library:1.4.0'
    implementation 'com.facebook.shimmer:shimmer:0.1.0@aar'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation "com.android.support:cardview-v7:27.1.1"
    implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'

}

这是我的 proguard-rules.pro:

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

-ignorewarnings
-keep class * {
    public private *;
}

##---------------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

# For using GSON @Expose annotation
-keepattributes *Annotation*

# 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.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson  ----------

# Retain generated class which implement Unbinder.
-keep public class * implements butterknife.Unbinder { public <init>(**, android.view.View); }

# Prevent obfuscation of types which use ButterKnife annotations since the simple name
# is used to reflectively look up the generated ViewBinding.
-keep class butterknife.*
-keepclasseswithmembernames class * { @butterknife.* <methods>; }
-keepclasseswithmembernames class * { @butterknife.* <fields>; }

-keep class org.ocpsoft.prettytime.i18n.**


#DEBUGGING ENABLER
-keepattributes SourceFile,LineNumberTable

-keep class com.example.android.rippleblockexplorer.model.** { *; }
-dontwarn kotlin.Unit

我完全不确定要添加或删除什么。显式调试在某处显示错误,但当我禁用 proguard 时它完全有效。我不明白为什么。

仅供参考,我正在使用 Java thread(你知道,覆盖 Runnable 等);如果这有帮助的话。

也许有人有解决方案或建议?非常感谢任何帮助。

【问题讨论】:

    标签: android build.gradle proguard android-proguard


    【解决方案1】:

    在调试模式下,proguard 文件不起作用。它在发布模式下工作。您可以关注开发者 android 网站。

    https://developer.android.com/studio/build/shrink-code

    【讨论】:

    • 所以我应该删除调试部分并进行测试吗?就这样?
    • 是的,在发布模式下,您应该使用 proguard 规则来避免应用程序的逆向工程。在调试模式下,您必须自己采取安全措施。
    • 你也可以在stackoverflow中关注这个答案。 stackoverflow.com/questions/16559723/…
    • 我怎么可能确定它是受保护的?假设它没有崩溃,但有任何迹象表明它受到保护?
    • 您是否要求处于调试模式?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 2017-05-23
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多