András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

Tag: xamarin android

  • Xamarin.Android : Google Play app target level error solution

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    If you are uploading an application to the Google Play, and it gives the following error:

    Your app currently targets API level 28 and must target at least API  level 29 to ensure it is built on the latest APIs optimized for security  and performance. Change your app's target API level to at least 29 
    

    You have to change the target framework and the target android version in your androidmanifest.xml or in the UI editor of the manifest. To change it, set the Target Framework in the properties tab of your android application

    Changing target framework

    Then Navigate to Android Manifest option, and set target android version

    Setting Target Android version

    If you are re archive your application, it will upload successfully.

  • Xamarin.Android: Missing aapt.exe

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    When trying to deploy an application to simulator or device, Visual Studio gives the following error:

    Cannot find `aapt.exe`. Please install the Android SDK Build-Tools package with the `C:\Program Files (x86)\Android\android-sdk\tools\android.bat` program.
    

    To solve this problem you sould go to Visual Studio’s Tools then Android and select SDK Manager option.

    Navigate to this option

    It will give an error dialog, that the SDK tool files are corrupted. Click repair.

    Click on Repair

    The mentioned batch command in the error message is deprecated by the way.

    Microsoft Windows [Version 10.0.19042.804]
    (c) 2020 Microsoft Corporation. All rights reserved.
    
    C:\Users\banditoth>cd C:\Program Files (x86)\Android\android-sdk\tools\
    
    C:\Program Files (x86)\Android\android-sdk\tools>android.bat
    **************************************************************************
    The "android" command is deprecated.
    For manual SDK, AVD, and project management, please use Android Studio.
    For command-line tools, use tools\bin\sdkmanager.bat
    and tools\bin\avdmanager.bat
    **************************************************************************
    
    Invalid or unsupported command ""
    
    Supported commands are:
    android list target
    android list avd
    android list device
    android create avd
    android move avd
    android delete avd
    android list sdk
    android update sdk
    
    C:\Program Files (x86)\Android\android-sdk\tools>
    
    
  • Xamarin.Forms: Bypass SSL Certificate validation on Android

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    Disclaimer: Bypassing SSL Certificate validation in production releases are not recommended. It can make your application vulnerable by hackers and reverse engineers, and your users will be unprotected from the bad guys. Consider to use the following codes with compile directives.

    But in the other hand, it can be handful to just ignore the certification errors in development enviroment. Local machines has self signed certificates, and it is easier to just bypass the validation method, rather than set the self signed certificate acceptance in our client applications.

    In Xamarin.Android (Lower than Android 10) and Xamarin.iOS, use the ServicePointManager in order to make your own certificate validator algorithm. The code below just accepts every cert.

    ServicePointManager.ServerCertificateValidationCallback =
                (message, certificate, chain, sslPolicyErrors) => true;
    

    To bring Android 10 also to work, construct your HttpClient with the following constructor:

                var httpClientHandler = new HttpClientHandler();
    #if DEBUG
                httpClientHandler.ServerCertificateCustomValidationCallback =
                    (message, certificate, chain, sslPolicyErrors) => true;
    #endif
                var httpClient= new HttpClient(httpClientHandler);
    
  • [Xamarin.Android]: Cleartext HTTP traffic to not permitted

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    If your Android application gives the following error, when trying to use a HttpClient:

    Java.IO.IOException: 'Cleartext HTTP traffic to 192.168.0.10 not permitted'
    

    Set usesClearTraffic property to true in the AndroidManifest.xml:

    <application android:label="MobileSolution.Android" 
    	     android:theme="@style/MainTheme"
                 android:usesCleartextTraffic="true">
    		
    </application>
    

    The restriction was introduced in Android 8. More explanation: https://koz.io/android-m-and-the-war-on-cleartext-traffic/

  • Xamarin Android: Dismiss lock screen or show activity on lock screen

    This content has 5 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    To gain an Activity to be showed on the lock screen, or bypass the phone’s lock screen (aka Keyguard) for important notification handling, for example an Incoming VOIP call, you need to do the following:

    Set the permission for disabling keyguard. Add the following permission to the AndroidManifest.xml:

      <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    

    Add flags to the Activity’s Window in order to show the activity on the lock screen, or to bypass the lock screen.

    this.Window.AddFlags(WindowManagerFlags.DismissKeyguard | WindowManagerFlags.ShowWhenLocked);
    

    Dismiss the keyguard if needed. (CrossCurrentActivity plugin is used to get the current activity):

    KeyguardManager keyguardManager = (KeyguardManager)CrossCurrentActivity.Current.Activity?.GetSystemService(Context.KeyguardService);
    if (keyguardManager != null)
    {	
    	keyguardManager.RequestDismissKeyguard(CrossCurrentActivity.Current.Activity, null);
    }