Add Flutter functionality as a dependency in android library - android

Why do I need this?
I'm creating a Flutter package (for android) and I want to create the MethodCallHandler's code in a separate AAR file that can be added in my plugin's code
So I'll have:
pluginNativeCode/android/MyPlugin.kt
import ir.malv.android.flutter.MySeparateMethodCallHandler
class MyFlutterPlugin: FlutterPlugin {
override fun onAttachedToEngine(...) {
// Here's the source of the problem
channel.setMethodCallHandler(MySeparateMethodCallHandler())
}
}
The MySeparateMethodCallHandler is not in the plugin/android part and it's imported as a gradle dependency
However in that library I don't have access to flutter's codes like MethodCallHandler class.
Similar behaviour in unity and react native
In react-native there is com.facebook.react:react-native:+ that can be added as compileOnly to get the needed react-native codes
In unity we have unity.jar file which contains unity native codes and can be added as compileOnly as well to provide engine's native APIs
What do we have in Flutter for this?
Is there a dependency that I can include as compileOnly and use it to get the needed classes and create my aar file in a separate project?

Here's a solution:
Add this remote to repositories of build.gradle
allprojects {
repositories {
// .. rest of remote urls
maven { url("http://download.flutter.io/")}
}
}
Add this dependency to module's build.gradle
dependencies {
compileOnly("io.flutter:flutter_embedding_debug:1.0.0-0fdb562ac8068ce3dda6b69aca3f355f4d1d2718")
// rest
}
Notice the compileOnly. This will avoid the class duplication. Plugin just requires it. The flutter app will provide it itself.
What is 0fdb562ac8068ce3dda6b69aca3f355f4d1d2718? It's the engine version that is good to match the version you are developing the plugin with.
in your code you will have access to needed classes such as MethodChannel, MethodChannel.Result and ...
Bonus: How do I know what engine I'm making the plugin with?
I think it should not matter if you just need a class like MethodChannel to invoke a method, but here's a way to get it using Android Studio
Open the project/example/android of your plugin using Android studio (that has Flutter plugin activated)
run gradle plugin_name:dependencies task and look for flutter_embedding_ phrase.

Related

Override java dependency in react native library

I need to use a react native library called #ihealth/ihealthlibrary-react-native
I added it to my project, but on build I get an error Could not find no.nordicsemi.android:dfu:1.6.1
This package version seems to be no longer available, so I would like to override the version in my react native app.
I added the following in MyProject/android/app/build.gradle
dependencies {
implementation("com.ihealth.ihealthlibrary:iHealthLibrary:1.5.1") {
exclude group:'no.nordicsemi.android:dfu:1.6.1'
}
implementation("no.nordicsemi.android:dfu:1.8.0")
}
I then get this error: Could not find com.ihealth.ihealthlibrary:iHealthLibrary:1.5.1.
Basically I can't figure out what's the correct package name and version to put in my config.
Where can I find this ?
This lib is really outdated. Go into the libraries build.gradle file and replace this implementation 'no.nordicsemi.android:dfu:1.6.1'
with: implementation 'no.nordicsemi.android:dfu:1.8.0'
then create a patch: yarn patch-package #ihealth/ihealthlibrary-react-native

How to get rid of Kotlin DSL warnings

I have just started learning KotlinDSL recently.
And in Android added buildSrc. In this folder I have module plugins: AppModulePlugin, CommonModulePlugin, FeatureModulePlugin. All of this compiles and the application installs correctly, everything is fine. but in these files a warning is displayed:
Cannot access 'com.android.build.gradle.internal.dsl.Lockable'
which is a supertype of 'com.android.build.gradle.BaseExtension'.
Check your module classpath for missing or conflicting dependencies
Please tell me how to get rid of these warnings?
IntelliJ has had this issue for months unfortunately - it reports correct code as invalid. See KTIJ-3769. I believe it's dependent on ticket KTIJ-19669.
As a workaround you can either replace the helper methods that the plugins introduce with 'plain' Gradle Kotlin
// androidExtension { }
project.extensions.configure<BaseExtension> {
}
Or only create buildSrc plugins in Kotlin (*.kt files), not Kotlin Script (*.kts files). This requires using the java-gradle-plugin and defining the plugins in buildSrc/build.gradle.kts.

How to: Target a local android dependency

I'm trying to modify a flutter package but the part that needs changing is in an Android dependency being used by that package.
The flutter package is epub_viewer (https://pub.dev/packages/epub_viewer) which implements a modified version of FolioReader (https://github.com/JideGuru/Folioreader-Android) ...
I've been trying to follow examples on here but it doesn't make sense and keeps telling me it can't find the package https://developer.android.com/studio/build/dependencies
My file structure is like this:
../users/documents/epub_viewer
../users/documents/folioreader
and I need to change this dependency in epub_viewer's app build.gradle
dependencies {
implementation "com.jideguru:folioreader:0.6.2"
...
}

how to conditionally choose module dependency or source code in the app?

In an android app, it has a lib module (lib-module) which could be used by other app or lib. And in this app it uses this lib module but directly with code:
include ':app', ':lib-module'
project(':lib-module').projectDir = new File(settingsDir, 'libModule')
there is a case that a different lib/sdk is using this lib-module (call it third-rd-lib here), so the third-rd-lib has dependency to a version of published lib-module.
and this app also has dependency on that third-rd-lib. now in the app the code of lib-module may not be in sync if the third-rd-lib is using a released version of lib-module which is not same as the code currently in the app's lib-module.
question, is there a way in the app to configure the build so that it could be build either using the source code of the lib-module, or using dependency on the version of released lib-module?
update the question
actually I am confused. when there are both the
include ':app', ':lib-module'
project(':lib-module').projectDir = new File(settingsDir, 'libModule')
and
implementation "com.android.lib:third-rd-lib:x.y.z"
// which has dependency on the lib-module:1.2.0
then when running the app, will the code from the dependency lib-module:1.2.0 get loaded or the source code of the lib-module in the app is loaded?
the doc and this one helped, and using this works
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module('com.android.app:lib-module') with project(':lib-module')
}

How do I use another library in my app?

The title is a little misleading but I honestly don't know how to word it in any other way. This is my project structure:
I want to use the highlighted (fasteranimationscontainer-master) in my current app that I'm working on. I imported it by putting the jar file in my library folder then adding it to my library through Android Studio. But I try to create an object of that imported library, it doesn't show up/import.
I'm still learning how to use android studio so any help would be much appreciated!
1. Library with only the Source Code
To include a library with only the source code (like FasterAnimationsContainer) to your project, you only need to import it as a module from File -> New-> Import Module in Android Studio:
There should be a dialog for the project folder, enter the path where your library reside.
Then you need to add the module to your app build.gradle as a dependency:
android {
...
dependencies {
...
compile project(':fasteranimationscontainer')
...
}
}
Now you can use it in your project.
2. Library from Maven or JCenter
To add dependencies to your project where the library has uploaded to maven or jcenter, you need to modify your app build.gradle file and add extra lines configuring the packages you require. For example, for certain Google or Android, dependencies look like:
android {
...
dependencies {
// Google Play Services
compile 'com.google.android.gms:play-services:6.5.+'
// Support Libraries
compile 'com.android.support:support-v4:22.2.1'
}
}
Try to always read the library README first.
Suggestion:
If you still learning using Android Studio, you can read Using Android Studio. Then read Getting Started with Gradle, because Android Studio is tightly related with Gradle.

Categories

Resources