Hello, in this video, we are going to look at how to call Java co native Android Java code. And essentially, you know, get some sort of response back from it. So this is great. If you want to do something Java, maybe it hasn't made its way to flutter yet and to Dart code because you know it nice, still newer than Java. Or maybe you already have libraries some code. This is a great way of integrating pre existing code without having to, you know, convert it over.
So the way you do this is actually pretty simple. We want to do in your dot file, and then we close this data set can in your Docker file, you do import package, locker, and you want to import the service you've got dot and this will allow us to essentially create a service a connection between the doll file and the Java file. The next step for us is to go down to our home Page what we want to do is create the the actual platform connection so static port is platform equals cons and it's a method channel This requires the know where the haters came from. This requires a name and for this name I'm going to put chrome dot Mater dot epic for slash epically really and if you want but i think is good to keep it in the sort of package name format comm dot flutter dot epic and I put epic in there just in case you want to interact with a nother channel as well so you could have you know definitely could have you know sub channel but you know the format is totally up to you.
Next, what we are going to do is create a method and let's avoid Don't want to call it 20 Christopher Princeton asynchronous method. Inside of here, we have a string, which is going to be the value that is returned, we're gonna have a try catch block. And in here we'll say run. platform, dot invoke method. And the method we are invoking isn't printing. We don't need the curly braces.
And we'll handle it all of this on the job. So I put a catch here. And we can just print out the error. If there's any errors. There shouldn't be but in case there is, and we're going to print out the value. Well, this is going to be triggered when I press while I show you see when I load up the button that says reverse in there for our previous tutorials, don't ask me why it's got this is for my animation, though he's told you anyway.
And so we can just say printing So now we're all set up on the DOD side of things, that's really all you need to do. And this value that we get back, you don't have to print it out, you could assign it to some other variable here, you producing calculations, that's totally up to you. But I'm just going to print it out into the console just to keep things simple. Next, if you go to Android, this is where your Angular project is. And if you notice that an iOS will cover all of that in a separate video, you will go to App ignore the red line source main Java and comm dot example dot flutter app. That is my package, go to the main activity, and in here is the default is the D is the default Java code.
So this is where we will be doing all of the magic to be able to run our Java code. So what we want to do here is why is it that final string, and this is only for channel. This is going to be equal to make sure you get this correctly This is going to be equal to exactly this. Copy that. Paste it also is one let you know if you have a little thing saying here, set up SDK, just click it. I've already done it.
A little pop up will open. You'll have various stuff like Android Studio Android does some stuff, click the JDK and you'll be all good to go. If you have any questions, feel free to pop and now so yeah go make sure this channel exactly the thing. And now what we can do is say new method channel. Yet flutter view based on the channel Now, we're going to set method call handler in here, let's say new method call handler dot method call handler. So, this is basically what it looks like all the source code was was a new method channel dot method called handler.
And this method will be called whenever something is sent to this, but because we have done invoke method, we want to make sure that if you have multiple methods, we can handle that because otherwise, loads of them will just be you know get triggered based on this little package name. In here, we can say this method called dot method is equal to so dot equals and then put what you put here, this exact thing where you put invoke method, so if you put open curly brackets, yeah, makes Put them down. If you didn't put it here, make sure you don't put them here. It literally is just a string. And now, to return the value back, you just do result, dot success. And what you put in here is what gets returned back to Amazon, say, buy from Java.
Now we are ready to run it. So if we run it. Now, the thing to bear in mind, when you are doing modifications to your native Android side, you can't use the hot reload or the hot restart, you will have to you know, shut down the application and rebuild either using the run method or the debug method. So that way, it recompile the Android side and puts that into the APK. So that's just something to bear in mind. So it will launch up any moment now and we'll have a button that says reverse again, ignore what it says.
And if I click it we get hi from Java and We have a Nope, we do not have hi from Java anywhere here is right here. And if you had multiple methods, so if we are multiple invoke methods with different method names, you could have an elsif. And then nobody else is saying is the method called equal to this one or that one. That's really all there is to it. Obviously, you can make it more complex than this. You can use all your job or import or your or when the complexities with the data structure, create your own classes, your own Java files, create instances of all of that stuff.
Yeah, you can, you can do that. You can do all of that stuff. And you can import your existing libraries as well. But this is the stepping stone. This is the foundation of connecting Dart to Java. In the next video, I'm going to show you how to return data back not just call a method, but I'm sorry, how to send data and the parameter to Java and then We'll return something back as well.
So that's really if you have any questions, feel free to drop me a message. And as usual, I look forward to seeing you in the next video.