xamarin android build error: “Failed to create JavaTypeInfo for class”

淺唱寂寞╮ 提交于 2020-12-03 02:32:17

问题


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:


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



回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/54361545/xamarin-android-build-error-failed-to-create-javatypeinfo-for-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!