This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 48678 - NDK r12b+ is not supported
Summary: NDK r12b+ is not supported
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild (show other bugs)
Version: 5.1
Hardware: PC Mac OS
: --- normal
Target Milestone: 15.1
Assignee: dean.ellis
URL:
Depends on:
Blocks:
 
Reported: 2016-12-02 02:25 UTC by Jonathan Pryor
Modified: 2017-03-17 08:00 UTC (History)
8 users (show)

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


Attachments

Description Jonathan Pryor 2016-12-02 02:25:52 UTC
NDK r12b removed the headers for API-4:

https://developer.android.com/ndk/downloads/revision_history.html#r12b
> 	• Removed all sysroots for pre-GB platform levels. We dropped support for them in r11,
> 	  but neglected to actually remove them.

Unfortunately, we hardcoded API-4 when targeting 32-bit ARM platforms:

https://github.com/xamarin/xamarin-android/blob/3fe80c5/src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs#L105-L106

> int level = NdkUtil.IsNdk64BitArch (arch) ? 21 : arch == AndroidTargetArch.Arm ? 4 : 9;

Consequently, when using NDK r12b or later and trying to use "Bundle assemblies into native code," it doesn't work.

*Worse*, the failure code is not obvious, because of:

https://github.com/xamarin/xamarin-android/blob/3fe80c5/src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs#L64

The error -- the android-4 directory can't be found -- is "swallowed" and reported as a normal message. The build continues:

>	Task "MakeBundleNativeCodeExternal" 
>		Error: System.InvalidOperationException: Platform header files for target Arm and API Level 4 was not found. Expected path is ".../android-ndk-r13b/platforms/android-4/arch-arm/usr/include"
>	                  at Xamarin.Android.Tasks.NdkUtil.GetNdkPlatformIncludePath (System.String androidNdkPath, Xamarin.Android.Build.Utilities.AndroidTargetArch arch, System.Int32 level) [0x00071] in <742ce926f1f848099e838fb951dcfe1c>:0 
>	…
>		0 Error(s) 
# Look ma! No errors!

Because the build continues, a `.apk` is generated, but the `.apk` is *unusable*. It contains *no* assemblies, and thus attempting to run results in the app exiting on launch:

> No assemblies found in '(null)' or '<unavailable>'. Assuming this is part of Fast Deployment. Exiting...

We need to fix the <MakeBundleNativeCodeExternal/> task to:

1. NOT IGNORE ERRORS. Line 64 should be a LogErrorFromException().

2. Sanely check for include directories, so that when android-4 doesn't exist, we continue checking for increasing directories and eventually find android-9, which does exist.
Comment 1 micke 2017-02-04 18:55:48 UTC
I second this. Any workaround or new status?
Comment 2 Craig Johnson 2017-02-18 19:21:51 UTC
I cannot even believe this was allowed. Seeing swallowed error using NDK r13b and a bogus .APK generated with no errors.
Comment 3 dean.ellis 2017-02-20 11:43:07 UTC
PR up https://github.com/xamarin/xamarin-android/pull/452
Comment 4 dean.ellis 2017-02-23 11:43:10 UTC
Fixed in xamarin-android/master/6f73856f
Comment 5 batmaci 2017-02-23 14:32:34 UTC
This is not fixed. It is happening using NDK r13b together with "bundle assemblies into native code".

 my error message is 
"System.InvalidOperationException: Platform header files for target Arm and API Level 4 was not found. Expected path is "C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r13b\platforms\android-4\arch-arm\usr\include""

I dont get why it complains about API level 4. I support min Api Level 21 in my android project
Comment 6 batmaci 2017-02-23 14:35:47 UTC
wait, is it the latest beta version now? Which nuget should we download to fix this issue? Please explain how it is resolved.
Comment 7 dean.ellis 2017-02-23 16:08:20 UTC
@batmaci

As part of the QA process we need to mark the bug as fixed so that our QA team will test the proposed fix on our master builds. Once it is verified|Fixes then it will make it into a release.
Comment 8 Brendan Zagaeski 2017-03-02 20:55:24 UTC
Non-engineering team update




## Candidate patch included in latest Alpha feature previews (published on March 1, 2017)

The new Alpha Xamarin.Android 7.2.0 (b24b9f2) from yesterday now references xamarin-android (1a23303), and that version includes the change for this issue [1].

[1] https://github.com/xamarin/xamarin-android/commits/1a2330336b9c0499efee5c85566a0e99bc437b96/src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs

I will post a follow-up verification comment to test that build locally with the test scenario described below for downgrading the NDK.




## Workaround by downgrading the NDK version


1. Download the android-ndk-r11c-windows-x86_64.zip NDK from [1].
[1] https://developer.android.com/ndk/downloads/older_releases.html


2. Unzip the NDK to the desired location.  For example, I chose to unzip it to C:\.  That creates a new folder named "android-ndk-r11c".  The zip file is unfortunately fairly dense so it can take 15 minutes or more to extract, at least when using Windows's built-in unzipping functionality.


3. Update the setting for the NDK path in "Tools > Options > Xamarin > Android Settings > Android NDK Location" to point to the older version of the NDK from step 2.


4. Clean the project, and then quit and restart Visual Studio.



After completing these steps, I was able to use the NDK successfully.  In particular, I was able to build, deploy, and run successfully with "Bundle assemblies into native code" enabled, whereas with r12-beta-1 I had hit the error as described in Comment 0.
Comment 9 Brendan Zagaeski 2017-03-02 21:36:24 UTC
## Verification status: verified fixed in XamarinVS 4.3.1.1, Xamarin.Android 7.1.99 (aka 7.2.0)

GOOD: XamarinVS 4.3.1.1   (3b21f70), Xamarin.Android 7.1.99.93 (8943b85)
BAD:  XamarinVS 4.3.0.784 (73f58d6), Xamarin.Android 7.1.0.41  (9578cdc)


(Note: Xamarin.Android 7.1.99.93 (8943b85) references xamarin-android (1a23303), just like the Xamarin.Android 7.2.0 (b24b9f2) package on Mac.)




## Steps followed to test

1. Create a new "Visual C# > Android > Blank App (Android)" project.

2. Enable "Project Properties > Android Options > Packaging [tab] > Bundle assemblies into native code" for the Release configuration.

3. "Enable the Project Properties > Android Options > Advanced [tab] > x86" (to allow testing on the Hyper-V Visual Studio Android Emulator).




## Additional test environment info

Android NDK r12 Beta 1 (64-bit)
(AndroidNdkDirectory: C:\android-ndk-r12-beta1\)

Java JDK 8u121 (64-bit)
(JavaSdkPath: C:\Program Files\Java\jdk1.8.0_121)

Android SDK Tools 25.2.4
Android SDK Platform-tools 25.0.3
Android SDK Build-tools 25.0.2

Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01586

Windows 10 (64-bit) Version 1607 (OS Build 14393.321)
US English locale, US eastern time zone



### Testing device

KitKat (Android 4.4 - API 19) Visual Studio Android Emulator
Comment 10 Brendan Zagaeski 2017-03-03 00:07:34 UTC
(Comment 9 was a verification for part (2) from Comment 0: the mkbundle step now searches for the lowest available platform in the NDK folder.)

Here is a verification for part (1) from Comment 0 (the swallowed error).




## Verification status for part (1) from Comment 0 0: also verified fixed in XamarinVS 4.3.1.1, Xamarin.Android 7.1.99 (aka 7.2.0)

GOOD: XamarinVS 4.3.1.1   (3b21f70), Xamarin.Android 7.1.99.93 (8943b85)
BAD:  XamarinVS 4.3.0.784 (73f58d6), Xamarin.Android 7.1.0.41  (9578cdc)



### Steps followed to test

1. Create a new "Visual C# > Android > Blank App (Android)" project.

2. Enable "Project Properties > Android Options > Packaging [tab] > Bundle assemblies into native code" for the Release configuration.

3. "Enable the Project Properties > Android Options > Advanced [tab] > x86" (to allow testing on the Hyper-V Visual Studio Android Emulator).

4. Rename "%ProgramFiles(x86)%\MSBuild\Xamarin\Android\mkbundle.exe" to a different name temporarily such as "%ProgramFiles(x86)%\MSBuild\Xamarin\Android\mkbundle_backup.exe" to _intentionally_ cause an error during the mkbundle step.

5. Attempt to build and deploy the project in the Release configuration.



### GOOD Results with XamarinVS 4.3.1.1 (3b21f70), Xamarin.Android 7.1.99.93 (8943b85)

As desired for the correction proposed in Comment 0, the build FAILS as soon as it is unable to locate `mkbundle`:

> [mkbundle] C:\Program Files (x86)\MSBuild\Xamarin\Android\mkbundle.exe --dos2unix=false ...
> The system cannot find the file specified
> Done executing task "MakeBundleNativeCodeExternal" -- FAILED.
> Done building target "_BuildApkEmbed" in project "AndroidApp1.csproj" -- FAILED.
> Done building project "AndroidApp1.csproj" -- FAILED.
> Build FAILED.


### BAD Results with XamarinVS 4.3.0.784 (73f58d6), Xamarin.Android 7.1.0.41 (9578cdc)

As described in Comment 0, the build completes "successfully", but the resulting .apk is invalid.  The problem is that the failure to locate `mkbundle` does not stop the build.  Here's an excerpt from the diagnostic build output that shows the BuildApk task starting after the error:

>[mkbundle] C:\Program Files (x86)\MSBuild\Xamarin\Android\mkbundle.exe --dos2unix=false ...
> Error: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
>    at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
>    at System.Diagnostics.Process.Start()
>    at Xamarin.Android.Tasks.MakeBundleNativeCodeExternal.DoExecute()
>    at Xamarin.Android.Tasks.MakeBundleNativeCodeExternal.Execute()
> Done executing task "MakeBundleNativeCodeExternal".
> Using "BuildApk" task from assembly "C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Build.Tasks.dll".


### Additional test environment info

Android NDK r12 Beta 1 (64-bit)
(AndroidNdkDirectory: C:\android-ndk-r12-beta1\)

Java JDK 8u121 (64-bit)
(JavaSdkPath: C:\Program Files\Java\jdk1.8.0_121)

Android SDK Tools 25.2.4
Android SDK Platform-tools 25.0.3
Android SDK Build-tools 25.0.2

Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01586

Windows 10 (64-bit) Version 1607 (OS Build 14393.321)
US English locale, US eastern time zone
Comment 11 MSiccDev 2017-03-14 17:46:31 UTC Comment hidden (me-too, obsolete)

Note You need to log in before you can comment on or make changes to this bug.