Skip to content

Commit 1f8822b

Browse files
authored
[tests] Add @(AndroidGradleProject) deploy test (#9429)
Adds a test which will install and run a `@(AndroidGradleProject)`- style binding project.
1 parent 640fbf9 commit 1f8822b

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/AndroidGradleModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AndroidGradleModule
1717

1818
public int CompileSdk { get; set; } = XABuildConfig.AndroidDefaultTargetDotnetApiLevel;
1919

20-
public int MinSdk { get; set; } = 21;
20+
public int MinSdk { get; set; } = XABuildConfig.AndroidMinimumDotNetApiLevel;
2121

2222
public bool IsApplication { get; set; } = false;
2323

tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6-
using System.Text.RegularExpressions;
76
using System.Xml.Linq;
87
using System.Xml.XPath;
98
using Mono.Cecil;
109
using NUnit.Framework;
10+
11+
using Xamarin.Android.Tools;
1112
using Xamarin.ProjectTools;
1213

1314
namespace Xamarin.Android.Build.Tests
@@ -1126,5 +1127,127 @@ public void MicrosoftIntune ([Values (false, true)] bool isRelease)
11261127
Path.Combine (Root, builder.ProjectDirectory, "logcat.log"), 30);
11271128
Assert.IsTrue (didLaunch, "Activity should have started.");
11281129
}
1130+
1131+
[Test]
1132+
public void GradleFBProj ([Values (false, true)] bool isRelease)
1133+
{
1134+
var moduleName = "Library";
1135+
var gradleTestProjectDir = Path.Combine (Root, "temp", "gradle", TestName);
1136+
var gradleModule = new AndroidGradleModule (Path.Combine (gradleTestProjectDir, moduleName));
1137+
gradleModule.PackageName = "com.microsoft.mauifacebook";
1138+
gradleModule.BuildGradleFileContent = $@"
1139+
plugins {{
1140+
id(""com.android.library"")
1141+
}}
1142+
android {{
1143+
namespace = ""{gradleModule.PackageName}""
1144+
compileSdk = {XABuildConfig.AndroidDefaultTargetDotnetApiLevel}
1145+
defaultConfig {{
1146+
minSdk = {XABuildConfig.AndroidMinimumDotNetApiLevel}
1147+
}}
1148+
}}
1149+
dependencies {{
1150+
implementation(""androidx.appcompat:appcompat:1.7.0"")
1151+
implementation(""com.google.android.material:material:1.11.0"")
1152+
implementation(""com.facebook.android:facebook-android-sdk:17.0.2"")
1153+
}}
1154+
";
1155+
gradleModule.JavaSources.Add (new AndroidItem.AndroidJavaSource ("FacebookSdk.java") {
1156+
TextContent = () => $@"
1157+
package com.microsoft.mauifacebook;
1158+
public class FacebookSdk {{
1159+
static com.facebook.appevents.AppEventsLogger _logger;
1160+
public static void initializeSDK(android.app.Activity activity, Boolean isDebug) {{
1161+
android.app.Application application = activity.getApplication();
1162+
com.facebook.FacebookSdk.sdkInitialize(application);
1163+
com.facebook.FacebookSdk.addLoggingBehavior(com.facebook.LoggingBehavior.APP_EVENTS);
1164+
com.facebook.appevents.AppEventsLogger.activateApp(application);
1165+
_logger = com.facebook.appevents.AppEventsLogger.newLogger(activity);
1166+
}}
1167+
public static void logEvent(String eventName) {{
1168+
_logger.logEvent(eventName);
1169+
}}
1170+
}}
1171+
",
1172+
});
1173+
1174+
var gradleProject = new AndroidGradleProject (gradleTestProjectDir) {
1175+
Modules = {
1176+
gradleModule,
1177+
},
1178+
};
1179+
gradleProject.Create ();
1180+
1181+
var proj = new XamarinAndroidApplicationProject {
1182+
IsRelease = isRelease,
1183+
ExtraNuGetConfigSources = {
1184+
"https://api.nuget.org/v3/index.json",
1185+
},
1186+
OtherBuildItems = {
1187+
new AndroidItem.TransformFile ("Transforms\\Metadata.xml") {
1188+
TextContent = () => $@"<metadata><attr path=""/api/package[@name='{gradleModule.PackageName}']"" name=""managedName"">Facebook</attr></metadata>",
1189+
},
1190+
new BuildItem (KnownProperties.AndroidGradleProject, gradleProject.BuildFilePath) {
1191+
Metadata = {
1192+
{ "ModuleName", moduleName },
1193+
},
1194+
},
1195+
new BuildItem ("AndroidMavenLibrary", "com.facebook.android:facebook-core") {
1196+
Metadata = {
1197+
{ "Version", "17.0.2" },
1198+
{ "Bind", "false" },
1199+
},
1200+
},
1201+
new BuildItem ("AndroidMavenLibrary", "com.facebook.android:facebook-bolts") {
1202+
Metadata = {
1203+
{ "Version", "17.0.2" },
1204+
{ "Bind", "false" },
1205+
},
1206+
},
1207+
},
1208+
PackageReferences = {
1209+
new Package {
1210+
Id = "Xamarin.AndroidX.AppCompat",
1211+
Version = "1.7.0.3",
1212+
},
1213+
new Package {
1214+
Id = "Xamarin.AndroidX.Annotation",
1215+
Version = "1.8.2.1",
1216+
},
1217+
new Package {
1218+
Id = "Xamarin.AndroidX.Legacy.Support.Core.Utils",
1219+
Version = "1.0.0.29",
1220+
},
1221+
new Package {
1222+
Id = "Xamarin.Google.Android.InstallReferrer",
1223+
Version = "1.1.2.6",
1224+
},
1225+
new Package {
1226+
Id = "Xamarin.AndroidX.Core.Core.Ktx",
1227+
Version = "1.13.1.5",
1228+
},
1229+
new Package {
1230+
Id = "Xamarin.Kotlin.StdLib",
1231+
Version = "2.0.21",
1232+
},
1233+
},
1234+
};
1235+
proj.MainActivity = proj.DefaultMainActivity.Replace ("//${AFTER_ONCREATE}", @"
1236+
Facebook.FacebookSdk.InitializeSDK(this, Java.Lang.Boolean.True);
1237+
Facebook.FacebookSdk.LogEvent(""TestFacebook"");
1238+
");
1239+
proj.AndroidManifest =@"<?xml version=""1.0"" encoding=""utf-8""?>
1240+
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" xmlns:tools=""http://schemas.android.com/tools"" android:versionCode=""1"" android:versionName=""1.0"" package=""com.xamarin.gradleproj"">
1241+
<application android:label=""UnnamedProject"">
1242+
<meta-data android:name=""com.facebook.sdk.ApplicationId"" android:value=""appid""/>
1243+
<meta-data android:name=""com.facebook.sdk.ClientToken"" android:value=""token""/>
1244+
</application>
1245+
</manifest>";
1246+
1247+
using var builder = CreateApkBuilder ();
1248+
Assert.IsTrue (builder.Build (proj));
1249+
RunProjectAndAssert (proj, builder);
1250+
}
1251+
11291252
}
11301253
}

0 commit comments

Comments
 (0)