Error while preparing an application for release

Hi all,

I have developed an app in Xamarin.Android and now I want to package it for distribution. I am using this guideline as my reference but I am stuck at the very first step!

As soon as I write these lines

#if RELEASE
[assembly: Application(Debuggable=false)]
#else
[assembly: Application(Debuggable=true)]
#endif

in the AssemblyInfo.cs I get the following error when I build the app:

C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(3,3): Error MSB4018: The "GenerateJavaStubs" task failed unexpectedly.
System.InvalidOperationException: Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute.
   at Xamarin.Android.Tasks.ManifestDocument.CreateApplicationElement(XElement manifest, List`1 subclasses, List`1 selectedWhitelistAssemblies)
   at Xamarin.Android.Tasks.ManifestDocument.Merge(List`1 subclasses, List`1 selectedWhitelistAssemblies, Boolean embed, Boolean replaceSplashScreen, IDictionary`2 splashScreenClasses)
   at Xamarin.Android.Tasks.GenerateJavaStubs.Run()
   at Xamarin.Android.Tasks.GenerateJavaStubs.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__20.MoveNext() (MSB4018) (LoyaltyMerchant)

Any help will be highly appreciated.

Thanks.

Best Answer

Answers

  • MHLDMHLD BRBeta ✭✭
    edited September 2013

    Seems that you already have the Application attribute.

    Can you post the entire file?

  • astahirastahir USMember ✭✭

    Thanks for the quick response Mario.

    Here is the complete AssemblyInfo.cs:

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using Android.App;
    
    // Information about this assembly is defined by the following attributes. 
    // Change them to the values specific to your project.
    
    [assembly: AssemblyTitle("LoyaltyMerchant")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("")]
    [assembly: AssemblyCopyright("Ahmed")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    
    // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
    // The form "{Major}.{Minor}.*" will automatically update the build and revision,
    // and "{Major}.{Minor}.{Build}.*" will update just the revision.
    
    [assembly: AssemblyVersion("1.0.0")]
    
    // The following attributes are used to specify the signing key for the assembly, 
    // if desired. See the Mono documentation for more information about signing.
    
    
    //[assembly: AssemblyDelaySign(false)]
    //[assembly: AssemblyKeyFile("")]
    
    
    #if RELEASE
    [assembly: Application(Debuggable=false)]
    #else
    [assembly: Application(Debuggable=true)]
    #endif
    

    Thanks again.

  • astahirastahir USMember ✭✭

    Ruben, you are right I do have another class that is derived from Application.

    Okay, one more question: if I go with the approach of adding the attribute to the existing class, do I always have to comment this line while I am developing and wish to debug, whereas add this line again when willing to package it for distribution?

  • astahirastahir USMember ✭✭

    Thanks again Ruben, finally I have moved ahead in releasing my app but unfortunately I am stuck once again!

    Please refer to the same guide I mentioned earlier. Go to the section "Compile the Application for Release" and at the end it says that after following the instructions when I build the code once again, I should have a couple of APKs in the Release folder but I don't get even one...what I see are a bunch of DLLs which are either used in my app or the DLL of the project itself.

    I notice a couple of things:

    1) The screenshot given at the aforementioned guide and the one I see in my Xamarin Studio (see attached) are different. I am using Xamarin Studio 4.0.8 (build 2) by the way.

    2) This shouldn't be an issue but the screenshot's Xamarin Studio is running on Mac whereas I am using Windows 7.

    Can you please bring me out of this pit too?

    Thanks.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    You can ignore that because those are not the APKs you will be distributing. But to create those APKs, click on Proejct->Create Android Package. Again, you would not use these APKs for distribution as they are signed with the default debug keystore.

    In the guide, continue on with the section "Creating a Private Keystore". Once you create your keystore, continue on with the section "Signing the APK (Xamarin Studio)". This section will create your APK that you will want to distribute, and one that Google Play will accept.

  • astahirastahir USMember ✭✭

    @rmacias, thank you very much for all your help. I have managed to generate the needed APK however now I am facing a new issue! The app gets installed on the device but as soon as I run it, it crashes right after the splash screen! This APK is generated from the same code that is running flawlessly on the emulator as well as the same device when it is connected to my machine for development debugging!

    Can you advise something here too? (I am sorry but I am very new to all this, so asking every question)

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    You can view the logcat when you start your application by using DDMS. Plug in your device to your PC, start DDMS, and you should see it show up. Watch the LogCat and start your app. It'll log everything it does and will capture the error that is happening.

    http://developer.android.com/tools/debugging/ddms.html

  • astahirastahir USMember ✭✭

    Ruben, thanks again for the help mate. This is what I found out.

    This is the stack trace:

        Error: NameResolutionFailure
           at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
           at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0 
           at Karma.Common.Helper.GetResponseFromWebService (System.String webServiceAddress) [0x00000] in <filename unknown>:0 
           at LoyaltyMerchant.MerchantSelectActivity.GetJsonString () [0x00000] in <filename unknown>:0 
    

    This is my method which gets the JSON response from server:
    public static String GetResponseFromWebService(String webServiceAddress)
    {
    String responseFromServer = null;

                // Create a request for the URL. 
                WebRequest request = WebRequest.Create (webServiceAddress);
    
                // Get the response.
                using (WebResponse response = request.GetResponse ())
                {
                    // Get the stream containing content returned by the server.
                    using (Stream dataStream = response.GetResponseStream ())
                    {
                        // Open the stream using a StreamReader for easy access.
                        using (StreamReader reader = new StreamReader (dataStream))
                        {
                            // Read the content.
                            responseFromServer = reader.ReadToEnd ();
                        }
                    }
                }
    
                return responseFromServer;
            }
    

    The error occurs at the line using (WebResponse response = request.GetResponse ()). This is strange because I have verified that the web service address is correct - when I open it in browser, it successfully displays the JSON. Plus, the same code runs without an issue on emulator and on the same device during debugging!

    Any further ideas?

  • rmaciasrmacias USBeta, University ✭✭✭✭✭
    edited September 2013

    Where is your webservice located and what address are you pointing to? Is the webservice running locally on your dev machine or is this running an an actual web server? From your device, can you access your web server?

    http://developer.android.com/tools/devices/emulator.html#networkaddresses

  • astahirastahir USMember ✭✭

    The web service is running on the actual web server and when I open the URL in device's browser, it does show me the JSON...so no problems there.

  • astahirastahir USMember ✭✭

    Ruben, issue has been resolved as I was missing a very trivial point. I hadn't added the INTERNET permission to AndroidManifest file. The issue went away as soon as I did that!

    Thanks a lot for all the help.

  • ramonesteban78ramonesteban78 ESUniversity ✭✭

    @astahir you saved my day!

    But how is it possible that the Android version was working good in debug mode without the permission? If the permission was mandatory even in DEBUG then we wouldn't have these kind of issues.

    Regards

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    @RamonEsteban.9134, the reason why is that when you compile in the DEBUG configuration, Xamarin will automatically add the INTERNET permission for you in the AppManifest. This is meant to be more of a convenience in order to get people going faster. However, it will not add it in any other configuration. The side effect, unfortunately, is developers forgetting they need to set that permission.

  • ehsaan_buttehsaan_butt USMember

    Help in resolving this error of Java Stubs ... thanks in advance guys


  • ehsaan_buttehsaan_butt USMember
    edited May 2016


    **_

  • batmacibatmaci DEMember ✭✭✭✭

    @rmacias said:
    No, you can just use the compiler directives

    #if RELEASE
    [Application(Debuggable=false)]
    #else
    [Application(Debuggable=true)]
    #endif
    MyAppClass : Application
    { 
      //code here
    }
    

    Is that a xamarin.forms sample? # tags are used in shared applications only I thought, no? how do you do it for xamarin.forms

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    @batmaci said:

    @rmacias said:
    No, you can just use the compiler directives

    #if RELEASE
    [Application(Debuggable=false)]
    #else
    [Application(Debuggable=true)]
    #endif
    MyAppClass : Application
    { 
      //code here
    }
    

    Is that a xamarin.forms sample? # tags are used in shared applications only I thought, no? how do you do it for xamarin.forms

    No, those are compiler directives. They are a part of the C# language and not specific to Xamarin Forms. Just be sure to define them in your project settings or by using #define.

Sign In or Register to comment.