skip to Main Content

The official guide on how to Add a Flutter screen to an Android app explains how to add a simple page but what about a screen which gets data from native side through flutter channel.

Issue with native calls inside flutter aar : –

After converting module into flutter aar , native code is not included hence native call wont work.

How to add native code / flutter channel code in flutter aar ?

2

Answers


  1. You may missing some steps.

    Follow the instruction:
    add-the-flutter-module-as-a-dependency

    After running flutter build aar you need to do more config as the console hints.

    Flutter will make a local repository for you, and you need to copy it’s path add to your build file.
    And then add the dependency to your host app.

    Hints would be like:

    Consuming the Module
      1. Open <host>/app/build.gradle
      2. Ensure you have the repositories configured, otherwise add them:
    
          repositories {
            maven {
                url '/Users/example/code/flutter_module/build/host/outputs/repo'
            }
            maven {
                url 'http://download.flutter.io'
            }
          }
    
      3. Make the host app depend on the Flutter module:
    
        dependencies {
          debugImplementation 'com.example.flutter_module:flutter_debug:1.0
          profileImplementation 'com.example.flutter_module:flutter_profile:1.0
          releaseImplementation 'com.example.flutter_module:flutter_release:1.0
        }
    
    
      4. Add the `profile` build type:
    
        android {
          buildTypes {
            profile {
              initWith debug
            }
          }
        }
    
    To learn more, visit https://flutter.dev/go/build-aar
    
    Login or Signup to reply.
  2. So your issue is that you try to create aar via the flutter module aar creation means, and you are correct, but only in cases when the flutter part is a standalone module not connected to your app in any way – including the platform channels. There is no way to overcome it currently – at least, I didn’t find any.

    You can achieve what you want, though. What you should do is create a wrapper Android native module that imports the flutter module and communicates with it via channels. In this native wrapper module, the flutter module should be injected via source code rather than by aar, as you understand. After that, create an Android(java, kotlin) interface that commutes your main app messages to/from the wrapper module in a needed way and which is exposed in a needed way from the wrapper module.

    After that, you just create an aar from the Android wrapper module and import it as an aar library into your main project using the exposed interface as the medium of communication.

    I’ve done it all, and it works – the only drawback is debugging and developing both flutter and native code simultaneously is tricky – as you understand, debugging flutter code from AAR is not possible, and flutter has to be built every time. You can overcome that issue also by having two different importing strategies for debug and release – so for release, you import aar, but for debug, you import the android wrapper module with the flutter module inside as a pure code – it would require also importing of the flutter itself to your main project, but I figure it is an acceptable bargain for such flexibility – moreover, you could delete it in some gradle script down the release building lane.

    Also to build that Android wrapper module with the flutter inside into aar you would need to adjust the build scripts and flutter tools like this:

    sed -E -i '' 's~minSdkVersion [0-9]+~minSdkVersion 19~' .android/app/build.gradle
    # workaround for https://github.com/flutter/flutter/issues/95345
    # shellcheck disable=SC2016
    sed -E -i '' 's~$flutterRoot/packages/flutter_tools/gradle/flutter.gradle~../../../android/flutter_module_library.gradle~' .android/Flutter/build.gradle
    
    

    And inside this flutter_module_library.gradle, which you would have to store in you repo you would have to copy and paste the flutter_tools/gradle/flutter.gradle but with places where the error will pop during build fixed – basically the flutter tools build script doesn’t know how to inject dependencies into the library projects – only into the app projects. I don’t remember exact places now – but they will pop up during the first build.

    The ios module builds without any major dancing around(as far as I remember)

    As you can see, it is not a trivial task and, I would say, hardly justified – but it gives you relative power and flexibility. And experience))

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search