Skip to content

Commit

Permalink
fix: Going back to consuming the Android SDK as .jar (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes authored Aug 10, 2023
1 parent ca26cd0 commit 20e49e1
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 698 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Resolved the internal dependency issue with the Android SDK that lead to build time issues like `runtime.jar is missing` and `ClassNotFoundException` during runtime ([#1417](https://github.com/getsentry/sentry-unity/pull/1417))
- The SDK now handles proguardfiles sections indicated by both `consumerProguardFiles` and `proguardFiles` ([#1401](https://github.com/getsentry/sentry-unity/pull/1401))

### Dependencies
Expand Down
11 changes: 8 additions & 3 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,15 @@ Expected to exist:
<Error Condition="!Exists('$(SentryAndroidRoot)')" Text="Couldn't find the Android root at $(SentryAndroidRoot)." />
<Message Importance="High" Text="Building Sentry Android SDK." />

<Exec WorkingDirectory="$(SentryAndroidRoot)" EnvironmentVariables="JAVA_HOME=$(JAVA_HOME_17_X64)"
Command="./gradlew -PsentryAndroidSdkName=sentry.native.android.unity sentry-android-core:publishAllPublicationsToUnityMavenRepository sentry-android-ndk:publishAllPublicationsToUnityMavenRepository sentry:publishAllPublicationsToUnityMavenRepository sentry-android:publishAllPublicationsToUnityMavenRepository --no-daemon --stacktrace --warning-mode none" />
<Exec WorkingDirectory="$(SentryAndroidRoot)" EnvironmentVariables="JAVA_HOME=$(JAVA_HOME_17_X64)" Command="./gradlew -PsentryAndroidSdkName=sentry.native.android.unity :sentry-android-core:assembleRelease :sentry-android-ndk:assembleRelease :sentry:jar --no-daemon --stacktrace --warning-mode none" />

<Exec WorkingDirectory="$(SentryAndroidRoot)" Command="cp -r build/unityMaven $(SentryAndroidArtifactsDestination)" />
<ItemGroup>
<AndroidSdkArtifacts Include="$(SentryAndroidRoot)sentry-android-ndk/build/outputs/aar/sentry-android-ndk-release.aar" />
<AndroidSdkArtifacts Include="$(SentryAndroidRoot)sentry-android-core/build/outputs/aar/sentry-android-core-release.aar" />
</ItemGroup>

<Copy SourceFiles="@(AndroidSdkArtifacts)" DestinationFiles="@(AndroidSdkArtifacts->'$(SentryAndroidArtifactsDestination)%(RecursiveDir)%(Filename)%(Extension)')" />
<Exec WorkingDirectory="$(SentryAndroidRoot)" Command="cp sentry/build/libs/sentry*.jar $(SentryAndroidArtifactsDestination)sentry.jar" />

<Error Condition="!Exists('$(SentryAndroidArtifactsDestination)')" Text="Failed to build the Android SDK." />
</Target>
Expand Down
2 changes: 1 addition & 1 deletion modules/sentry-java
Submodule sentry-java updated 137 files
83 changes: 61 additions & 22 deletions src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using Sentry.Extensibility;
using Sentry.Unity.Editor.ConfigurationWindow;
Expand Down Expand Up @@ -31,6 +32,10 @@ public class AndroidManifestConfiguration
private readonly bool _isDevelopmentBuild;
private readonly ScriptingImplementation _scriptingImplementation;

public const string SDKDependencies = @"
implementation(name: 'sentry-android-ndk-release', ext:'aar')
implementation(name: 'sentry-android-core-release', ext:'aar')";

public AndroidManifestConfiguration()
: this(
SentryScriptableObject.ConfiguredBuildTimeOptions,
Expand Down Expand Up @@ -69,8 +74,8 @@ public void OnPostGenerateGradleAndroidProject(string basePath)
var unityProjectPath = Directory.GetParent(Application.dataPath).FullName;
var gradleProjectPath = Directory.GetParent(basePath).FullName;

SetupAndroidSdk(unityProjectPath, gradleProjectPath);
SetupGradle(gradleProjectPath);
CopyAndroidSdkToGradleProject(unityProjectPath, gradleProjectPath);
AddAndroidSdkDependencies(gradleProjectPath);
SetupSymbolsUpload(unityProjectPath, gradleProjectPath);
SetupProguard(gradleProjectPath);
}
Expand Down Expand Up @@ -244,47 +249,81 @@ private void SetupProguard(string gradleProjectPath)
}
}

private void SetupAndroidSdk(string unityProjectPath, string gradleProjectPath)
internal void CopyAndroidSdkToGradleProject(string unityProjectPath, string gradlePath)
{
var sdkSetup = new AndroidSdkSetup(_logger, unityProjectPath, gradleProjectPath);
var nativeSupportEnabled = _options is { Enabled: true, AndroidNativeSupportEnabled: true };
var androidSdkPath = Path.Combine(unityProjectPath, "Packages", SentryPackageInfo.GetName(), "Plugins", "Android", "Sentry~");
var targetPath = Path.Combine(gradlePath, "unityLibrary", "libs");

try
if (_options is { Enabled: true, AndroidNativeSupportEnabled: true })
{
if (nativeSupportEnabled)
if (!Directory.Exists(androidSdkPath))
{
sdkSetup.AddAndroidSdk();
throw new DirectoryNotFoundException($"Failed to find the Android SDK at '{androidSdkPath}'.");
}
else

_logger.LogInfo("Copying the Android SDK to '{0}'.", gradlePath);
foreach (var file in Directory.GetFiles(androidSdkPath))
{
sdkSetup.RemoveAndroidSdk();
var destinationFile = Path.Combine(targetPath, Path.GetFileName(file));
if (!File.Exists(destinationFile))
{
File.Copy(file, destinationFile);
}
}
}
catch (Exception e)
else
{
_logger.LogError($"Failed to {(nativeSupportEnabled ? "add" : "remove")} the Android SDK to the gradle project", e);
_logger.LogInfo("Removing the Android SDK from the output project.");
foreach (var file in Directory.GetFiles(androidSdkPath))
{
var fileToDelete = Path.Combine(targetPath, Path.GetFileName(file));
if (File.Exists(fileToDelete))
{
File.Delete(fileToDelete);
}
}
}
}

private void SetupGradle(string gradleProjectPath)
internal void AddAndroidSdkDependencies(string gradleProjectPath)
{
var gradleSetup = new GradleSetup(_logger, gradleProjectPath);
var nativeSupportEnabled = _options is { Enabled: true, AndroidNativeSupportEnabled: true };
const string regexPattern = @"(dependencies\s\{\n).+";

try
var gradleFilePath = Path.Combine(gradleProjectPath, "unityLibrary", "build.gradle");
if (!File.Exists(gradleFilePath))
{
if (nativeSupportEnabled)
throw new FileNotFoundException($"Failed to find 'build.gradle' at '{gradleFilePath}'.");
}

var gradle = File.ReadAllText(gradleFilePath);

if (_options is { Enabled: true, AndroidNativeSupportEnabled: true })
{
if (gradle.Contains(SDKDependencies))
{
gradleSetup.UpdateGradleProject();
_logger.LogDebug("Android SDK dependencies already added. Skipping.");
return;
}
else

_logger.LogInfo("Adding Android SDK dependencies to 'build.gradle'.");

var regex = new Regex(regexPattern);
var match = regex.Match(gradle);
if (!match.Success)
{
gradleSetup.ClearGradleProject();
throw new ArgumentException($"Failed to add Sentry Android dependencies to 'build.gradle'.\n{gradle}", nameof(gradle));
}

File.WriteAllText(gradleFilePath, gradle.Insert(match.Index + match.Length, SDKDependencies));
}
catch (Exception e)
else
{
_logger.LogError($"Failed to {(nativeSupportEnabled ? "modify" : "clear")} the 'build.gradle' files.", e);
if (gradle.Contains(SDKDependencies))
{
_logger.LogInfo("Android SDK dependencies have previously been added. Removing them.");

File.WriteAllText(gradleFilePath, gradle.Replace(SDKDependencies, ""));
}
}
}

Expand Down
45 changes: 0 additions & 45 deletions src/Sentry.Unity.Editor/Android/AndroidSdkSetup.cs

This file was deleted.

Loading

0 comments on commit 20e49e1

Please sign in to comment.