Add plain Android library to a Flutter plugin - flutter

This might be a duplicate of this question. But it has no answer and I will give some details here.
I have created a module with File->New Module->Android Library inside my Flutter plugin's android project. And now I have a structure like below:
|-my_plugin
|-android
|-settings.gradle
|-build.gradle
|-mylibrary
|-build.gradle
/android/settings.gradle:
rootProject.name = 'my_plugin'
include ':mylibrary'
/android/build.gradle:
...
dependencies {
implementation project(':mylibrary')
}
When I build example plugin project (which is automatically created by Flutter CLI) with flutter build apk or flutter run, I get this error:
Project with path ':mylibrary' could not be found in project ':my_plugin'.
Any suggestions?

For those who are still looking for a solution. You can try this.
Instead of putting the below code in the plugin setting.gradle, place it in example/android/settings.gradle
include ':mylibrary'

Related

how to bundle flutter ffi plugin

I am writing uber h3 plugin for flutter. I have working source code but I have problems with creating package more precisely I have problems to bundle libh3.so
Quick overview:
there is uber h3 c source code which is downloaded by. download_deps.sh
then android/build.gradle has build commands
externalNativeBuild {
cmake {
path "../ios/CMakeLists.txt"
}
}
then example/lib/main.dart has some initialization code.
initializeH3((String name) => Platform.isAndroid
? DynamicLibrary.open("lib${name}.so")
: DynamicLibrary.process());
but when I try to build it library file can not be found.
-I tried different locations.
It seems that library native .so is not bundled into application
But I don't konw why
According to tutorial https://flutter.dev/docs/development/platform-integration/c-interop
it should be bundled.
Here is the source code https://github.com/fmatuszewski/h3
I have managed to get this running, the pubspec.yaml was incorrectly formatted and required:
flutter:
plugin:
platforms:
android:
package: com.example.h3
pluginClass: H3Plugin
ios:
pluginClass: H3Plugin
Adding in at the end.
In doing this the package was properly attached.
libh3.so needs to be added into android/src/main/jniLibs to naturally be attached however I also moved the code:
final DynamicLibrary h3 = Platform.isAndroid
? DynamicLibrary.open("libh3.so")
: DynamicLibrary.process();
Back into h3.dart and removed main.dart to get this to run.
I think these were the main issues to get the code running. I had issues working out what was wrong as I am new to plugins - so it was a long process to sit and work out all the ins and outs. In doing this I ended up reworking all of the code to understand what was going on. If none of these points work or you wants eyes on the working code let me know and I can upload it to Github.

Flutter NFC reader plugin does not work with error inside plugin

I'm trying to make a flutter version of my old app , it needs NFC .
First I create a default project for test run and it works fine.
I tried to use flutter-nfc-reader and install it following "Installation" in that default project.
And here comes the problem:
After I edit "pubspec.yaml" and do "packeges get" , it automatically edit "GeneratedPluginRegistrant" under "myapp/android/app/src/main/java/io.flutter.plugins".
added
import it.matteocrippa.flutternfcreader.FlutterNfcReaderPlugin;
and
FlutterNfcReaderPlugin.registerWith(registry.registrarFor("it.matteocrippa.flutternfcreader.FlutterNfcReaderPlugin"));
But the import can't find that package "it.matteocrippa.flutternfcreader" which is inside the plugin.
I tried just copy that package but that file will be full of error when it is inside my project.
I have no idea how to fix it....
As Github reads:
last step import to the project:
import 'package:flutter_nfc_reader/flutter_nfc_reader.dart';

Target URI doesn't exist, when trying to import a package in flutter

I have stumbled upon a problem in importing the package in Flutter, I tried to solve this by running flutter packages get and also shutting down the project in Android studio and reopening it.
import 'package:task_02_category_widget/category.dart';
Here is the line above, and the error I'm running into when I run it gives the following error in the console.
Your application could not be compiled, because its dependencies could
not be established.
The following Dart file:
/Users/username/Documents/flutter_rectangle_2/lib/main.dart
...refers, in an import, to the following library:
package:task_02_category_widget/category.dart
That library is in a package that is not known. Maybe you forgot to
mention it in your pubspec.yaml file?
If task_02_category_widget/category.dart is part of an old project you are reusing you should put it in a folder in your flutter application and include it like "../ folder /task_02_category_widget/category.dart ". If it is part of github repository you have copy pasted from, just copy the file and use the step above. Most probably you are looking for that . In any other case check here to find the source code.
You should have in your project at a file called pubspec.yaml a definition like this:
name: my_app
dependencies:
task_02_category_widget:
Let’s say that your package is laid out as follows:
task_02_category_widget/
lib/
category.dart
Then, you can import it:
import 'package:task_02_category_widget/category.dart';
More information:
https://www.dartlang.org/tools/pub/get-started
https://www.dartlang.org/guides/libraries/create-library-packages

Unknown globals when installing ZXing Scanner

I have a Xamarin.Forms project which needs a QR code scanner. I found ZXing Scanner which seems to be a well established library for such purposes.
I installed it in the corresponding android project which worked without errors. When I wanted to build the app, Resource.Designer.cs was adjusted with the following lines:
global::ZXing.Mobile.Resource.Id.contentFrame = global::my.project.Droid.Resource.Id.contentFrame;
global::ZXing.Mobile.Resource.Layout.zxingscanneractivitylayout = global::my.project.Droid.Resource.Layout.zxingscanneractivitylayout;
global::ZXing.Mobile.Resource.Layout.zxingscannerfragmentlayout = global::my.project.Droid.Resource.Layout.zxingscannerfragmentlayout;
The problem is that I as well get the following errors:
'my.project.Droid.Resource.Id' does not contain a definition for 'contentFrame'
'my.project.Droid.Resource.Layout' does not contain a definition for 'zxingscanneractivitylayout'
'my.project.Droid.Resource.Layout' does not contain a definition for 'zxingscannerfragmentlayout'
I installed ZXing using NuGet but I as well tried to add the dlls manually. I get the same errors. Can anyone help me how to fix this?
Thank you in advance.
It's quite possible that your Resource.Designer.cs is not updating correctly:
Clean Project Build
Remove ZXing package.
Clean Project Build
Add ZXing package
Clean and Build Project once again
If it won't help:
https://kb.xamarin.com/customer/portal/articles/1638018-my-android-resource-designer-cs-file-will-not-update or https://forums.xamarin.com/discussion/13339/resource-designer-cs-not-regenerated/

Create a package in dart

How do I create a package in the new Dart Editor?
There is no "Add Pub support" checkbox?
Also how to create "packages" with the new editor?
Is a tutorial out there that describes the process with the new Editor?
To create a package named mypackage.
For Dart package:
dart create --template=package-simple mypackage
For Flutter package:
flutter create --template=package mypackage
From the Dart/Flutter Documentation:
Step 1: Create the package
To create a Flutter package, use the --template=package flag with flutter create:
flutter create --template=package hello
This creates a package project in the hello folder with the following content:
LICENSE
A (mostly) empty license text file.
test/hello_test.dart
The unit tests for the package.
hello.iml
A configuration file used by the IntelliJ IDEs.
.gitignore
A hidden file that tells Git which files or folders to ignore in a project.
.metadata
A hidden file used by IDEs to track the properties of the Flutter project.
pubspec.yaml
A yaml file containing metadata that specifies the package’s dependencies. Used by the pub tool.
README.md
A starter markdown file that briefly describes the package’s purpose.
lib/hello.dart
A starter app containing Dart code for the package.
.idea/modules.xml, .idea/modules.xml, .idea/workspace.xml**
A hidden folder containing configuration files for the IntelliJ IDEs.
CHANGELOG.md
A (mostly) empty markdown file for tracking version changes to the package.
There's no such possibilty in the Dart Editor for now. To create a package follow these steps :
create an New Application mylib without sample content
add a pubspec.yaml file
add a lib folder
create a mylib.dart containing the code you want to package
See the Package layout conventions for more informations.
You can create a dart project following the flutter way that allow you to auto generate the structure and the hierarchy of the package.
Follow below steps to create a package in DART:
Step 1: Create the package
$ flutter create --template=package hello
Step 2: Implement the package
For pure Dart packages, simply add the functionality inside the main lib/.dart file, or in several files in the lib directory.
To test the package, add unit tests in a test directory.
For additional details on how to organize the package contents, see the Dart library package documentation:
https://flutter.dev/docs/development/packages-and-plugins/developing-packages
Any dart app is a package. To create a new Dart app use:
dart create my_package