Why Android App-Indexing feature not works in Unity project when import it as a plugin.?
Hi, Currently our team is trying to enable App indexing and Autocomplete features in my Android game , for that we created an Sample Android App and tried to implement the App indexing as well as deep-linking .
In Android app side it worked properly. we tested the deep linking with following link
https://developers.google.com/app-indexing/android/test#test-autocompletions
And we tested Autocomplete in App by following link
https://codelabs.developers.google.com/codelabs/app-indexing/#0
so Both works correctly on App side .. And i would like to specify that we have created an Android studio project with the same Package ID as our Unity Project .. Followed all the steps mentioned in Unity's Android Plugin Manual.
For More details i am Including My Java source code...
public class MainActivity extends UnityPlayerActivity { private static MainActivity sInstance; private GoogleApiClient mClient; private String mUrl; private String mTitle; private String mDescription;
public static MainActivity GetInstance()
{
if(sInstance == null)
{
sInstance = new MainActivity();
}
return sInstance;
}
public int initFromUnity ()
{
return 15;
}
public void OnCreateCall(Activity currentActivity)
{
mUrl ="android-app://com.MyGames.firstgame/https/play.google.com/store/apps/";
mTitle = "Master Chef";
mDescription = "Become a Master Chef and run your own restaurant";
mClient = new GoogleApiClient.Builder(currentActivity).addApi(AppIndex.API).build();
}
public Action getAction()
{
Thing object = new Thing.Builder()
.setName(mTitle)
.setDescription(mDescription)
.setUrl(Uri.parse(mUrl))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
public void OnStartCall()
{
mClient.connect();
PendingResult<Status> result = AppIndex.AppIndexApi.start(mClient,getAction());
result.setResultCallback(new ResultCallback<Status>()
{
@Override
public void onResult(@NonNull Status status)
{
if(status.isSuccess())
{
Log.d(MainActivity.class.getName(),"App Indexing API Recorded page"+ mTitle +"view successfully.");
}
else
{
Log.e(MainActivity.class.getName(),"App Indexing API: There was an error recording the page view."+status.toString());
}
}
});
}
public void OnStopCall()
{
AppIndex.AppIndexApi.end(mClient, getAction());
PendingResult<Status> result = AppIndex.AppIndexApi.end(mClient,getAction());
mClient.disconnect();
result.setResultCallback(new ResultCallback<Status>()
{
@Override
public void onResult(@NonNull Status status)
{
if (status.isSuccess())
{
Log.d(MainActivity.class.getName(), "App Indexing API Recorded page " + mTitle + " view end successfully.");
}
else
{
Log.e(MainActivity.class.getName(), "App Indexing API: There was an error recording the page view." + status.toString());
}
}
});
}
=========================================================== public class AndroidEnableAppIndexing : MonoBehaviour { AndroidJavaObject mAppIndexingActivity; AndroidJavaObject mCurrentActivity; AndroidJavaClass appIndexingClass;
void Awake ()
{
Debug.Log ("In Awake");
#if !UNITY_EDITOR
AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
mCurrentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
appIndexingClass = new AndroidJavaClass ("com.MyGames.firstgame.MainActivity");
mAppIndexingActivity = appIndexingClass.CallStatic<AndroidJavaObject> ("GetInstance");
mAppIndexingActivity.Call ("OnCreateCall",mCurrentActivity);
#endif
}
void Start()
{
Debug.Log ("In Start");
#if !UNITY_EDITOR
mAppIndexingActivity.Call ("OnStartCall");
#endif
}
void OnApplicationQuit()
{
Debug.Log("Application is going to quit.");
#if !UNITY_EDITOR
mAppIndexingActivity.Call ("OnStopCall");
#endif
}
}
==================================================== In Unity project deep linking is working properly ,, but when i include the Jar File and try to access the methods from Jar file it shows the error No such method found as follows..
One more observation is when i Comment the methods OnCreateCall(), OnStartCall,OnStopCall() ,which include google client API's "initFromUnity" method is called successfully .. do i need to add any class in unity project related to AppIndexing in a way that it should connect to the google client and work properly in Game.
"Only after including the Google client related API's is shows AndroidJavaException no such static method .. When i Comment them i can call all the methods from the plugins properly."
Answer by ricardo_arango · May 19, 2016 at 11:12 PM
What the error is saying is that the static method MainActivity doesn't exist. You need to double check your code to see if there is an error. I just did a test like this:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
AndroidJavaObject mAppIndexingActivity;
void Start () {
Debug.Log ("Start");
AndroidJavaClass appIndexingClass = new AndroidJavaClass ("com.unity3d.ProductName.AppIndexingActivity");
Debug.Log ("appIndexingClass: " + appIndexingClass);
mAppIndexingActivity = appIndexingClass.CallStatic<AndroidJavaObject> ("GetInstance");
Debug.Log ("mAppIndexingActivity: " + mAppIndexingActivity);
}
}
And:
package com.unity3d.ProductName;
public class AppIndexingActivity {
private static AppIndexingActivity sInstance;
public static AppIndexingActivity GetInstance()
{
if(sInstance == null)
{
sInstance = new AppIndexingActivity();
}
return sInstance;
}
}
The method was found correctly.
Hi,
i am also trying to work on this and want to reach at best solution..Could you please let me know is there any plugin for this??? or any video so that i can find out the whole scenario of implementation..
thanks
I tried this code but I want to open url. The following code does not work because the class is static.
private Intent myIntent;
Uri uri = Uri.parse("http://www.google.com"); myIntent= new Intent(Intent.ACTION_VIEW, uri); startActivity(myIntent);
Could you help?