1

In my app I’ve recently updated Kotlin from 1.5.31 to 1.6.10. Since then a specific function that uses reflection (kotlin-reflect) stopped working as expected. It works perfectly fine in 1.5.31.

Now, when I run the debug build, the function works correctly, but when I run the release build (the one that is shrunk and obfuscated) it doesn’t.
This was something that I detected by pure chance (unfortunately I’m not using TDD… yes, I know I should, but sometimes we learn the hard way), so I decided it’s time to start testing to avoid this kind of unexpected issues, especially this one.

I’ve been doing some research, but I’ve found nothing on how to do this specific thing.
The idea is to be able to run a test that would catch this issue that only appears when the code is obfuscated.
I’ve implemented a simple test and the test doesn’t fail as it should, because it is using the code that it is not obfuscated.

The test is the following:

class UtilsTest {
   @Test
   fun dataClassAsStringToDataClass_ValidInput_OutputShouldBeEqualInput(){
       //GIVEN
       val defaultTimerData = TimerViewModelData(
           hours = DEFAULT_HOURS,
           minutes = DEFAULT_MINUTES,
           seconds = DEFAULT_SECONDS,
           showSmallTimer = DEFAULT_SHOW_SMALL_TIMER,
           elapsedTimeMode = DEFAULT_ELAPSED_TIME_MODE
       )
       val defaultTimerDataAsString = defaultTimerData.toString()

       //WHEN
       val dataClassRecovered = dataClassAsStringToDataClass<TimerViewModelData>(defaultTimerDataAsString)

       //THEN
       assertEquals(dataClassRecovered, defaultTimerData)
   }
}

As you may have guessed, I have some -keep rules implemented in my proguard-rules.pro file, otherwise the code wouldn’t run even on Kotlin 1.5.31.

I’ve been reading about testProguardFiles in gradle, but I don’t know if this is useful at all, or at least I couldn’t make it work as I expected.

This is part of my build.gradle file:

buildTypes {
   debug {
       versionNameSuffix "-debug"
   }

   release {
       minifyEnabled true
       shrinkResources true
       proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
       testProguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
       signingConfig signingConfigs.release
       ndk {
           debugSymbolLevel "FULL"
       }
   }
}

Let me know in the comments if you need any other information, and I’ll be glad to share.

Just in case, let’s clarify that the purpose of this question is to know how to do the test that should fail when the code changes its behavior as a consequence of being obfuscated, not what has changed between Kotlin versions that is breaking my code… that is another story.

MatJB
  • 106
  • 2
  • 8

0 Answers0