Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
AndroidX (#1095)
Browse files Browse the repository at this point in the history
* Use AndroidX references for Android 10+

* Directly use AndroidX api's on API 29 +

* Bump sample/device tests to api 29

* Migrate resource files in device tests / sample to androidx

* Fix nuget references

* Fix test with api 29 deprecations

* Go STABLE!!!

* Drop Android 6.0-7.1 as targets

This is just the compile targets, it still runs all the way to 4.4 - and probably earlier

Co-authored-by: Matthew Leibowitz <[email protected]>
  • Loading branch information
Redth and mattleibow authored Feb 20, 2020
1 parent d73fb42 commit f37613f
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 16 deletions.
6 changes: 5 additions & 1 deletion DeviceTests/DeviceTests.Android/DeviceTests.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>Library</OutputType>
<RootNamespace>DeviceTests.Droid</RootNamespace>
<AssemblyName>XamarinEssentialsDeviceTestsAndroid</AssemblyName>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
Expand Down Expand Up @@ -57,6 +57,10 @@
<PackageReference Include="Xamarin.Android.Support.v7.MediaRouter" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.1.0" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.0.0" />
<PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0" />
<PackageReference Include="Xamarin.AndroidX.MediaRouter" Version="1.1.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.devices" Version="2.5.25" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0.1.0" package="com.xamarin.essentials.devicetests" android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.google.android.material.tabs.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<android.support.v7.widget.Toolbar
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
Expand Down
6 changes: 6 additions & 0 deletions DeviceTests/DeviceTests.Android/Tests/FileProvider_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ public void Get_Existing_External_Cache_Shareable_Uri(FileProviderLocation locat
public void Get_Existing_External_Shareable_Uri(FileProviderLocation location)
{
// Save an external directory file

#if !__ANDROID_29__
var externalRoot = AndroidEnvironment.ExternalStorageDirectory.AbsolutePath;
#endif

var root = Platform.AppContext.GetExternalFilesDir(null).AbsolutePath;
var file = CreateFile(root);

Expand All @@ -200,10 +204,12 @@ public void Get_Existing_External_Shareable_Uri(FileProviderLocation location)
Assert.Equal("content", shareableUri.Scheme);
Assert.Equal("com.xamarin.essentials.devicetests.fileProvider", shareableUri.Authority);

#if !__ANDROID_29__
// replace the real root with the providers "root"
var segements = Path.Combine(root.Replace(externalRoot, "external_files"), Path.GetFileName(file));

Assert.Equal(segements.Split(Path.DirectorySeparatorChar), shareableUri.PathSegments);
#endif
}

static string CreateFile(string root, string name = "the-file.txt")
Expand Down
2 changes: 1 addition & 1 deletion Samples/Samples.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.essentials" android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
2 changes: 1 addition & 1 deletion Samples/Samples.Android/Resources/layout/Tabbar.axml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.google.android.material.tabs.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion Samples/Samples.Android/Resources/layout/Toolbar.axml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<android.support.v7.widget.Toolbar
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
Expand Down
7 changes: 6 additions & 1 deletion Samples/Samples.Android/Samples.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidUseAapt2>true</AndroidUseAapt2>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
Expand Down Expand Up @@ -58,6 +58,7 @@
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="2.1.1" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="2.1.1" />
<PackageReference Include="Microsoft.AppCenter.Distribute" Version="2.1.1" />
<PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.0" />
<PackageReference Include="Xamarin.Forms" Version="4.3.0.947036" />
<PackageReference Include="Xamarin.Forms.Visual.Material" Version="4.3.0.947036" />
<PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.3" />
Expand All @@ -67,6 +68,10 @@
<PackageReference Include="Xamarin.Android.Support.v7.MediaRouter" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.1.0" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.0.0" />
<PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0" />
<PackageReference Include="Xamarin.AndroidX.MediaRouter" Version="1.1.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions Xamarin.Essentials/Browser/Browser.android.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Threading.Tasks;

using Android.Content;
#if __ANDROID_29__
using AndroidX.Browser.CustomTabs;
#else
using Android.Support.CustomTabs;

#endif
using AndroidUri = Android.Net.Uri;

namespace Xamarin.Essentials
Expand Down
5 changes: 5 additions & 0 deletions Xamarin.Essentials/Permissions/Permissions.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
using Android;
using Android.Content.PM;
using Android.OS;
#if __ANDROID_29__
using AndroidX.Core.App;
using AndroidX.Core.Content;
#else
using Android.Support.V4.App;
using Android.Support.V4.Content;
#endif

namespace Xamarin.Essentials
{
Expand Down
1 change: 0 additions & 1 deletion Xamarin.Essentials/Platform/Platform.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Android.Net;
using Android.Net.Wifi;
using Android.OS;
using Android.Support.V4.Content;
using AndroidUri = Android.Net.Uri;

namespace Xamarin.Essentials
Expand Down
13 changes: 9 additions & 4 deletions Xamarin.Essentials/Types/FileProvider.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
using Android.Content;
using Android.OS;
using AndroidEnvironment = Android.OS.Environment;
#if __ANDROID_29__
using ContentFileProvider = AndroidX.Core.Content.FileProvider;
#else
using ContentFileProvider = Android.Support.V4.Content.FileProvider;
#endif

namespace Xamarin.Essentials
{
Expand All @@ -13,9 +18,9 @@ namespace Xamarin.Essentials
Exported = false,
GrantUriPermissions = true)]
[MetaData(
"android.support.FILE_PROVIDER_PATHS",
"android.support.FILE_PROVIDER_PATHS", // IMPORTANT: This string doesn't change with AndroidX
Resource = "@xml/xamarin_essentials_fileprovider_file_paths")]
public class FileProvider : global::Android.Support.V4.Content.FileProvider
public class FileProvider : ContentFileProvider
{
internal static bool AlwaysFailExternalMediaAccess { get; set; } = false;

Expand Down Expand Up @@ -89,9 +94,9 @@ internal static bool IsFileInPublicLocation(string filename)
#if __ANDROID_29__
Platform.AppContext.GetExternalFilesDir(null).CanonicalPath,
#else
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
AndroidEnvironment.ExternalStorageDirectory.CanonicalPath,
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0618 // Type or member is obsolete
#endif
Platform.AppContext.ExternalCacheDir.CanonicalPath
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Threading.Tasks;
using Android.Content;
#if __ANDROID_29__
using AndroidX.Browser.CustomTabs;
#else
using Android.Support.CustomTabs;
#endif

namespace Xamarin.Essentials
{
Expand Down
9 changes: 8 additions & 1 deletion Xamarin.Essentials/Xamarin.Essentials.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.WatchOS10;MonoAndroid60;MonoAndroid70;MonoAndroid71;MonoAndroid80;MonoAndroid81;MonoAndroid90;MonoAndroid10.0;tizen40;</TargetFrameworks>
<TargetFrameworks>netstandard1.0;netstandard2.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.WatchOS10;MonoAndroid80;MonoAndroid81;MonoAndroid90;MonoAndroid10.0;tizen40;</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.16299;</TargetFrameworks>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<RootNamespace>Xamarin.Essentials</RootNamespace>
Expand Down Expand Up @@ -76,11 +76,18 @@
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors" />
<AndroidResource Include="Resources\xml\*.xml" />
</ItemGroup>
<!-- NOTE: When Android 11.x comes out we neeed to make this check more robust -->
<ItemGroup Condition="$(TargetFramework.StartsWith('MonoAndroid')) And '$(TargetFramework)' != 'MonoAndroid10.0'">
<PackageReference Include="Xamarin.Android.Support.Compat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="28.0.0.3" />
<PackageReference Condition=" '$(OS)' == 'Windows_NT' And $(TargetFrameworkVersion.TrimStart('vV')) &lt; 9.0 And $(TargetFrameworkVersion.TrimStart('vV')) &lt; 10.0" Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
<PackageReference Condition=" '$(OS)' != 'Windows_NT' " Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
</ItemGroup>
<!-- NOTE: When Android 11.x comes out we neeed to make this check more robust -->
<ItemGroup Condition="'$(TargetFramework)' == 'MonoAndroid10.0'">
<PackageReference Include="Xamarin.AndroidX.Browser" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<Compile Include="**\*.ios.cs" />
<Compile Include="**\*.ios.*.cs" />
Expand Down

0 comments on commit f37613f

Please sign in to comment.