Android Unit Tests with proguard enabled

ぃ、小莉子 提交于 2019-12-17 23:27:03

问题


I have a problem that Proguard strips out methods of my debug APK (I need to run proguard on debug beccause of method dex file limit), even if they are used in the Test apk. E.g. i use GSON addProeprty method in Unit test, but not in the App apk. This method gets stripped away and causes the test to fail. But i do not want to configure proguard to just keep all of GSOn because of the dex file limit, but also do not want to list all methods seperately. is there a way to tell rpguard to consider the unit tests as source code entry points?


回答1:


This is what I did.

Add a custom proguard rules file.

/project/app/proguard-test-rules.pro

# Proguard rules that are applied to your test apk/code.
-ignorewarnings

-keepattributes *Annotation*

-dontnote junit.framework.**
-dontnote junit.runner.**

-dontwarn android.test.**
-dontwarn android.support.test.**
-dontwarn org.junit.**
-dontwarn org.hamcrest.**
-dontwarn com.squareup.javawriter.JavaWriter
# Uncomment this if you use Mockito
#-dontwarn org.mockito.**

The add the following to your build.gradle for your app. To use the proguard file when testing.

/project/app/build.gradle

android {
    debug {
        minifyEnabled true
        testProguardFile 'proguard-test-rules.pro'
    }
}



回答2:


None of the above answers did the trick for me. I had two issues: I needed to also use the default proguard file for testing, and my default proguard file was wrong.

  1. To use the default proguard file, in addition to your own:

    android {
        debug {
            minifyEnabled true
            testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project-test.pro'
        }
    }
    
  2. The default proguard file (and all of the tools/proguard folder) is apparently not replaced by default when you update the SDK tools through Android studio. My machine was using an outdated config file, which was causing weird proguard issues. To update proguard's default config, replace ~/Library/Android/Sdk/tools/proguard/proguard-android.txt with this.




回答3:


Instrumentation tests (and others?) do not use the same proguard file as your debug/release apk's. You might try setting the testProguardFile option inside the debug and release blocks. This test-specific proguard file can be very permissive because it's not being used for the debug/release apk's.




回答4:


I've solved this problem in my build by having an additional "dev" buildType where I enable proguard, but configure it to keep all code in my own package, and a few specific library classes that happen to be used from tests only. I also disable obfuscation in the dev buildType so that it can be debugged from an IDE.

For debug and release builds I use my "real" proguard settings including obfuscation and optimizations.



来源:https://stackoverflow.com/questions/26482119/android-unit-tests-with-proguard-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!