Xamarin开发中我所遇到Error错误的解决办法
Xamarin的坑还是不少的,有些问题可能会一而再,再而三地重复出现,为了避免下一次遇到同一个问题重复的查找,我将在开发中遭遇到的问题汇总在这里,既方便我自己查阅,也提供给别人做参考;以下给出的解决方案中一部分是解决思路,一部分是确实成功地解决了问题的方案(我将用斜体标出)。
1.Xamarin.iOS:报Failed to inherit CoreMedia permissions from NNNN错误;
解决方案: 1.检查iOS Bundle signing的设置是否正确; 2.custom Entitlements 是否有值,且为Entitlements.plist; 3.测试设备中是否存在多个使用同一个app group的app; 4.证书中是否包含了对app group的正确申请;
2.Xamarin.iOS:报The “XamlCTask” task failed unexpectedly错误;
解决方案:
- clean或者rebuilt项目;
- 重启IDE;
- 检查PCL的配置文件是否一致;
3.Xamarin.Android:打包安装包时报“Error MSB4018: The “ResolveLibraryProjectImports” task failed unexpectedly.”错误
解决方案: 先rebuild All,再进行打包;
4.遇到# Error inflating class android.support.v7.widget.FitWindowsFrameLayout问题
原因:链接器移除了对FitWindowsFrameLayout的打包 解决方案: 欺骗链接器,让其认为我们使用了这个库; 在代码中加入如下代码:
......
static bool falseflag = false;
......
if (falseflag) {
var ignore = new FitWindowsFrameLayout(Application.Context);
}//虽然这段代码不会真正地被调用,但我在实践中发现这段代码放在Activity的初始化代码会好一些。
5.遇到 # You need to use a Theme.AppCompat theme (or descendant) with this activity 问题
这个问题好像是xamarin的坑,如果你修改了layout中的xml文件,可能再次build的时候就会报这个问题。 解决方案: 1.clean后,build; 2.删除bin和obj目录,rebuild;
6.Xamarin.Android 发布release版用proguard混淆时出现下述问题:
- 出现java.exe exited with code 1错误; 解决方案: 更新你的proguard,下载最新版本:下载地址 下载之后,解压缩,用里面的内容替换{你的SDK文件夹}/tools/proguard下的所有文件。
2.proguard导致编译终止
解决方案:
查看编译的输出内容,找到PROGUARD的报错项,如:
在项目中添加一个proguard.cfg文件,将其build Action设为proguardConfiguration(最好用文本编辑器创建一个无BOM的文件,否则会导致出现Unknown option '-keep' in line 1 of file 'proguard.cfg'
的问题:官方文档)。
在文件中加入对所提示的类的保护,如:
-keep class org.apache.http.**{*;}
-dontwarn org.apache.http.**
再执行编译,就好了。 涉及的包可能不止一个,你需要一个一个添加。
3.编译通过,但运行出现问题,比如界面显示不正常; 解决方案: 在proguard.cfg文件中添加如下代码:
-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.
-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native <methods>;
}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}
# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontwarn android.support.**
# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep @android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}
6.Xamarin.iOS绑定第三方库时遇到如下的错误报告:
/Users/huangboru/myfile/xamarin_workspace/IflyMSCLib/IflyMSCLib/MTOUCH: Error MT5211: Native linking failed, undefined Objective-C class: IFlyRecognizerView. The symbol '_OBJC_CLASS_$_ IFlyUserWords' could not be found in any of the libraries or frameworks linked with your application. (MT5211) (IflyMSCLib)
解决方案:找到ApiDefinition.cs文件中对应的类声明,在前面加上 [Protocol],如:
[BaseType(typeof(NSObject))]
[Protocol]
interface IFlyUserWords {
........
}
7.Xamarin.Android真机Debug部署时出现以下错误:
Mono.AndroidTools.RequiresUninstallException: The installed package is incompatible. Please manually uninstall and try again.
解决方案:以Release方式安装后卸载;
8.在Android10系统上运行出现 Cleartext HTTP traffic to * not permitted
这是因为官方网络安全配置里表示,自Android9开始,系统默认停用明文支持;
解决方案1:使用“https”替代“http”;
解决方案2:在AndroidManifest.xml中添加如下声明:
android:usesCleartextTraffic="true"
形如:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
9.在release模式下编译iOS项目时报Failed to resolve assembly:'Xamarin.Forms.Core,Version=2.0.0.0,Cultrue=neutral,PublicKeyToken=null'(MT2002)
错误;
原因:表面原因是Xamarin.Forms.Core出了问题,实则不是;真实的原因可能iOS项目缺少对某个引用,该引用被iOS项目所引用的项目引用。
解决方案:分析此前向iOS项目所引用的项目中添加了哪些引用,尝试将它们也添加到iOS项目的引用中,编译验证问题是否解决;
- 原文作者:岁寒
- 原文链接:http://www.suihanime.com/post/Xamarin/Xamarin%E5%BC%80%E5%8F%91%E4%B8%AD%E6%88%91%E6%89%80%E9%81%87%E5%88%B0Error%E9%94%99%E8%AF%AF%E7%9A%84%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。