Skip to content

Commit

Permalink
Add extension to Asset Delivery (#842)
Browse files Browse the repository at this point in the history
It adds the `AssetPackStateUpdateListenerWrapper` to the `Xamarini.Google.Android.Play.Asset.Delivery` nuget package to help users deal with ondemand asset packs. 
This class is also n the Xamarin.Google.Android.Play.Core package, but that package has been depricated  and appears to have issues on API 34.

Remove the old `AssetPackStateUpdateListenerWrapper` from `Xamarini.Google.Android.Play.Core` as it will conflict with the one that will be in `Xamarini.Google.Android.Play.Asset.Delivery`. Do the same for the `SplitInstallStateUpdateListenerWrapper` this will now be in `Xamarini.Google.Android.Play.Feature.Delivery`.

Add a sample app called `BuildAllPlayDotNet` which will use the Asset Delivery API and the wrapper class to make sure it is compiled into the nuget package.
  • Loading branch information
dellis1972 committed Mar 11, 2024
1 parent 3ba6fbc commit 204f71c
Show file tree
Hide file tree
Showing 107 changed files with 1,245 additions and 337 deletions.
27 changes: 26 additions & 1 deletion build.cake
Expand Up @@ -540,6 +540,30 @@ Task("libs-native")
CopyFileToDirectory($"{root}/extensions-aar/build/outputs/aar/extensions-aar-release.aar", outputDir);
Unzip($"{outputDir}/extensions-aar-release.aar", outputDir);
MoveFile($"{outputDir}/classes.jar", $"{outputDir}/extensions.jar");
root = "./source/com.google.android.play/asset.delivery.extensions/";
RunGradle(root, "build");
outputDir = "./externals/com.xamarin.google.android.play.asset.delivery.extensions/";
EnsureDirectoryExists(outputDir);
CleanDirectories(outputDir);
CopyFileToDirectory($"{root}/extensions-aar/build/outputs/aar/extensions-aar-release.aar", outputDir);
Unzip($"{outputDir}/extensions-aar-release.aar", outputDir);
MoveFile($"{outputDir}/classes.jar", $"{outputDir}/extensions.jar");
root = "./source/com.google.android.play/feature.delivery.extensions/";
RunGradle(root, "build");
outputDir = "./externals/com.xamarin.google.android.play.feature.delivery.extensions/";
EnsureDirectoryExists(outputDir);
CleanDirectories(outputDir);
CopyFileToDirectory($"{root}/extensions-aar/build/outputs/aar/extensions-aar-release.aar", outputDir);
Unzip($"{outputDir}/extensions-aar-release.aar", outputDir);
MoveFile($"{outputDir}/classes.jar", $"{outputDir}/extensions.jar");
});


Expand Down Expand Up @@ -913,6 +937,7 @@ Task("samples-only-dotnet")
"./samples/dotnet/BuildAllDotNet.sln",
"./samples/dotnet/BuildAllMauiApp.sln",
"./samples/dotnet/BuildAllXamarinForms.sln",
"./samples/dotnet/BuildAllPlayDotNet.sln",
};
DotNetMSBuildSettings settings = null;
Expand Down Expand Up @@ -982,7 +1007,7 @@ Task("allbindingprojectrefs")
generateTargets("./output/Xamarin.Firebase.*.nupkg", "./output/FirebasePackages.targets");
generateTargets("./output/Xamarin.GooglePlayServices.*.nupkg", "./output/PlayServicesPackages.targets");
generateTargets("./output/Xamarin.Google.MLKit.*.nupkg", "./output/Google.MLKit.targets");
generateTargets("./output/Xamarin.Google.Play.*.nupkg", "./output/Google.Play.targets");
generateTargets("./output/Xamarin.Google.Android.Play.*.nupkg", "./output/Google.Play.targets");
});


Expand Down
2 changes: 2 additions & 0 deletions published-namespaces.txt
Expand Up @@ -564,6 +564,7 @@ Xamarin.Google.Android.DataTransport.Runtime.Synchronization
Xamarin.Google.Android.DataTransport.Runtime.Time
Xamarin.Google.Android.DataTransport.Runtime.Util
Xamarin.Google.Android.Odml.Image
Xamarin.Google.Android.Play.Asset.Delivery
Xamarin.Google.Android.Play.Core
Xamarin.Google.Android.Play.Core.AppUpdate
Xamarin.Google.Android.Play.Core.AppUpdate.Install
Expand All @@ -587,6 +588,7 @@ Xamarin.Google.Android.Play.Core.SplitInstall
Xamarin.Google.Android.Play.Core.SplitInstall.Model
Xamarin.Google.Android.Play.Core.SplitInstall.Testing
Xamarin.Google.Android.Play.Core.Tasks
Xamarin.Google.Android.Play.Feature.Delivery
Xamarin.Google.Android.Recaptcha
Xamarin.Google.ErrorProne.Annotations
Xamarin.Google.ErrorProne.Annotations.Concurrent
Expand Down
2 changes: 2 additions & 0 deletions samples/NuGet.config
Expand Up @@ -14,6 +14,7 @@
<packageSourceMapping>
<!-- key value for <packageSource> should match key values from <packageSources> element -->
<packageSource key="Local Output">
<package pattern="Xamarin.Chromium.*" />
<package pattern="Xamarin.GooglePlayServices.*" />
<package pattern="Xamarin.Firebase.*" />
<package pattern="Xamarin.Google.MLKit.*" />
Expand Down Expand Up @@ -41,6 +42,7 @@
<package pattern="Xamarin.CodeHaus.Mojo.*" />
<package pattern="Xamarin.GoogleAndroid.*" />
<package pattern="Xamarin.Google.Android.*" />
<package pattern="Xamarin.Google.Android.Play.*" />
</packageSource>
<packageSource key="nuget.org">
<package pattern="Xamarin.GooglePlayServices.*" />
Expand Down
27 changes: 27 additions & 0 deletions samples/dotnet/BuildAllPlayDotNet.sln
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28606.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuildAllPlayDotNet", "BuildAllPlayDotNet\BuildAllPlayDotNet.csproj", "{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}.Release|Any CPU.Build.0 = Release|Any CPU
{7CA41D33-5A33-4968-AEBD-9A76B7E83BB7}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {47AA0B75-DCFB-4D55-AFC5-DB072FDDCCB1}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions samples/dotnet/BuildAllPlayDotNet/AndroidManifest.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" >
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
@@ -0,0 +1 @@
FastFollow Asset
@@ -0,0 +1 @@
Install Time Asset
1 change: 1 addition & 0 deletions samples/dotnet/BuildAllPlayDotNet/Assets/OnDemandAsset.txt
@@ -0,0 +1 @@
OnDemand Asset
42 changes: 42 additions & 0 deletions samples/dotnet/BuildAllPlayDotNet/BuildAllPlayDotNet.csproj
@@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(_DefaultDotNetSampleTargetFrameworks)</TargetFrameworks>
<AndroidPackageFormat>aab</AndroidPackageFormat>
<!-- Some packages specify a minimum of 28 (eg: Xamarin.AndroidX.HeifWriter) -->

<SupportedOSPlatformVersion>29</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationId>com.companyname.BuildAllDotNet</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<AndroidBundleToolExtraArgs Condition=" '$(Configuration)' == 'Debug' ">--local-testing</AndroidBundleToolExtraArgs>

<!-- Override property from 'Directory.Build.props' -->
<!--
<AndroidGenerateResourceDesigner>true</AndroidGenerateResourceDesigner>
-->

<!-- Prevent linking, since that would remove ~everything -->
<!--
<PublishTrimmed>False</PublishTrimmed>
<RunAOTCompilation>False</RunAOTCompilation>
-->

</PropertyGroup>

<ItemGroup>
<AndroidAsset Update="Assets/InstallTimeAsset.txt" AssetPack="installtimepack" DeliveryType="InstallTime" />
<AndroidAsset Update="Assets/FastFollowAsset.txt" AssetPack="fastfollowpack" DeliveryType="FastFollow" />
<AndroidAsset Update="Assets/OnDemandAsset.txt" AssetPack="ondemandpack" DeliveryType="OnDemand" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Google.Android.Play.Asset.Delivery" Version="2.1.0.5" />
<!-- Remove the old Conflicting Package otherwise this causes build errors. -->
<PackageReference Remove="Xamarin.Google.Android.Play.Core" Version="2.1.0.5" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions samples/dotnet/BuildAllPlayDotNet/Directory.Build.targets
@@ -0,0 +1,3 @@
<!-- This is empty to prevent picking up any parent Directory.Build.targets. -->
<Project>
</Project>
53 changes: 53 additions & 0 deletions samples/dotnet/BuildAllPlayDotNet/MainActivity.cs
@@ -0,0 +1,53 @@
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;

using Xamarin.Google.Android.Play.Core.AssetPacks;
using Xamarin.Google.Android.Play.Core.AssetPacks.Model;
using static Xamarin.Google.Android.Play.Core.AssetPacks.AssetPackStateUpdateListenerWrapper;

namespace BuildAllPlayDotNet;

[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
IAssetPackManager? assetPackManager;
AssetPackStateUpdateListenerWrapper? listener;

protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

assetPackManager = AssetPackManagerFactory.GetInstance (this);
// Create our Wrapper and set up the event handler.
listener = new AssetPackStateUpdateListenerWrapper();
listener.StateUpdate += Listener_StateUpdate;

var location = assetPackManager.GetPackLocation ("installtimepack");
assetPackManager.Fetch (new string[] {"fastfollowpack"});
assetPackManager.Fetch (new string[] {"ondemandpack"});
}

void Listener_StateUpdate(object? sender, AssetPackStateEventArgs e)
{
var status = e.State.Status();
Android.Util.Log.Info ("Listener_StateUpdate", status.ToString ());
}

protected override void OnResume()
{
// regsiter our Listener Wrapper with the IAssetPackManager so we get feedback.
assetPackManager?.RegisterListener(listener?.Listener);
base.OnResume();
}

protected override void OnPause()
{
assetPackManager?.UnregisterListener(listener?.Listener);
base.OnPause();
}
}
44 changes: 44 additions & 0 deletions samples/dotnet/BuildAllPlayDotNet/Resources/AboutResources.txt
@@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.xml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable/
icon.png

layout/
main.xml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "Resource"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the Resource class would expose:

public class Resource {
public class Drawable {
public const int icon = 0x123;
}

public class Layout {
public const int main = 0x456;
}

public class Strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or
Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string
to reference the first string in the dictionary file values/strings.xml.
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_text"
/>
</RelativeLayout>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#2C3E50</color>
</resources>
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">BuildAllDotNet</string>
<string name="app_text">Hello, Android!</string>
</resources>
8 changes: 8 additions & 0 deletions source/GooglePlayServicesProject.cshtml
Expand Up @@ -256,6 +256,14 @@
{
<EmbeddedJar Include="..\..\externals\com.xamarin.google.android.play.core.extensions\extensions.jar" />
}
@if (@Model.NuGetPackageId == "Xamarin.Google.Android.Play.Asset.Delivery")
{
<EmbeddedJar Include="..\..\externals\com.xamarin.google.android.play.asset.delivery.extensions\extensions.jar" />
}
@if (@Model.NuGetPackageId == "Xamarin.Google.Android.Play.Feature.Delivery")
{
<EmbeddedJar Include="..\..\externals\com.xamarin.google.android.play.feature.delivery.extensions\extensions.jar" />
}
</ItemGroup>
}
<ItemGroup>
Expand Down
Expand Up @@ -17,5 +17,10 @@
>
Xamarin.Google.Android.Play.Core.AssetPacks.Model
</attr>
<attr
path="/api/package[@name='xamarin.google.android.play.core.assetpacks']"
name="managedName">
Xamarin.Google.Android.Play.Core.AssetPacks
</attr>

</metadata>
Expand Up @@ -5,4 +5,16 @@
>
Java.Lang.Object
</attr>
<attr
path="/api/package[@name='com.google.android.play.core.assetpacks']/class[@name='NativeAssetPackStateUpdateListener']/method[@name='onStateUpdate' and count(parameter)=1 and parameter[1][@type='com.google.android.play.core.assetpacks.AssetPackState']]/parameter[1]"
name="managedType"
>
Java.Lang.Object
</attr>
<attr
path="/api/package[@name='xamarin.google.android.play.core.assetpacks']/interface[@name='AssetPackStateUpdateListenerWrapper.AssetPackStateListener']/method[@name='OnStateUpdate']/parameter[@name='p0']"
name="name"
>
state
</attr>
</metadata>
@@ -0,0 +1 @@
/build
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
@@ -0,0 +1 @@
/build

0 comments on commit 204f71c

Please sign in to comment.