compileOnly dependency in an Android Flutter plugin - android

I am working on an Android plugin for a Flutter project. For this plugin, I need to reference a compileOnly dependency.
It seems that I have to add the compileOnly dependency in two Gradle files.
plugin build.gradle (to have code completion working)
app build.gradle (otherwise the building of the app fails with a gradle error)
/Users/0000000-android_battery_status-dalosy/example/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java:19: error: cannot access EMDKManager
flutterEngine.getPlugins().add(new com.dalosy.android_battery_status.AndroidBatteryStatusPlugin());
^
class file for com.symbol.emdk.EMDKManager not found
Is there a way to avoid the second one?
It feels bad to require the users of my plugin to add a compileOnly dependency in their Android project.

Related

How to utilize Firebase both in Flutter as well as native android within the same project?

I have a project in flutter that relies on Firebase for authentication, firestore, and functions. However, I have a receiver class that extends BroadcastReceiver and runs in the background, on the android side of my application. Within this receiver class I utilize Firestore, Analytics, Auth and Cloud_Functions.
Given this unique android setup, I need to instantiate the following dependencies in my app level build.gradle file:
implementation 'com.google.firebase:firebase-firestore:24.3.1'
implementation 'com.google.firebase:firebase-auth:21.0.8'
implementation 'com.google.firebase:firebase-firestore-ktx:24.3.1'
implementation 'com.google.firebase:firebase-messaging:23.0.8'
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.firebase:firebase-functions-ktx:20.1.1'
implementation 'com.google.firebase:firebase-analytics-ktx:21.1.1'
implementation platform('com.google.firebase:firebase-bom:30.4.0')
implementation 'com.google.firebase:firebase-auth-ktx'
I also have the following in my Flutter pubspec.yaml file:
firebase_core: ^1.24.0
cloud_firestore: ^3.5.1
firebase_messaging: ^13.1.0
cloud_functions: ^3.3.9
firebase_auth: ^3.11.2
as the Flutter project itself handles a lot of other things as well that are dependent on Firebase (shared components between iOS and Android that use these libraries).
I've tested my app on just iOS and it builds and runs fine since I don't use any kind of iOS specific classes in the background, and I've also run the android app by itself without flutter and it builds and runs fine. But when I bring those native android classes into my flutter project, I get runtime errors when spinning my app up for android:
E/flutter (15469): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter (15469): #0 FirebaseCoreHostApi.initializeCore
package:firebase_core_platform_interface/…/pigeon/messages.pigeon.dart ...
in reference to the following line of code:
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
I'm still able to run the iOS side of the app fine, and all dependencies are up to date for me, so I'm fairly confident the issue stems from me having the same packages installed within my app.build.gradle file.
Note: If I remove those dependencies in my gradle file, then the app breaks and throws errors immediately as it can't find those libraries anywhere.
My question is: How can I implement these firebase packages and reference them from within a native Kotlin file found in the android section of my Flutter project?
I suspect I'm just not searching for the right thing, but everything online just says "Update your packages in your pubspec.yaml file, and mine are already updated so those responses aren't helpful at all.
It seems you have imported the same dependencies on both the flutter project and the generated kotlin project which leads to conflicts. And it is not necessary to import both Java and kotlin dependencies simultaneously if you are writing the Kotlin App in the app-level build gradle file.
Make sure you have the required Gradle version with the Android Gradle plugin version and resolve most of the unnecessary conflicts. As per the Gradles, the issue is because of the app-level Gradle dependencies. Hence remove all manually and re-build the flutter project or add dependencies one by one manually on kotlin priority basis. It will do the job
The Flutter Firebase package depends on the native Firebase package, so it may look as if the same package is imported separately on android native and flutter side but build tools already manage it so it is allowed
But the flutter and native side need to use the same library version to avoid any conflicts
Please use the below command to find version inconsistencies and fix it
./gradlew dependencies
More info on the above command can be found in the below-mentioned link
https://medium.com/mindorks/avoiding-conflicts-in-android-gradle-dependencies-28e4200ca235

Flutter GeneratedPluginRegistrant: can't find android dependency for a plugin

I wrote a Flutter plugin that find the absolute path for a file on an Android system using the PickiT library. The plugin, called Flutter absolute path, has the dependency of the PickiT library added on the plugin build.gradle:
dependencies {
implementation 'com.github.HBiSoft:PickiT:2.0.5'
}
But when I add this plugin to my app and I try to compile it, I receive this error:
<APP_PATH>/GeneratedPluginRegistrant.java:34: error: cannot access PickiTCallbacks
flutterEngine.getPlugins().add(new net.altermundi.flutter_absolute_path.FlutterAbsolutePathPlugin());
^
class file for com.hbisoft.pickit.PickiTCallbacks not found
The only way that I found to make my plugin working is to add the library to my project on the app/build.gradle dependencies also.
How can I make the plugin Android dependency implicit on the whole project without adding it?

Missing common classes on a KMM project when integrated in an Android App as a Gradle project

I have an existing Android app where I like to integrate a KMM library project as a git submodule. We want to integrate a git submodule on local builds and using a maven repository dependency on CI builds.
To achieve this, I added the directory of the submodule in my settings.gradle via includeBuild and substitute all dependencies of a specific module:
includeBuild('my-kmm-library-project') {
dependencySubstitution {
substitute module('com.example.any-kmm-artifact:any') using project(':any')
}
}
Adding
implementation 'com.example.any-kmm-artifact:any:1.0.0'
to my application build.gradle file allows access to all classes and packages in the android sourceset int the submodule /my-kmm-library-project/any/src/androidMain. But I have no option to import "classes" from the commonMain sourceset. I already tried to let androidMain depend on commonMain.
kmm/build.gradle.kts:
val androidMain by getting {
dependsOn(commonMain)
...
}
This doesn't lead to any success.
I also tried to add a jvm() target on KMM project and let jvmMain depend on androidMain, but this produces compile error, cause the Android SDK classes cannot be resolved.
I think the main problem is that, the classes are just compiled into the aar which can be assembled. But the module is integrated as a gradle project implementation.
Is there a KMM option to compile "common classes" and access them through a gradle module?

Firebase libraries with conflicting library versions fail to run my app

So i'm using the following firebase library version in my app:
Project's build.gradle:
firebase_version = '15.0.0'
And the following library dependencies:
App's build.gradle:
implementation "com.google.firebase:firebase-firestore:$firebase_version"
It looks like these versions were released on the 10th of April, however when compiling my app with the updated libraries, it fails to run with the following error:
error: cannot access zzbgl
class file for com.google.android.gms.internal.zzbgl not found
When checking all my library versions, my build.gradle has the following error:
not sure if anyone has experienced this by any chance since the latest updates? Before updating from 12.0.1, everything was working.
Thanks
Simply override (add to your gradle file) the conflicting library, updating the version to match the ones you already have in your gradle file. Somewhere in your dependencies someone is using an older version of this library and it's crashing with your version:
implementation "com.google.android.gms:play-services-auth:$firebase_version"

Android Studio 3 and Gradle 4.3 -> Library modules no longer process local JARs -> how to handle this?

Upgrade to Android Studio 3.0.0 mentions this, and doesn't elaborate on how to handle it:
Library modules no longer process local JARs. This is to speed up incremental builds that are caused by changes to a library module's code.
So I have a project with a library project in it. In my library project's build.gradle file I have this:
compile files('libs/com.somelib.somepackage.jar')
I changed compile to implementation and when I tried to run my app, all my classes that tried to access the import com.somelib.somepackage.SomeClass import statement threw an error that this package didnt exist.
I changed back to compile and I was able to build and run my app.
I want to comply to the new rules since compile is deprecated and will be removed with the next Gradle release, so how do I go about doing that?
If you are trying to access classes from the .jar that is included in the library project from the app project, you will have to use api instead of implementation otherwise the classes will only be accessible in the library project:
implementation files('libs/com.somelib.somepackage.jar')
should be
api files('libs/com.somelib/somepackage.jar')
As said by the documentation:
... When a module includes an api dependency, it's letting Gradle know
that the module wants to transitively export that dependency to other
modules, so that it's available to them at both runtime and compile
time ...
Reference:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations

Categories

Resources