android: Proguard (R8) : A type variable is not in scope (Xamarin.Kotlin.StdLib + keepattributes Signature)

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VS 2022 17.2.0

Description

A simple android project with a reference to Xamarin.Google.Android.Material package 1.6.0 and a proguard file with single line -keepattributes Signature fails.

Steps to Reproduce

  • Install Xamarin.Google.Android.Material 1.6.0 package
  • Add proguard.cfg with single line -keepattributes Signature
  • Build project in release configuration with r8 code shrinker

Did you find any workaround?

No response

Relevant log output

4>  Invalid signature '(TT;)TV;' for method java.lang.Object kotlin.jvm.internal.PropertyReference1.get(java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/PropertyReference0.class: (TaskId:792)
4>  Invalid signature '()TV;' for method java.lang.Object kotlin.jvm.internal.PropertyReference0.get(). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference2.class: (TaskId:792)
4>  Invalid signature '(TD;TE;)TV;' for method java.lang.Object kotlin.jvm.internal.MutablePropertyReference2.get(java.lang.Object, java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference2.class: (TaskId:792)
4>  Invalid signature '(TD;TE;TV;)V' for method void kotlin.jvm.internal.MutablePropertyReference2.set(java.lang.Object, java.lang.Object, java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference.class: (TaskId:792)
4>  Invalid signature '()Lkotlin/reflect/KMutableProperty$Setter<TV;>;' for method kotlin.reflect.KMutableProperty$Setter kotlin.jvm.internal.MutablePropertyReference.getSetter(). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference.class: (TaskId:792)
4>  Invalid signature '()Lkotlin/reflect/KProperty$Getter<TV;>;' for method kotlin.reflect.KProperty$Getter kotlin.jvm.internal.MutablePropertyReference.getGetter(). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/PropertyReference2.class: (TaskId:792)
4>  Invalid signature '(TD;TE;)TV;' for method java.lang.Object kotlin.jvm.internal.PropertyReference2.get(java.lang.Object, java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference0.class: (TaskId:792)
4>  Invalid signature '()TV;' for method java.lang.Object kotlin.jvm.internal.MutablePropertyReference0.get(). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference0.class: (TaskId:792)
4>  Invalid signature '(TV;)V' for method void kotlin.jvm.internal.MutablePropertyReference0.set(java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference1.class: (TaskId:792)
4>  Invalid signature '(TT;)TV;' for method java.lang.Object kotlin.jvm.internal.MutablePropertyReference1.get(java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/MutablePropertyReference1.class: (TaskId:792)
4>  Invalid signature '(TT;TV;)V' for method void kotlin.jvm.internal.MutablePropertyReference1.set(java.lang.Object, java.lang.Object). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)
4>  Info in ...\.nuget\packages\xamarin.kotlin.stdlib\1.6.20.1\buildTransitive\monoandroid12.0\..\..\jar\org.jetbrains.kotlin.kotlin-stdlib-1.6.20.jar:kotlin/jvm/internal/PropertyReference.class: (TaskId:792)
4>  Invalid signature '()Lkotlin/reflect/KProperty$Getter<TV;>;' for method kotlin.reflect.KProperty$Getter kotlin.jvm.internal.PropertyReference.getGetter(). (TaskId:792)
4>R8 : Validation error : A type variable is not in scope.
4>  Signature is ignored and will not be present in the output. (TaskId:792)

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 5
  • Comments: 70 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Any progress here? Now the steps to reproduce are there.

@jonathanpeppers steps to reproduce:

  • create a new android project
  • change .csproj:
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net7.0-android</TargetFramework>
        <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
        <OutputType>Exe</OutputType>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <ApplicationId>com.companyname.ProguardIssue</ApplicationId>
        <ApplicationVersion>1</ApplicationVersion>
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <AndroidLinkTool>r8</AndroidLinkTool>
    </PropertyGroup>
    <ItemGroup>
        <ProguardConfiguration Include="proguard.cfg" />
        <PackageReference Include="Xamarin.Google.Android.Material" Version="1.7.0.2" />
    </ItemGroup>
</Project>
  • add the proguard.cfg file
-keepattributes Signature
  • try compiling in release mode and you will get the following errors:
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.
0>R8: Error  Validation: A type variable is not in scope.

PR #9298 has been merged. So it will be in .net 9. I will talk it over with my boss to see if we can get this into a .net 8 service release.

Hi all,

I was finally able to solve this issue against .NET 7 Android with the latest r8 (8.1.56) with the following proguard.cfg and confirm the sample app launches (if anyone else would like to confirm). The main culprit (as above) in the r8 issue was related to some of the androidx.core.util.** libraries being kept with their signatures (which were invalid according to r8). By sort of painstakingly finding out all my androidx references instead of just keeping all androidx.** { *; }, I was able to solve any resulting runtime issues. Google Android Material itself wasn’t really the problem it just conveniently references the majority of the AndroidX support packages that r8 objected to. I think the reason this affected Xamarin Android so hard has to do with the proguard.cfg that Xamarin was including, but .NET Android doesn’t appear to be including the same ruleset. Anyway it would be great if other people could confirm if this works for them:

-keepattributes AnnotationDefault,
                EnclosingMethod,
                InnerClasses,
                RuntimeVisibleAnnotations, # <-- Kotlin metadata is a runtime-visible annotation
                RuntimeVisibleParameterAnnotations,
                RuntimeVisibleTypeAnnotations,
                Signature

-keep class com.microsoft.maui.** { *; }

-keep class com.bumptech.glide.** { *; }

# Google Android Material
-keep class com.google.android.material.** { *; }

# AndroidX
-keep class androidx.activitiy.** { *; }
-keep class androidx.appcompat.** { *; }
-keep class androidx.autofill.** { *; }
-keep class androidx.biometric.** { *; }
-keep class androidx.core.app.** { *; }
-keep class androidx.core.net.** { *; }
-keep class androidx.core.text.** { *; }
-keep class androidx.core.transition.** { *; }
-keep class androidx.core.view.** { *; }
-keep class androidx.core.widget.** { *; }
-keep class androidx.startup.** { *; }
-keep class androidx.security.crypto.** { *; }
-keep class androidx.window.** { *; }
-keep class com.android.internal.policy.** { *; }
-keep class androidx.fragment.** { *; }
-keep class androidx.work.impl.background.** { *; }
-keep class androidx.viewpager2.** { *; }
-keep class androidx.recyclerview.** { *; }
-keep class com.android.internal.policy.** { *; }
-keep class androidx.percentlayout.** { *; }
-keep class androidx.browser.** { *; }
-keep class android.app.** { *; }
-keep class android.view.** { *; }
-keep class android.util.** { *; }
-keep class android.net.** { *; }
-keep class androidx.navigation.** { *; }

# Java
-keep class java.util.** { *; }

Here was the rule I had previously set that was causing me all the grief (and reproduces this issue): -keep class androidx.** { *; }

I downloaded Xamarin.Android 12.0, and pulled out r8.jar from it: r8.zip

Can you unzip this somewhere, and try using in a project, such as:

<PropertyGroup>
  <AndroidR8JarPath>/path/to/your/r8.jar</AndroidR8JarPath>
</PropertyGroup>

If the problem is r8, I’m not sure what we can do? We can downgrade it?

We’re using the latest on maven: https://mvnrepository.com/artifact/com.android.tools/r8/3.3.28

And we should have dependabot setup to update it.

I found out I’m facing the same exact issue when compiling an Android project on release mode and set the following configuration: image

The proguard.cfg file has the following content:

# 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 this information.
-keepattributes Signature

I am able to replicate the issue on Xamarin.Android 12.3.3.3 and 13.0.0.0.

Update 1: It worked using Xamarin.Android 11.3.0.4 or 12.0.0.3 (just like our friend @themronion mentioned above)

This issue is blocking us. Any updates on a fix?

will it be fixed at some point?

Quarterly prompt? 😃

Hi @vyacheslav-volkov. We have added the “need-info” label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

The source code for r8 is here: https://r8.googlesource.com/r8

If someone would like to file an issue here, maybe Google would respond?

@AndrewNeuronation if you read the comments above, we are now looking for a sample project that hits this issue, so we can investigate further. Thanks!

I downloaded Xamarin.Android 12.0, and pulled out r8.jar from it: r8.zip

Can you unzip this somewhere, and try using in a project, such as:

<PropertyGroup>
  <AndroidR8JarPath>/path/to/your/r8.jar</AndroidR8JarPath>
</PropertyGroup>

If the problem is r8, I’m not sure what we can do? We can downgrade it?

We’re using the latest on maven: https://mvnrepository.com/artifact/com.android.tools/r8/3.3.28

And we should have dependabot setup to update it.

Using the custom/old r8.jar workaround solves the problem for now.

Hello @jonathanpeppers. Somehow I though about “upgrading” r8 but not downgrading it. I can confirm that it fixed the issue. Thanks for this one !

Still, it’s a bit weird that it occurs only on Xamarin applications.

We have exactly the same problem with Hms library. -keepattribute Signature cannot be removed or it causes a crash at runtime. If you keep it, the application does not compile anymore.

That’s quite a blocking one.

BuildError.zip Here is my build log, I am getting the same error but it is coming from androidx.core.util.lrucache.

It is a serious blocking issue for my company. @jonathanpeppers do you have any advice on how to workaround this?

I cant remove the keepattributes Signature from the config because removing it causes a crash in the hms library at runtime