Bug 38656 - StrictMode does not expose the 'detectClearTextTraffic`API
Summary: StrictMode does not expose the 'detectClearTextTraffic`API
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: unspecified
Hardware: All Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-02-11 00:20 UTC by Matthew Robbins
Modified: 2016-02-11 21:40 UTC (History)
1 user (show)

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


Attachments
Scrrenshot of the android manifest. (291.02 KB, image/png)
2016-02-11 04:38 UTC, Matthew Robbins
Details
Screenshot for the VM policy builder binding code. (344.87 KB, image/png)
2016-02-11 04:39 UTC, Matthew Robbins
Details


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 FIXED

Description Matthew Robbins 2016-02-11 00:20:56 UTC
Starting from Android API Level 23, StrictMode is able to detect clear-text network traffic and dump an error into logcat. This is via the following methods:

http://developer.android.com/reference/android/os/StrictMode.VmPolicy.Builder.html#detectCleartextNetwork()
http://developer.android.com/reference/android/os/StrictMode.VmPolicy.Builder.html#penaltyDeathOnCleartextNetwork()

When targeting and running on an Android 23 / L device, these APIs are accessible through the Java reflection API. We can pull the method out and invoke them like so:

	var builder = new StrictMode.VmPolicy.Builder ();

	var methods = builder.Class.GetMethods ();
	var clearTextMethod = builder.Class.GetMethod ("detectCleartextNetwork");
	builder = clearTextMethod.Invoke (builder) as StrictMode.VmPolicy.Builder;

	builder.PenaltyLog ();
	StrictMode.SetVmPolicy(builder.Build ());

While this is a workaround, it's not ideal. Can these methods be exposed through the Xamarin.Android runtime api?
Comment 1 Matthew Robbins 2016-02-11 00:21:28 UTC
=== Xamarin Studio ===

Version 5.10.2 (build 56)
Installation UUID: 0c4328cb-e1b1-4543-8254-d92b3ee4da80
Runtime:
	Mono 4.2.2 (explicit/996df3c Wed Jan 20 00:19:48 EST 2016)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402020030

=== Xamarin.Profiler ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.1.10 (Business Edition)
Android SDK: /Users/matthewrobbins/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		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)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.2.1 (9548.1)
Build 7C1002

=== Xamarin.iOS ===

Version: 9.4.1.25 (Business Edition)
Hash: 962a050
Branch: master
Build date: 2016-01-29 16:59:11-0500

=== Xamarin.Mac ===

Version: 2.4.1.6 (Starter Edition)

=== Build Information ===

Release ID: 510020056
Git revision: bb74ff467c62ded42b7b7ac7fdd2edc60f8647b0
Build date: 2016-01-26 16:24:41-05
Xamarin addins: 8b797d7ba24d5abab226c2cf9fda77f666263f1b
Build lane: monodevelop-lion-cycle6-c6sr1

=== Operating System ===

Mac OS X 10.11.3
Darwin JobAdders-iMac-6.local 15.3.0 Darwin Kernel Version 15.3.0
    Thu Dec 10 18:40:58 PST 2015
    root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
Comment 2 Jonathan Pryor 2016-02-11 03:03:00 UTC
> Can these methods be exposed through the Xamarin.Android runtime api?

Are they actually missing?

> $ monop -r:/Library/Frameworks/Xamarin.Android.framework/Libraries/xbuild-frameworks/MonoAndroid/v6.0/Mono.Android.dll \
    Android.OS.StrictMode | grep DetectClear
			public Builder DetectCleartextNetwork ();

It appears to be there for me...

Unfortunately our documentation site hasn't been updated for API-23 yet, but the bindings have been.
Comment 3 Matthew Robbins 2016-02-11 03:18:11 UTC
I'll double check when I get back to my desk but auto-completion doesn't suggest them nor does it compile.

I'm currently on the latest revision of the Stable channel.

Will confirm in an hour.
Comment 4 Matthew Robbins 2016-02-11 04:38:26 UTC
Created attachment 14997 [details]
Scrrenshot of the android manifest.

Attached is a screenshot of the manifest for the project.
Comment 5 Matthew Robbins 2016-02-11 04:39:11 UTC
Created attachment 14998 [details]
Screenshot for the VM policy builder binding code.

Attached is a screen shot of the vm policy builder binding.
Comment 6 Matthew Robbins 2016-02-11 04:41:02 UTC
I'll upgrade to Alpha and Beta to verify.
Comment 7 Jonathan Pryor 2016-02-11 11:54:43 UTC
What is the $(TargetFrameworkVersion) value in your .csproj? It needs to be v6.0.

Additionally, Comment #2 is from the currents table:

> $ /Developer/MonoAndroid/usr/bin/mandroid  --version
> mandroid 6.0.1.148707959
Comment 8 Matthew Robbins 2016-02-11 21:40:20 UTC
That was it! Thanks for the help Jonathon.