Bug 26115 - Android Unit Testing Fails to Resolve Service
Summary: Android Unit Testing Fails to Resolve Service
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.18.1
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-01-16 13:53 UTC by Cody Beyer (MSFT)
Modified: 2015-01-19 16:16 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Cody Beyer (MSFT) 2015-01-16 13:53:54 UTC
### Description

Adding an Android Unit Test project to a solution, and then adding a project as a reference, does not allow you to instantiate a new version of the server that was added

### Details

This was originally reported on the following forum post: http://forums.xamarin.com/discussion/29203/unit-testing-with-android-unit-test-project-in-xamarin-forms

### Version 

=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: 3e4348fa-e0b3-46a4-8426-58f27e19159e
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.51 (Business Edition)
Hash: dfb682f
Branch: 
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/codybeyer/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin.Mac ===

Version: 1.10.0.18 (Business Edition)

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.10.1
Darwin Codys-MBP.router 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 1 Abhishek 2015-01-19 10:53:14 UTC
I have tried this issue and able to reproduce this issue at my end.

Steps to Reproduce:

1. Create a sample Xamarin Forms Application.
2. Create a  new Demo class in the Xamarin.Forms.Droid.
3. Add new Project "Android Unit Test" to the solution.
4. Add Reference of Xamarin.Android in the Unit Test application.
5. Create an instance of above class in the TestSample but unable to create an  instance of that class.

Screencast: http://www.screencast.com/t/2pu29nkh4z

Could you please confirm, whether I am following correct steps to reproduce the behavior which you are facing? And is it same issue that you have reported here?

Build Info:

=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: 93e693b0-b53d-40f4-b29c-b61ff5cbe892
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.iOS ===

Version: 8.6.0.51 (Enterprise Edition)
Hash: dfb682f
Branch: 
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Enterprise Edition)
Android SDK: /Users/xamarin23/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin.Mac ===

Version: 1.10.0.18 (Enterprise Edition)

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.10.0
Darwin 800BC.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 2 Jesse Johnston 2015-01-19 13:53:44 UTC
The steps you have listed are not correct.  I inadvertently posted that question to the "Xamarin.Forms" section of the forums, but I am not using Xamarin.Forms.

I just created a new project and I am able to reproduce the issue with the following steps: 

1. Create a Portable Library project.
2. Create a Android Application project.
3. Add PCL project as reference in Android Application project
4. Create a Android Unit Test project.
5. Add PCL project and Android Application project as references in Unit Test project.
6. Create an example class in PCL project
7. Create an example class in Android Application project
8. Try to create an instance of both example classes in Android Unit Test project

The outcome is that I am unable to instantiate the Android Application example class in the Android Unit Test project.


As I mentioned in my forum post, this seems to be a build dependency issue.  I followed a similar setup when creating a iOS Unit Test project.  When I build my iOS Unit test project, it first builds the PCL, then it builds the iOS Application, and finally it builds the iOS Unit Test project.  When I build the Android Unit Test project, it first build the PCL, then it attempts to build the Unit Test project, but it does not build the Android Application project.
Comment 3 Jesse Johnston 2015-01-19 13:58:18 UTC
Sorry for the double post, but to answer your second question: Yes, the problem you are facing does appear to be the same even though the steps to reproduce were slightly different.
Comment 4 Jonathan Pryor 2015-01-19 15:41:30 UTC
> 5. Add PCL project and Android Application project as references in Unit Test project.

No.

There be dragons here. :-)

(We *really* need to add an warning for doing this...and documentation...)

The primary difference between an Android Library project and an Android Application project is how Resource constants are handled: in Applications, they're `const`, and in Library projects, they're `static` and *writable*.

The reason for this difference is that it's possible for a Library project to provide a resource, e.g. "Images/foo.png", and for the Application to provide the "same" resource name but different content. 

During process startup all of the Library "constant values" are updated to the Application's constant values so that the Application-provided resources are used.

Because of this architecture, it's not valid to have an Application project be referenced by an Application project, because the values may (will) differ between them, resulting in all manner of "weirdness".

The Unit Test Project is an Application project.

Meaning your step (5) means "Add an Application project as a reference to an Application project," which means the Resource values will be all wrong, and all manner of weirdness *would* result.

Note the *would*; *past tense*.

For Xamarin.Android 5.0, we were going to change these semantics to better improve Android Wear projects: an Android Wear project is an Application project, so if you added a (Wear) Application project to an Application project, the Wear project would *not* be used as an assembly reference. Instead, we'd *separately package* the Wear app and include it as a Resource within the referencing Application project, making Wear projects much easier to deal with (no need to write custom Resource XML, no need to manually build + package the Wear project, etc.)

Xamarin.Android 5.0 isn't out yet, but some of this functionality is part of the IDE (the IDE adds %(ProjectReference.IsAppExtension) metadata for the Wear project), which *did* ship, and Xamarin.Android 4.20 *also* got some of this (incomplete) Wear support.

Project references which have %(ProjectReference.IsAppExtension)=True are *removed* from @(ProjectReference) prior to compilation; consequently, your ExampleApp.Android.dll reference is stripped and never seen by the ExampleApp.Android.UnitTests project, which is why you get a compiler error when attempting to use types that come from the Application project.

This is currently By Design™, though we clearly need to improve the IDE and documentation experience here, and the fact that Xamarin.Android 5.0 hasn't shipped yet doesn't help matters.
Comment 5 Jonathan Pryor 2015-01-19 15:43:00 UTC
Marking as ANSWERED as per Comment #4.
Comment 6 Jesse Johnston 2015-01-19 15:51:00 UTC
Just to clarify, you can't use an "Android Unit Test" project to test code in an "Android Application" project?

Or maybe the better question is, how can I unit test code that is in an Android Application project?
Comment 7 Jonathan Pryor 2015-01-19 16:16:14 UTC
> how can I unit test code that is in an Android Application project?

Don't. If you want to write unit tests for a type T, place the type T into a PCL project or an Android Library project. You can then use T from both the Unit Test project and the Application project without any problem.