Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xamarin android build error: "Failed to create JavaTypeInfo for class"

On build, the compiler is throwing the following error:

Error Failed to create JavaTypeInfo for class: App.Droid.Controls.WebViewJavaScriptInterface due to System.NullReferenceException: Object reference not set to an instance of an object. at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.Signature..ctor(String name, String signature, String connector, String managedParameters, String outerType, String superCall) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.Signature..ctor(MethodDefinition method, ExportAttribute export) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.AddMethod(MethodDefinition registeredMethod, MethodDefinition implementedMethod) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator..ctor(TypeDefinition type, String outerType, Action2 log) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator..ctor(TypeDefinition type, Action2 log) at Xamarin.Android.Tasks.Generator.GenerateJavaSource(TaskLoggingHelper log, TypeDefinition t, String outputPath, String applicationJavaClass, Boolean useSharedRuntime, Boolean generateOnCreateOverrides, Boolean hasExportReference)

I have created a custom renderer for the webview, where I am trying to inject JavaScriptInterface. I have a solution with different Projects, which might be reason for the above issue, or maybe not.

public class WebviewRendererEX : WebViewRenderer
{
    Context _context;

    public WebviewRendererEX(Context context) : base(context)
    {
        _context = context;
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            CookieManager cm = CookieManager.Instance;
            cm.SetAcceptCookie(true);
            cm.SetAcceptThirdPartyCookies(Control, true);
            Control.Settings.JavaScriptEnabled = true;
            Control.Settings.DomStorageEnabled = true;
            Control.AddJavascriptInterface(this, "Android");
            Device.BeginInvokeOnMainThread(() =>
            {
                Control.EvaluateJavascript("function someNavigate(dict){Android.navigateTo(dict);}", null);
            });    
        }
    }
}

public class WebViewJavaScriptInterface : Java.Lang.Object
{

    private Context context;
    public WebViewJavaScriptInterface(Context context)
    {
        this.context = context;
    }
    [Java.Interop.Export("navigateTo")]
    [JavascriptInterface]
    public void NavigateTo(Dictionary<string, object> dict)
    {
        Console.WriteLine(dict);
    }
}

I expected that the App should run without any complietime issue, And once webview loaded javascriptInterface should work.

like image 398
Srikar Avatar asked Jan 25 '19 08:01

Srikar


3 Answers

Based on your builder error, the ExportAttribute is used to instruct the “Java code generator to export a Java method that becomes an Android Callable Wrapper (ACW)” and Dictionary<string, object> is not a Java object (duh) and the Java code generator has no idea how to handle it.

[Java.Interop.Export("navigateTo")]
[JavascriptInterface]
public void NavigateTo(Dictionary<string, object> dict)
{
   Console.WriteLine(dict);
}

So the simple fix to this problem was to switch the parameter type from Dictionary<string, object> to Java.Lang.Object. Now the Java code generator can properly generate an ACW and the compilation succeeds.

[Java.Interop.Export("navigateTo")]
[JavascriptInterface]
public void NavigateTo(Java.Lang.String dict)
{
    Console.WriteLine(dict);
}
like image 45
Leon Lu - MSFT Avatar answered Nov 16 '22 21:11

Leon Lu - MSFT


Failed to create JavaTypeInfo for class - build error in Visual Studio 2019, Xamarin Forms template

I got a similar error: (the details are different though, see below)

Failed to create JavaTypeInfo for class:
 Android.Support.V4.View.Accessibility.AccessibilityManagerCompat/IAccessibilityStateChangeListenerImplementor 
due to System.IO.DirectoryNotFoundException: 
Could not find a part of the path '...\obj\Debug\81\android\src\mono\android\support\v4\view\accessibility\AccessibilityManagerCompat_AccessibilityStateChangeListenerImplementor.java'.

This is on Visual Studio 2019, after upgrading to Xamarin Forms 4.x, though I think the upgrade doesn't matter here.

This issue has been reported here on the visual studio developer community - https://developercommunity.visualstudio.com/content/problem/521034/failed-to-create-javatypeinfo.html

For me, it turned out to be a 'Long Path' issue, as reported by a few other users in the dev community (refer to the above link). Once I moved my solution a couple of directories up, to reduce the overall path length, it started working fine.

like image 130
Sumant Avatar answered Nov 16 '22 19:11

Sumant


So this is definitely related to the MAX_PATH problem on Windows. This is a long dicussion about this here . Hopefully according to microssoft support team they are planing solve this issue and removing MAX_PATH as limit; but at the moment I use last edition of visual studio (Microsoft Visual Studio 2019 Version 16.2.3) I still get the same error.

Accoridng Microsfot Support team :

Some background on why those .java names are so long. Java requires that the class name must match the filename (Unlikc C#). And because some of these android types have really long names we are in a position where we cannot change them 😕 For now moving the project closer to the root of the drive is the easiest solution.

I solve the problem by moving my project to up directories and renaming long folder names.

like image 6
nzrytmn Avatar answered Nov 16 '22 19:11

nzrytmn