-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathAndroidGradleProject.cs
More file actions
122 lines (108 loc) · 3.73 KB
/
AndroidGradleProject.cs
File metadata and controls
122 lines (108 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.IO;
using Xamarin.Android.Tools;
namespace Xamarin.ProjectTools
{
public class AndroidGradleProject
{
public string ProjectDirectory { get; private set; } = string.Empty;
public List<AndroidGradleModule> Modules { get; set; } = new List<AndroidGradleModule> ();
public string BuildFilePath => Path.Combine (ProjectDirectory, "build.gradle.kts");
/// <summary>
/// Android Gradle Plugin version (e.g., "8.5.0", "9.1.1")
/// </summary>
public string AgpVersion { get; set; } = "9.1.1";
/// <summary>
/// Gradle wrapper version to use (e.g., "8.12", "9.0").
/// Defaults to "9.3.1" (minimum required by AGP 9.1.1). If set to null or empty, the Gradle wrapper version generated by gradle init is used.
/// </summary>
public string? GradleVersion { get; set; } = "9.3.1";
GradleCLI gradleCLI = new GradleCLI ();
public AndroidGradleProject (string directory)
{
ProjectDirectory = directory;
}
public void Create ()
{
Directory.CreateDirectory (ProjectDirectory);
gradleCLI.Init (ProjectDirectory);
var settingsFile = Path.Combine (ProjectDirectory, "settings.gradle.kts");
File.WriteAllText (settingsFile, settings_gradle_kts_content);
File.WriteAllText (BuildFilePath, GetBuildGradleKtsContent ());
foreach (var module in Modules) {
module.Create ();
File.AppendAllText (settingsFile, $"{Environment.NewLine}include(\":{module.Name}\")");
}
File.AppendAllText (Path.Combine (ProjectDirectory, "gradle.properties"), "android.useAndroidX=true");
// Update Gradle wrapper version if specified
if (!string.IsNullOrEmpty (GradleVersion)) {
var wrapperPropertiesPath = Path.Combine (ProjectDirectory, "gradle", "wrapper", "gradle-wrapper.properties");
if (File.Exists (wrapperPropertiesPath)) {
var content = File.ReadAllText (wrapperPropertiesPath);
// Replace the distribution URL with the specified Gradle version
content = System.Text.RegularExpressions.Regex.Replace (
content,
@"distributionUrl=.*",
$@"distributionUrl=https\://services.gradle.org/distributions/gradle-{GradleVersion}-bin.zip"
);
File.WriteAllText (wrapperPropertiesPath, content);
}
}
}
public static AndroidGradleProject CreateDefault (string projectDir, bool isApplication = false)
{
var proj = new AndroidGradleProject (projectDir) {
Modules = {
new AndroidGradleModule (Path.Combine (projectDir, "TestModule")) {
IsApplication = isApplication,
},
},
};
proj.Create ();
return proj;
}
/// <summary>
/// Creates a default Gradle project with specified AGP and Gradle versions.
/// </summary>
public static AndroidGradleProject CreateDefault (string projectDir, string agpVersion, string? gradleVersion, bool isApplication = false, int? compileSdk = null)
{
var proj = new AndroidGradleProject (projectDir) {
AgpVersion = agpVersion,
GradleVersion = gradleVersion,
Modules = {
new AndroidGradleModule (Path.Combine (projectDir, "TestModule")) {
IsApplication = isApplication,
CompileSdk = compileSdk ?? XABuildConfig.AndroidDefaultTargetDotnetApiLevel.Major,
},
},
};
proj.Create ();
return proj;
}
string GetBuildGradleKtsContent () =>
$@"
plugins {{
id(""com.android.application"") version ""{AgpVersion}"" apply false
id(""com.android.library"") version ""{AgpVersion}"" apply false
}}
";
const string settings_gradle_kts_content =
@"
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
";
}
}