Call java plugin method in unity

Hi I’m trying to use login plugin for Unity. I put java files in Unity and customized AndroidManifest and build.gradle.

Build is success, but there’s a problem. I want that when I click the button, call “CallAndroid” method in java. BUT…Nothing happens. I export this and open in Android Studio, but nothing message in Logcat.
I’m a beginner so I don’t know what should I do. :frowning: Please Help me!!!

This is Unity code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Login : MonoBehaviour
{
    AndroidJavaClass unityClass;
    AndroidJavaObject unityActivity;
    AndroidJavaObject unityContext;
    AndroidJavaClass plugin;

    private void Awake()
    {
        //Get Unity Context
        unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

        plugin = new AndroidJavaClass("com.company.example.javaPlugin");
    }

    public void NavLogin()
    {
        plugin.Call("CallAndroid", unityContext);
    }
}

And this is Java code

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

public class javaPlugin extends UnityPlayerActivity {

private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d("#####", "onCreate()");
super.onCreate(savedInstanceState);

}

public void CallAndroid(Context context)
{
this.mContext = context;
Log.d("#####", "CallAndroid()");

Intent loginIntent = new Intent(this, naverLogin.class);
context.startActivity(loginIntent);
     
}


@Override
public void onBackPressed() {
Log.d("#####", "onBackPressed()");
//super.onBackPressed();
}
}

In order to call CallAndroid() you need an object, not class.
If this class is used as activity in your application, then the unityActivity variable will have an instance of your class, so you can call that method on it directly. You can also remove the call to getApplicationContext from C# code and call it directly in Java code.

Thank you for your answer. I solved my problem by referring to your answer!!!

1 Like

Hello qkrwlgp7827 , if you could please post your updated code for my reference, it would be appreciated very much.
Thanks