Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter "Add 2 App" Android integration with adjustments to generated .android

I'm working on an Android app with 3+ years of Kotlin development and want to gradually migrate it to Flutter feature-by-feature. I use option B of the "add 2 app" integration where I depend on the module's source code. Features written Flutter are integrated into the native Android app with FlutterActivity or FlutterFragment. This worked great for the first feature however now I have an issue with migrating the second feature. The second feature requires the packages camera and firebase_ml_vision. Now the problem is that camera requires a min SDK of 21 but the generated Android code in .android sets the min SDK to 16. Additionally firebase_ml_vision requires initialization of Firebase which also needs to be added to .android. I'm thinking about adding .android to VCS and add the required changes however this is generated code. It gets removed when flutter clean is called and generated on flutter pub get. I would have to constantly adjust the generated code when Flutter changes/removes it :( .android only hosts the "skeleton" Android app which is started when the Flutter project is run from IDE (or command line). It is not the host app (the old, native app) where Firebase is already configured. However the .android app is used for quick development cycles. If I cannot use this anymore because of said limitations, I would always have to start the native Android app (host) and lose many benefits of Flutter like hot reload :( Has anyone come across the same problem?

Update:

In this article they clearly state that .android should not be modified or added to VCS. They even mention the camera module as an example. However this does not work and can be reproduced with a few simple steps:

  1. Create a new module: flutter create --template=module --project-name example
  2. cd example
  3. Add camera plugin to pubspec.yaml
  4. flutter pub get
  5. Now running flutter build apk results in the following error:
/Users/sven/Development/example/.android/app/src/main/AndroidManifest.xml Error:
        uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [:camera] /Users/sven/.pub-cache/hosted/pub.dartlang.org/camera-0.5.8+11/android/build/intermediates/library_manifest/release/AndroidManifest.xml as the library might be using APIs not available in 16
        Suggestion: use a compatible library with a minSdk of at most 16,
                or increase this project's minSdk version to at least 21,
                or use tools:overrideLibrary="io.flutter.plugins.camera" to force usage (may lead to runtime failures)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processReleaseManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [:camera] /Users/sven/.pub-cache/hosted/pub.dartlang.org/camera-0.5.8+11/android/build/intermediates/library_manifest/release/AndroidManifest.xml as the library might be using APIs not available in 16
        Suggestion: use a compatible library with a minSdk of at most 16,
                or increase this project's minSdk version to at least 21,
                or use tools:overrideLibrary="io.flutter.plugins.camera" to force usage (may lead to runtime failures)
like image 977
Sven Jacobs Avatar asked Nov 19 '20 11:11

Sven Jacobs


1 Answers

I noticed you are facing 2 issues/ questions:

  1. Minimum android sdk compatibility mismatch: Probably requires some work by the flutter team for a proper fix (I expected the flutter_module/build.gradle to be more useful, but it wasn't (not the one in build/ or .android), but as a workaround, some :app module AndroidManifest.xml changes using uses-sdk will work:
<manifest ...>
   <uses-sdk android:targetSdkVersion="29" android:minSdkVersion="21"
             tools:overrideLibrary="com.example.flutter_module, com.example.flutter_module.host"/>
    ...
</manifest>

(I set the default package name for the flutter module but you should replace both instances of com.example.flutter_module with your package name. (check your build.gradles). What I do here is raise the minSDK for the 2 flutter modules that were added. The error message you got recommended lowering the minSDK for camera to 16, but this is not a good idea (the camera plugin developers set it to 21 probably because the APIs they use have minSDK 21, e.g. camera2).


  1. How to setup firebase/ dependencies config in build.gradle? (i.e. firebase_ml_vision and firebase 'platform specific configuration'): Change your main app's app/build.gradle and AndroidManifest.xml as per the instructions. This is what the doc says:

Perform those Android Gradle file edits on your outer, existing Android app rather than in your Flutter module.


Let me know if you need anymore help :)

like image 141
Ben Butterworth Avatar answered Oct 18 '22 23:10

Ben Butterworth