Android Dev

Flutter Tutorial – The way to Migrate to V2 Embedding

If you happen to hopped on the Flutter bandwagon in it’s early days, most certainly you might have a undertaking or two that have been created previous to model 1.12 of Flutter. If that’s the case, you might have seen this message everytime you run Pub get in one in every of your initiatives:

This app is utilizing a deprecated model of the Android embedding.
To keep away from sudden runtime failures, or future construct failures, attempt to migrate this app to the V2 embedding.

Check out the docs for migrating an app: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects

Now, the doc itself has the steps it’s essential to observe to make this warning disappear, but it surely doesn’t at all times make clear what to vary and the place.

This text offers you a step-by-step walkthrough on migrating your Flutter utility to V2 Embedding so you may make that warning go away for good.

Computerized Migration – The Simple Manner Out

It must be stated that you could forego this technique of migration in case your utility could be simply recreated. So what does that imply?

Effectively, if the code in your utility isn’t advanced, then you possibly can simply save the information in your lib folder and create a brand new undertaking utilizing flutter create. That means, you’ll have a undertaking that’s already migrated to V2 Embedding and can simply want to repeat paste the code you might have in your lib folder.

However, in case your undertaking is extra advanced – let’s say it is a package deal that has platform particular code – you’ll most likely be higher off by migrating it manually.

Handbook Migration – Comply with These Steps

  1. Open the MainActivity.kt (or .java) file in your utility
  2. It’s good to take away any content material this file has and simply depart it naked with a category declaration (except you might have particular logic there).
  3. Take away all the imports and ensure to have one import that’s this:
import io.flutter.embedding.android.FlutterActivity;

The top consequence needs to be as follows:

import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {   
    
}
  1. Open the AndroidManifest.xml file and alter the title attribute underneath the applying tag to ${applicationName} – so it seems like this:
<utility      
    android:title="${applicationName}"> 
     .... 
</utility>
  1. It’s good to add the next meta information inside your utility tags:
<meta-data           
     android:title="flutterEmbedding"       
     android:worth="2" />
  1. If you need a selected Splash display screen conduct, you will want to take away the Splash display screen meta tag:
<meta-data                     android:title="io.flutter.app.android.SplashScreenUntilFirstFrame"                android:worth="true" />
  1. Then head to your types.xml file and configure the LaunchTheme there with the drawable of your liking:
<?xml model="1.0" encoding="utf-8"?>
<assets>    
     <type title="LaunchTheme" mum or dad="@android:type/Theme.Black.NoTitleBar">        <merchandise title="android:windowBackground">@drawable/launch_background
        </merchandise>   
    </type>
</assets>

Your AndroidManifest.xml will look one thing like this after all of the adjustments above:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package deal="PACKAGE_NAME">
<utility        
    android:title="${applicationName}"    
    android:label="APPLICATION_LABEL"      
    android:icon="@mipmap/ic_launcher">     
        <exercise           
            android:title=".MainActivity"
            android:exported="true"   
            android:launchMode="singleTop"     android:theme="@type/LaunchTheme"                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"            android:hardwareAccelerated="true"            android:windowSoftInputMode="adjustResize">     
                <intent-filter>    
                    <motion android:title="android.intent.motion.MAIN"/>                                <class android:title="android.intent.class.LAUNCHER"/>  
                    </intent-filter>     
       </exercise>     
       <meta-data       
               android:title="flutterEmbedding"         
            android:worth="2" /> 
 </utility>
</manifest>

AndroidX Assist

Your undertaking may additionally be wanted to be migrated to make use of AndroidX libraries as an alternative of the older help libraries. You may be alerted for this whenever you construct and run your utility:

Your app isn’t utilizing AndroidX. To keep away from potential construct failures, you possibly can rapidly migrate your app by following the steps on https://goo.gl/CP92wY.

Fixing that is quite easy, as Android Studio has in-built help for migrating to AndroidX.

Begin by opening the Android folder of your Flutter utility as a standalone undertaking

Click on on Refactor → Migrate to AndroidX:

Image
Dropdown menu to Migrate to AndroidX

You’ll then be prompted to avoid wasting a replica of your undertaking and previous that, the migration course of will happen.

Errors You Would possibly See

Throughout this migration course of, you may encounter a number of errors when constructing your utility. Probably the most distinguished ones are:

  • Unable to get mutable Home windows surroundings variable map
  • cvc-complex-type.2.4.a: Invalid content material was discovered beginning with ingredient ‘base-extension’. One in every of ‘{layoutlib}’ is predicted
  • Warning: This model solely understands SDK XML variations as much as 2 however an SDK XML file of model 3 was encountered. This will occur should you use variations of Android Studio and the command-line instruments that have been launched at completely different instances

The primary two errors are associated to one another and each stem from the identical root trigger. It’s as a result of your undertaking was arrange with an outdated Gradle model and it’s wanted to improve it.

To take action, observe these steps:

  1. Open the Android folder in your Flutter utility as a standalone undertaking
  2. Click on on File → Mission Construction:

Image
Dropdown menu to pick out Mission Construction

  1. Change the Gradle model to one thing more moderen and that matches the present Android Studio model you might be utilizing

Image
AGP and Gradle settings display screen

You might additionally use the AGP Improve Assistant to do that as nicely by going to Instruments →AGP Improve Assistant:

Image
Dropdown menu to improve AGP utilizing the AGP Improve Assistant

The third difficulty, which is a warning, is likely to be attributable to having an outdated model of the Android SDK Instruments. To learn to try this, you possibly can go here.

Your undertaking needs to be now absolutely migrated, compiling and working easily.

If you wish to learn different articles I’ve written, you possibly can test them out right here:

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker