Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AndroidPackagingOptionsExclude cannot be modified #8456

Closed
t4e-dev opened this issue Oct 24, 2023 · 8 comments
Closed

AndroidPackagingOptionsExclude cannot be modified #8456

t4e-dev opened this issue Oct 24, 2023 · 8 comments
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@t4e-dev
Copy link

t4e-dev commented Oct 24, 2023

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

VS2022 17.7.5

Description

We have a large former Xamarin Android application which we converted to .NET7.
The application is using some bindings which are using Kotlin and kotlin-reflect
The application works in DEBUG mode but when it gets packaged in RELEASE mode the app crashes on Android.
The exception in ADB log is :

java.lang.AssertionError: Built-in class kotlin.Any is not found at 
kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns$3.invoke(KotlinBuiltIns.java:93)

After digging a lot around we discovered that debug APK and release APK have different content regarding kotlin modules.
This lead us to the csproj ItemGroup element AndroidPackagingOptionsExclude which was added with this PR : 7356

We played around with the element and tried to remove it with

  <ItemGroup>
    <AndroidPackagingOptionsExclude Remove="$([MSBuild]::Escape('*.kotlin_*'))" />
  </ItemGroup>

in the main project. But our remove was ignored during build and the exclusion was still happening.
So we modified the Xamarin.Android.Common.targets file directly by commenting out the wildcard. This resulted into a working application again.

So we can confirm so far that the wildcard '.kotlin_ may be to restrictive and should be removed from the target file. The filter is removing the files *kotlin_builtins which are used by the package kotlin.reflect.jvm

Regarding the remove of the AndroidPackagingOptionsExclude in our csproj : Are we doing something wrong with the itemgroup above ?

Steps to Reproduce

  • Build an android app that is using kotlin-reflect
  • Build in release mode

Did you find any workaround?

Modifying xamarin.android.common.targets

<ItemGroup>
	<AndroidPackagingOptionsExclude Include="DebugProbesKt.bin" />
	<!--<AndroidPackagingOptionsExclude Include="$([MSBuild]::Escape('*.kotlin_*'))" />-->
</ItemGroup>

Relevant log output

No response

@t4e-dev t4e-dev added Area: App Runtime Issues in `libmonodroid.so`. needs-triage Issues that need to be assigned. labels Oct 24, 2023
@grendello grendello added Area: App+Library Build Issues when building Library projects or Application projects. and removed Area: App Runtime Issues in `libmonodroid.so`. needs-triage Issues that need to be assigned. labels Oct 24, 2023
@jonathanpeppers
Copy link
Member

@t4e-dev can you add a Directory.Build.targets that has:

<Project>
  <ItemGroup>
    <AndroidPackagingOptionsExclude Remove="$([MSBuild]::Escape('*.kotlin_*'))" />
  </ItemGroup>
</Project>

Or if you want to try removing all of them: <AndroidPackagingOptionsExclude Remove="@(AndroidPackagingOptionsExclude)" />

If this works, @dellis1972 maybe we should consider moving this <ItemGroup> to AutoImport.props. Then customers could put this directly in their project file instead of having to use a Directory.Build.targets?

Or is what we are doing a bad idea, because it breaks kotlin.reflect.jvm?

@dellis1972
Copy link
Contributor

We need some way to keep the stuff that kotlin.reflect.jvm needs. Perhaps the NuGet (assuming there is a binding) can make sure the required files are included. But there should be a way to override this. The AutoImport.props move sounds like a good idea.

@t4e-dev
Copy link
Author

t4e-dev commented Oct 25, 2023

Thanks for your quick support.
I added the Directory.Build.targets file and tried both options you mentioned above.
After that I run a build with --verbosity diag

AndroidPackagingOptionsExclude
                       *.kotlin_*
                       DebugProbesKt.bin

is still present in the diag log. So the instructions from the targets file are somehow ignored.
I think you can verify this easily with

  • creating an .NET7 android app / library
  • create Directory.Build.targets file
  • run build with diag verbosity
  • search in output for "AndroidPackagingOptionsExclude"

@dellis1972
Copy link
Contributor

@t4e-dev

Placing this in the Directory.Build.targets will work around the issue for now

<Project>
  <Target Name="_Foo" AfterTargets="AfterBuild">
    <ItemGroup>
        <AndroidPackagingOptionsExclude Remove="$([MSBuild]::Escape('*.kotlin_*'))" />
    </ItemGroup>
  </Target>
</Project>

The down side is you will have allot of kotlin files in your apk/aab, I assume most of which are not needed.
Can you give us a list of the files which are required by kotlin.reflect.jvm? Its it just files that contain 'builtins'? We will need to update the *.kotlin_* regex to make sure to include those and not remove them.

@dellis1972
Copy link
Contributor

Looking at the file list I see

unzip -lv bin/Release/net8.0-android/com.companyname.KotlinStuff-Signed.apk | grep builtin
      90  Defl:N       72  20% 01-01-1981 01:01 9463aeee  META-INF/services/kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoader
     928  Defl:N      561  40% 01-01-1981 01:01 8e787046  kotlin/annotation/annotation.kotlin_builtins
    3685  Defl:N     1521  59% 01-01-1981 01:01 f41c30cf  kotlin/collections/collections.kotlin_builtins
     200  Defl:N      157  22% 01-01-1981 01:01 c69fe143  kotlin/coroutines/coroutines.kotlin_builtins
     646  Defl:N      394  39% 01-01-1981 01:01 d8d2330d  kotlin/internal/internal.kotlin_builtins
   18640  Defl:N     5252  72% 01-01-1981 01:01 f77f89dd  kotlin/kotlin.kotlin_builtins
    3399  Defl:N     1292  62% 01-01-1981 01:01 cb3b8aed  kotlin/ranges/ranges.kotlin_builtins
    2396  Defl:N     1266  47% 01-01-1981 01:01 852a0981  kotlin/reflect/reflect.kotlin_builtins

I suspect only the reflect files are required for kotlin.reflect.jvm. So it looks like its dependent on the NuGet references :(

@dellis1972
Copy link
Contributor

Can you try this. It should preserve all the 'builtins' .

<Project>
  <Target Name="_Foo" AfterTargets="AfterBuild">
    <ItemGroup>
        <AndroidPackagingOptionsExclude Remove="$([MSBuild]::Escape('*.kotlin_*'))" />
        <AndroidPackagingOptionsExclude Include="$([MSBuild]::Escape('*.kotlin_m*'))" />
    </ItemGroup>
  </Target>
</Project>

@t4e-dev
Copy link
Author

t4e-dev commented Oct 25, 2023

That did the trick! Thanks a lot. :-) Perfect support!

Regarding the required files : Honestly I cannot really tell what the required files are.
We created a Xamarin binding around : https://payworks.mpymnt.com/cp_int_pos_paybutton_overview/cp_int_pos_paybutton_integration.html#android
kotlin-reflect is something that is used internally and so is not transparent to us.

My best guess for a general product like Visual Studio is to look into the magic that happens when in an Android native application

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

in a build.gradle file are applied.
My guess would be that the same rules that are beeing applied there should match the build process for NET7 Android Maui app.

@jpobst jpobst changed the title AndroidPackagingOptionsExclude can not be modified AndroidPackagingOptionsExclude cannot be modified Nov 16, 2023
@dellis1972
Copy link
Contributor

This is fixed via #8459

@github-actions github-actions bot locked and limited conversation to collaborators Feb 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

No branches or pull requests

4 participants