Skip to content

Commit

Permalink
[Android] Bump target SDK version in Android manifest (dotnet#80923)
Browse files Browse the repository at this point in the history
* Set target SDK version

* Update Android docs

* Disable tests that do not pass with target API 31

* Check if the installed Android SDK is up-to-date

* Update skip explanation

* Use latest SDK

* Disable failing System.Net.Security test
  • Loading branch information
simonrozsival authored Jan 24, 2023
1 parent 655973d commit 828edfb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
8 changes: 4 additions & 4 deletions docs/workflow/testing/libraries/testing-android.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Android SDK and NDK can be automatically installed via the following script:
set -e

NDK_VER=r23c
SDK_VER=6200805_latest
SDK_API_LEVEL=29
SDK_BUILD_TOOLS=29.0.3
SDK_VER=9123335_latest
SDK_API_LEVEL=33
SDK_BUILD_TOOLS=33.0.1

if [[ "$OSTYPE" == "darwin"* ]]; then
HOST_OS=darwin
Expand Down Expand Up @@ -63,7 +63,7 @@ Android Studio offers a convenient UI:
Before running a build you might want to set the Android SDK and NDK environment variables:
```
export ANDROID_SDK_ROOT=<PATH-TO-ANDROID-SDK>
export ANDROID_NDK_ROOT=<PATH-TO-ANDROID-NDK>
export ANDROID_NDK_ROOT=<PATH-TO-ANDROID-NDK>
```

Now we're ready to build everything for Android:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ public void ProcessStart_UseShellExecute_OnWindows_DoesNotThrow(bool isFolder)
[ActiveIssue("https://github.com/dotnet/runtime/issues/34685", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[InlineData(true), InlineData(false)]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.tvOS, "Not supported on iOS and tvOS.")]
[SkipOnPlatform(TestPlatforms.Android, "Android doesn't allow executing custom shell scripts")]
public void ProcessStart_UseShellExecute_Executes(bool filenameAsUrl)
{
string filename = WriteScriptFile(TestDirectory, GetTestFileName(), returnValue: 42);
Expand Down Expand Up @@ -374,6 +375,7 @@ public void ProcessStart_UseShellExecute_ExecuteOrder()
nameof(PlatformDetection.IsNotAppSandbox))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34685", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.tvOS, "Not supported on iOS and tvOS.")]
[SkipOnPlatform(TestPlatforms.Android, "Android doesn't allow executing custom shell scripts")]
public void ProcessStart_UseShellExecute_WorkingDirectory()
{
// Create a directory that will ProcessStartInfo.WorkingDirectory
Expand Down Expand Up @@ -2561,7 +2563,7 @@ public void NonElevatedUser_QueryProcessNameOfSystemProcess()
{
// returns the username of the owner of the process or null if the username can't be queried.
// for services.exe, this will be null.
string? servicesUser = Helpers.GetProcessUserName(p);
string? servicesUser = Helpers.GetProcessUserName(p);

// this isn't really verifying that services.exe is owned by SYSTEM, but we are sure it is not owned by the current user.
if (servicesUser != currentProcessUser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public async Task DefaultConnect_EndToEnd_Ok(string host)
[InlineData(true)]
[InlineData(false)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/70981", TestPlatforms.OSX)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/68206", TestPlatforms.Android)]
public Task ConnectWithRevocation_WithCallback(bool checkRevocation)
{
X509RevocationMode mode = checkRevocation ? X509RevocationMode.Online : X509RevocationMode.NoCheck;
Expand Down
3 changes: 3 additions & 0 deletions src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class AndroidAppBuilderTask : Task

public string? MinApiLevel { get; set; }

public string? TargetApiLevel { get; set; }

public string? BuildApiLevel { get; set; }

public string? BuildToolsVersion { get; set; }
Expand Down Expand Up @@ -106,6 +108,7 @@ public override bool Execute()
apkBuilder.AndroidSdk = AndroidSdk;
apkBuilder.AndroidNdk = AndroidNdk;
apkBuilder.MinApiLevel = MinApiLevel;
apkBuilder.TargetApiLevel = TargetApiLevel;
apkBuilder.BuildApiLevel = BuildApiLevel;
apkBuilder.BuildToolsVersion = BuildToolsVersion;
apkBuilder.StripDebugSymbols = StripDebugSymbols;
Expand Down
27 changes: 20 additions & 7 deletions src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
public class ApkBuilder
{
private const string DefaultMinApiLevel = "21";
private const string DefaultTargetApiLevel = "31";

public string? ProjectName { get; set; }
public string? AppDir { get; set; }
public string? AndroidNdk { get; set; }
public string? AndroidSdk { get; set; }
public string? MinApiLevel { get; set; }
public string? TargetApiLevel { get; set; }
public string? BuildApiLevel { get; set; }
public string? BuildToolsVersion { get; set; }
public string OutputDir { get; set; } = ""!;
Expand Down Expand Up @@ -118,14 +120,24 @@ public ApkBuilder(TaskLoggingHelper logger)
if (string.IsNullOrEmpty(MinApiLevel))
MinApiLevel = DefaultMinApiLevel;

// make sure BuildApiLevel >= MinApiLevel
if (string.IsNullOrEmpty(TargetApiLevel))
TargetApiLevel = DefaultTargetApiLevel;

// make sure BuildApiLevel >= MinApiLevel and BuildApiLevel >= TargetApiLevel
// only if these api levels are not "preview" (not integers)
if (int.TryParse(BuildApiLevel, out int intApi) &&
int.TryParse(MinApiLevel, out int intMinApi) &&
intApi < intMinApi)
if (int.TryParse(BuildApiLevel, out int intApi))
{
throw new ArgumentException($"BuildApiLevel={BuildApiLevel} <= MinApiLevel={MinApiLevel}. " +
"Make sure you've downloaded some recent build-tools in Android SDK");
if (int.TryParse(MinApiLevel, out int intMinApi) && intApi < intMinApi)
{
throw new ArgumentException($"BuildApiLevel={BuildApiLevel} < MinApiLevel={MinApiLevel}. " +
"Make sure you've downloaded some recent build-tools in Android SDK");
}

if (int.TryParse(TargetApiLevel, out int intTargetApi) && intApi < intTargetApi)
{
throw new ArgumentException($"BuildApiLevel={BuildApiLevel} < TargetApiLevel={TargetApiLevel}. " +
"Make sure you've downloaded some recent build-tools in Android SDK");
}
}

string buildToolsFolder = Path.Combine(AndroidSdk, "build-tools", BuildToolsVersion);
Expand Down Expand Up @@ -397,7 +409,8 @@ public ApkBuilder(TaskLoggingHelper logger)
File.WriteAllText(Path.Combine(OutputDir, "AndroidManifest.xml"),
Utils.GetEmbeddedResource("AndroidManifest.xml")
.Replace("%PackageName%", packageId)
.Replace("%MinSdkLevel%", MinApiLevel));
.Replace("%MinSdkLevel%", MinApiLevel)
.Replace("%TargetSdkVersion%", TargetApiLevel));

string javaCompilerArgs = $"-d obj -classpath src -bootclasspath {androidJar} -source 1.8 -target 1.8 ";
Utils.RunProcess(logger, javac, javaCompilerArgs + javaActivityPath, workingDir: OutputDir);
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="%PackageName%"
a:versionCode="1"
a:versionName="1.0">
<uses-sdk a:minSdkVersion="%MinSdkLevel%" />
<uses-sdk a:minSdkVersion="%MinSdkLevel%" a:targetSdkVersion="%TargetSdkVersion%" />
<uses-permission a:name="android.permission.INTERNET"/>
<uses-permission a:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission a:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Expand Down

0 comments on commit 828edfb

Please sign in to comment.