Reputation: 99
I have one native sdk i am integrating that sdk in flutter but it gives error. is there any other way to integrate third party sdk in flutter
Upvotes: 3
Views: 12678
Reputation: 3285
To integrate third party native SDKs you have follow Platform Channel approach.
For the step by step guide on How to integrate third party SDK, I've written this article. https://www.solutelabs.com/blog/integrating-third-party-native-sdks-in-flutter
Upvotes: 7
Reputation: 51
After spending some research and analysis. I was successfully able to integrate the sdk(.aar) file into the flutter application. The third-party SDK is one of the products. The SDK is written in pure JAVA.
In flutter we have to use the Methodchannel to interact flutter app with the native modules or extend the functionality.
Below are the steps to integrate the SDK.
Step: 1
The documentation will say that to create a flutter plugin wrapper. But i have done very basic and straight forward integration.
First open your android folder(root level) in android studio. The sample path project>android
Put your .aar file in the libs folder(Create if it not there). Sample path: project>android>app>libs
Step 2:
Open your build.gradle file in the app folder and add below lines
rootProject.allprojects{
...
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
...
// add your sdk dependencies too
implementation files("libs/app-release.aar")
}
After that, sync the project.
Step: 3
Now open your flutter project in a new window. Edit your MainActivity.kt file
import com.mypackage.MySDKStartActivity;
private val CHANNEL = "yourMethodChannelName"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "methodName") {
val arg1 : String? = call.argument("arg1");
val arg2 : String? = call.argument("arg2");
startNewActivity(arg1, arg1); // Method to start the SDK
}
}
}
private fun startNewActivity(arg1: String?, arg2: String?) {
val intent = Intent(context, MySDKStartActivity::class.java)
intent.putExtra("param1",arg1); // send some data to the sdk if it required
intent.putExtra("param2",arg2);
startActivity(intent);
}
Step: 4
Edit your dart file. For example my flutter project main.dart file which is there in project>lib>main.dart
static const platform = const MethodChannel('smsAggregationChannel');
@override
Widget build(BuildContext context) {
…
ElevatedButton(
child: Text('Start Aggregate'),
onPressed: _startAggregation,
)
}
Future<void> _startAggregation() async {
try {
final String arg1 = "<arg1>";
final String arg2 = "<arg2>";
await platform.invokeMethod('startAggregation', {// start the aggregation with args
'arg1': arg1,
'arg2': agr2
});
} on PlatformException catch (e) {
print("Failed to start SDK");
}
}
After all the changes are done. Run your flutter project in the android studio or intelliji. Start the application with main.dart You can invok the sdk on button press.
If any issue comes try clear(flutter clean
) the flutter and re-run the app.
My flutter version is 1.22.6 and Dart 2.10.5
Upvotes: 4