9

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.

1

5 Answers 5

30

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answer. But come on Xamarin... It's 2019. Stop using Windows 95 APIs to access files. NTFS does not have 256 limit.
Thanks Long Path was exactly what it was. Just a simple demo app as well.
Thanks this also solved my issue. Its very annoying because I have a highly ordered system and I want to keep this order. I also had this with Qt. Paths with spaces were not accepted. Very MSDOS'e
If you prefer to keep your folder structure, you can mount your solution folder to a drive using subst command. Open your Visual Studio solution from the mounted drive instead and you are set.
it was the long path for me. have an upvote. The error message was scary and somewhat misslieading
|
6

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.

Comments

1

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);
}

Comments

0

I ran into this as well, seems like the Xamarin tutorials should warn people about this.

Comments

0

I got the same exception, when I built first a mobile application. Error code was XA4209 and suggested to: The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

After I changed the characters of the path, everithing worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.