Xamarin開發(fā)中我所遇到Error錯(cuò)誤的解決辦法

Xamarin的坑還是不少的,有些問題可能會(huì)一而再,再而三地重復(fù)出現(xiàn),為了避免下一次遇到同一個(gè)問題重復(fù)的查找,我將在開發(fā)中遭遇到的問題匯總在這里,既方便我自己查閱,也提供給別人做參考;以下給出的解決方案中一部分是解決思路,一部分是確實(shí)成功地解決了問題的方案(我將用斜體標(biāo)出)。

1.Xamarin.iOS:報(bào)Failed to inherit CoreMedia permissions from NNNN錯(cuò)誤;

解決方案:
1.檢查iOS Bundle signing的設(shè)置是否正確;
2.custom Entitlements 是否有值,且為Entitlements.plist;
3.測(cè)試設(shè)備中是否存在多個(gè)使用同一個(gè)app group的app;
4.證書中是否包含了對(duì)app group的正確申請(qǐng);

2.Xamarin.iOS:報(bào)The "XamlCTask" task failed unexpectedly錯(cuò)誤;

解決方案:

  1. clean或者rebuilt項(xiàng)目;
  2. 重啟IDE;
  3. 檢查PCL的配置文件是否一致;

3.Xamarin.Android:打包安裝包時(shí)報(bào)“Error MSB4018: The "ResolveLibraryProjectImports" task failed unexpectedly.”錯(cuò)誤

解決方案:
先rebuild All,再進(jìn)行打包;

4.遇到# Error inflating class android.support.v7.widget.FitWindowsFrameLayout問題

原因:鏈接器移除了對(duì)FitWindowsFrameLayout的打包
解決方案:
欺騙鏈接器,讓其認(rèn)為我們使用了這個(gè)庫(kù);
在代碼中加入如下代碼:

......
static bool falseflag = false;
......
if (falseflag) {
    var ignore = new FitWindowsFrameLayout(Application.Context);
}//雖然這段代碼不會(huì)真正地被調(diào)用,但我在實(shí)踐中發(fā)現(xiàn)這段代碼放在Activity的初始化代碼會(huì)好一些。

5.遇到 # You need to use a Theme.AppCompat theme (or descendant) with this activity 問題

這個(gè)問題好像是xamarin的坑,如果你修改了layout中的xml文件,可能再次build的時(shí)候就會(huì)報(bào)這個(gè)問題。
解決方案:
1.clean后,build;
2.刪除bin和obj目錄,rebuild;

6.Xamarin.Android 發(fā)布release版用proguard混淆時(shí)出現(xiàn)下述問題:

  1. 出現(xiàn)java.exe exited with code 1錯(cuò)誤;
    解決方案:
    更新你的proguard,下載最新版本:下載地址
    下載之后,解壓縮,用里面的內(nèi)容替換{你的SDK文件夾}/tools/proguard下的所有文件。

2.proguard導(dǎo)致編譯終止
解決方案:
查看編譯的輸出內(nèi)容,找到PROGUARD的報(bào)錯(cuò)項(xiàng),如:


在項(xiàng)目中添加一個(gè)proguard.cfg文件,將其build Action設(shè)為proguardConfiguration(最好用文本編輯器創(chuàng)建一個(gè)無(wú)BOM的文件,否則會(huì)導(dǎo)致出現(xiàn)Unknown option '-keep' in line 1 of file 'proguard.cfg'的問題:官方文檔)。

在文件中加入對(duì)所提示的類的保護(hù),如:

-keep class org.apache.http.**{*;}
-dontwarn org.apache.http.**

再執(zhí)行編譯,就好了。
涉及的包可能不止一個(gè),你需要一個(gè)一個(gè)添加。

3.編譯通過(guò),但運(yùn)行出現(xiàn)問題,比如界面顯示不正常;
解決方案:
在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綁定第三方庫(kù)時(shí)遇到如下的錯(cuò)誤報(bào)告:

/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文件中對(duì)應(yīng)的類聲明,在前面加上 [Protocol],如:

    [BaseType(typeof(NSObject))]
    [Protocol]
    interface IFlyUserWords {
........  
}

7.Xamarin.Android真機(jī)Debug部署時(shí)出現(xiàn)以下錯(cuò)誤:

Mono.AndroidTools.RequiresUninstallException: The installed package is incompatible. Please manually uninstall and try again.

解決方案:以Release方式安裝后卸載;

8.在Android10系統(tǒng)上運(yùn)行出現(xiàn)Cleartext HTTP traffic to * not permitted

這是因?yàn)?a target="_blank">官方網(wǎng)絡(luò)安全配置里表示,自Android9開始,系統(tǒng)默認(rèn)停用明文支持;

解決方案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項(xiàng)目時(shí)報(bào)Failed to resolve assembly:'Xamarin.Forms.Core,Version=2.0.0.0,Cultrue=neutral,PublicKeyToken=null'(MT2002)錯(cuò)誤;

原因:表面原因是Xamarin.Forms.Core出了問題,實(shí)則不是;真實(shí)的原因可能iOS項(xiàng)目缺少對(duì)某個(gè)引用,該引用被iOS項(xiàng)目所引用的項(xiàng)目引用。

解決方案:分析此前向iOS項(xiàng)目所引用的項(xiàng)目中添加了哪些引用,嘗試將它們也添加到iOS項(xiàng)目的引用中,編譯驗(yàn)證問題是否解決;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容