2012-03-09 31 views
0

由MonoDroid Professional生成的apk文件的文件大小非常大(18MB),我試圖通過調用SDK程序集上的鏈接來減少它(選擇'SDK Assemblies Only'when建造)。不幸的是,這失敗了。Monodroid Linker對於簡單的AsyncTask失敗

下面是測試代碼,演示了這個問題:

TestAsyncTask : AsyncTask<MyInt, int, int> 
{ 
    Activity1 _myActivity; 
    public TestAsyncTask(Activity1 activity) 
    : base() 
    { 
    _myActivity = activity; 
    } 

    protected override void OnPreExecute() 
    { 

    } 

    protected override int RunInBackground(params MyInt[] @params) 
    { 
    int taget = @params[0].IntValue; 
    Android.Util.Log.Debug("RunInBackground", taget.ToString()); 

    System.Threading.Thread.CurrentThread.Join(1000); 
    return taget; 
    } 

    protected override void OnPostExecute(int results) 
    { 
    _myActivity.textView1.Text = results + " Complete!"; 
    } 
} 

public class MyInt : Java.Lang.Object 
{ 
    public int IntValue; 
} 

,這裏是在設備上運行時,它遇到的輸出:

UNHANDLED EXCEPTION: System.ArgumentException: Couldn't bind to method 'GetDoInBackground_arrayLjava_lang_Object_Handler'. 

at System.Delegate.GetCandidateMethod (System.Type,System.Type,string,System.Reflection.BindingFlags,bool,bool) <IL 0x0010c, 0x00728> 

at System.Delegate.CreateDelegate (System.Type,System.Type,string,bool,bool) <IL 0x00018, 0x000f7> 

at System.Delegate.CreateDelegate (System.Type,System.Type,string) <IL 0x00005, 0x00063> 

at Android.Runtime.JNIEnv.RegisterJniNatives (intptr,int,intptr,intptr,int) <IL 0x00175, 0x00a03> 

at (wrapper delegate-invoke) <Module>.invoke_intptr__this___intptr_intptr_string_string (intptr,intptr,string,string) <IL 0x0005d, 0x00117> 

at Android.Runtime.JNIEnv.GetMethodID (intptr,string,string) <IL 0x00012, 0x000a7> 

at Android.Runtime.JNIEnv.CreateInstance (intptr,string,Android.Runtime.JValue[]) <IL 0x00007, 0x00063> 

at Android.Runtime.JNIEnv.CreateInstance (System.Type,string,Android.Runtime.JValue[]) <IL 0x0000a, 0x00093> 

at Android.OS.AsyncTask`3<MonoAndroidApplication1.MyInt, int, int>..ctor() <IL 0x00049, 0x0015b> 

at MonoAndroidApplication1.TestAsyncTask..ctor (MonoAndroidApplication1.Activity1) <IL 0x00001, 0x0005b> 

at MonoAndroidApplication1.Activity1.OnCreate (Android.OS.Bundle) <IL 0x00035, 0x00213> 

at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00012, 0x000e7> 

at (wrapper dynamic-method) object.8bb95c67-1299-4094-bc2d-2b10b61aa06b (intptr,intptr,intptr) <IL 0x00012, 0x00033> 
03-09 14:17:04.623 E/mono (20045): 

我能夠將文件大小減少到11MB,如果我跳過使用AndroidLinkSkip作爲PropertyGroup鏈接MonoAndroid.dll。謝謝你的幫助。

回答

1

看一看這樣的:

Using Background Threads in Mono For Android Applications

我不使用任何Android的原生多線程功能,但使用.NET版本。 通常情況下,所有你需要做的是:

ThreadPool.QueueUserWorkItem(state => { ... }); 
+0

感謝馬修!你真的很棒。我們按照建議更改了代碼,並設法將文件大小降至4.4MB。它現在非常出色! – 2012-03-13 23:40:06

+0

@IronHarry謝謝!如果它有效,你可以接受我的答案 - 這將幫助其他人找到答案(我想要更多的代表);-) – Matthew 2012-03-14 06:47:01