maui icon indicating copy to clipboard operation
maui copied to clipboard

[regression/8.0.3] .NET 8 MAUI deploying from visual studio to physical Android device using Release Configuration deletes existing data

Open DP-Technology-LLC opened this issue 2 years ago • 10 comments

Description

In .NET 7, when starting up a MAUI App in Release mode or Debug mode on a physical Android device, Settings and the existing SQLite db (and probably any app data), would remain in place each time you started the app.

After upgrading to .NET 8, DEBUG will not touch the existing Settings and SQLite database just like .NET 7. However if you flip it to Release Configuration, everything gets blown away.

  • There is no GA version in the Version with bug drop down, I am not using a Release Canidate even though it is the one selected.

Steps to Reproduce

  • From Visual Studio, start your app with .NET 7 on a physical Android device using Release Configuration. Create your data.
  • Once again from Visual Studio, start your app with .NET 7 on a physical Android device using Release Configuration. Your data should still be there.
  • Upgrade your existing project to use .NET 8.
  • Once again from Visual Studio, start your app with .NET 8 on a physical Android device using Release Configuration. Your data is no longer there.

Link to public reproduction project repository

No response

Version with bug

8.0.0-rc.2.9511

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.86

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No workaround

Relevant log output

No response

DP-Technology-LLC avatar Nov 15 '23 20:11 DP-Technology-LLC

Hi @DP-Technology-LLC. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

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.

ghost avatar Nov 15 '23 22:11 ghost

Not knowing how you've structured your codebase, as you didn't provide a repro project, my guess would be that if you had files used as output in your build that would be bundled into the APK and released in your application, changing the build configuration and runtime would result in a new output folder and files. This sounds like expected behavior to me.

If you could provide a reproduction project with how you've set up your project, that would help us understand what you're trying to do.

drasticactions avatar Nov 15 '23 22:11 drasticactions

In that regards we didn't change anything outside of the default setup when you create a new .net MAUI project. We will create a new project to see if we can reproduce it. We will update you when with what we find.

DP-Technology-LLC avatar Nov 15 '23 23:11 DP-Technology-LLC

Created a new .net MAUI project targeting .net 8 using:

  • Visual Studio 17.8.0
  • .net 8.0.100
  • .net maui 34.0.43/8.0.100 VS 17.8.34309.116

This is the only change made - In MainPage.xaml.cs put the following code:

protected override async void OnAppearing()
{
    base.OnAppearing(); 
    var currentValue = SampleValue;

    if (currentValue == "whatsgoingon") { await App.Current.MainPage.DisplayAlert("Successfully got set value from Preferences", null, "OK"); }
    else
    {
        SampleValue = "whatsgoingon";
        await App.Current.MainPage.DisplayAlert("No set value in Preferences. Setting value now.", null, "OK");
    }
}


public static string SampleValue
{
    get { return Preferences.Get("SampleValue", String.Empty); }
    set { Preferences.Set("SampleValue", value); }
}

When starting the app on physical android in Release configuration, each push from Visual Studio wipes out the Preference.

From Visual Studio start the app on physical android in Release configuration, there is no set preference (expected). Close the app on the physical device, tap to open app on device, you will see that it successfully fetched the Preference (expected). Once again from Visual Studio, start the app on physical android in Release configuration, there is no set preference (unexpected).

Follow the same steps on in DEBUG Configuration. Your Preference does not get wiped out.

App works as expected when creating a new .net MAUI project targeting .net 7 using:

  • Visual Studio 17.8.0
  • .net 8.0.100
  • .net maui 34.0.43/8.0.100 VS 17.8.34309.116

App works as expected when creating a new .net MAUI project targeting .net 7 using:

  • Visual Studio 17.6.4
  • .net 7.0.100
  • .net maui 33.0.46/7.0.100 VS 17.6.33815.320

DP-Technology-LLC avatar Nov 16 '23 00:11 DP-Technology-LLC

@DP-Technology-LLC I can see this behavior in Xamarin.Forms apps too. I guess its Visual Studio bug.

kcrg avatar Nov 16 '23 05:11 kcrg

I also had this after migrating from .Net 7.0 to .Net 8.0.

LeDahu22 avatar Nov 16 '23 14:11 LeDahu22

This situation is a reason to sit back and laugh at life. For us targeting .net 7 with DEBUG configuration, the performance on the physical android device was extremely poor. In release mode performance was acceptable, so all android pushes were done in release mode. .net 8 blogs reveal tremendous performance improvements (highlighting android in the quantitative comparisions) but upon the first push all user data has been wiped clean.

@kcrg @LeDahu22 : thanks for verifying this issue.

DP-Technology-LLC avatar Nov 16 '23 14:11 DP-Technology-LLC

Hello?, ello?, llo?, lo?, o?

User Data still gone for-ev-er.

DP-Technology-LLC avatar Nov 20 '23 22:11 DP-Technology-LLC

This issue can be easily reproduced in the following way :

  • From a .NET 7 branch, you start your app (debug mode) and let it create your files (databases, directories, config files, or whatever)
  • Then switch to .NET 8 branch with the same functionnal code, you start your app (debug mode again), everything is erased during deployment with VS (even before the app is started on the device).

In my case, app data are stored on the SD Card of my Android device.

Edit: also, every app permissions are removed (going from .NET 7 to 8 and backwards)

LeDahu22 avatar Nov 28 '23 10:11 LeDahu22

Same issue here.

I use the "AppDataDirectory" as root folder and create specific subfolder i.e. for database, logs, attachments, ....

var rootFolder = FileSystem.Current.AppDataDirectory; var dbPath = Path.Combine(rootFolder, "db");

Result on Android: /data/user/0/com.company.app/files/db

SailDev avatar Jan 07 '24 15:01 SailDev

Verified this issue with Visual Studio 17.10.0 Preview 1, I can use the code of 'DP-Technology-LLC' to reproduce it on the Android platform with the .NET MAUI project.

jaosnz-rep avatar Mar 11 '24 09:03 jaosnz-rep

I experience the same or a related issue. This is only happening when I deploy a release build to an Android device or emulator.

  • I create a sqlite db in code using EF Core for SQLite
  • Add data to the database using the app
  • Deploy the app again and can see from the behavior that the database/sqlite file was no longer present. The code follow the same path as with a first install when there is no database to connect to
  • In my code I check if I can connect to the database/file. If not I create a new database. As expected the added data is not present in the newly created database.

The problem is that the database/sqlite file is removed during the deployment. This is only happens with a Release build being deployed to an Android device or emulator. It doesn't happen with a debug build or on a Window machine or emulator.

I was hoping this is a setting, but it looks like the deployment removes all the files in the app folder first.

I have added the code below. As you can see I increase the version number with 5 when I cannot connect to the database (first install). With the second deployment the database should be present and this path should not be followed. However, unfortunately it actually is.

`public async Task EnsureCreated() { int versionNumber = int.Parse(_configuration["DatabaseVersionNumber"]);

if (await _context.Database.CanConnectAsync())
{
    // Connect after first time creation
    Version version = await _context.Versions.FirstOrDefaultAsync();
    if (version != null)
    {
        // Check for database update
        if (version.Number < versionNumber)
        {
            // Detach current version
            _context.Entry(version).State = EntityState.Detached;

            // Recreate database
            await _context.Database.EnsureDeletedAsync();
            await _context.Database.EnsureCreatedAsync();

            // Update version number
            version = new()
            {
                Number = versionNumber
            };

            _context.Versions.Add(version);

            await _context.SaveChangesAsync();
        }
    }
}
else
{
    // First time creation
    await _context.Database.EnsureCreatedAsync();

    // Update version number
    Version version = new()
    {
        Number = versionNumber + 5
    };

    _context.Versions.Add(version);

    await _context.SaveChangesAsync();
}

Ensured?.Invoke();

}`

eschimmel avatar Jun 05 '24 08:06 eschimmel

I think I found the problem. There is an important difference between a Debug and Release deployment. Looking at the Output window you can see that during a Release deployment the app is uninstalled first: C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe -s R9AT309TR5K uninstall -k com.myorg.myapp C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe -s R9AT309TR5K shell cmd package uninstall com.myorg.myapp

An uninstall removes the complete app, including all the files. In my case, the sqlite database file is removed.

In a way, I am relieved to find out this seems to be the problem, because this will not be an issue when the app is installed from the play store.

eschimmel avatar Jun 06 '24 01:06 eschimmel