Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.android.core;

import android.content.Context;
import android.os.Build;
import io.sentry.core.ILogger;
import io.sentry.core.SentryLevel;
import io.sentry.core.SentryOptions;
Expand All @@ -22,30 +23,26 @@ static void init(SentryOptions options, Context context, ILogger logger) {
ManifestMetadataReader.applyMetadata(context, options);
createsEnvelopeDirPath(options, context);

if (options.isEnableNdk()) {
options.addEventProcessor(new DefaultAndroidEventProcessor(context, options));
options.setSerializer(new AndroidSerializer(options.getLogger()));

if (options.isEnableNdk() && isNdkAvailable()) {
try {
// TODO: Create Integrations interface and use that to initialize NDK
Class<?> cls = Class.forName("io.sentry.android.ndk.SentryNdk");

// TODO: temporary hack
String cacheDirPath = context.getCacheDir().getAbsolutePath() + "/sentry-envelopes";
File f = new File(cacheDirPath);
f.mkdirs();

Method method = cls.getMethod("init", SentryOptions.class, String.class);
Object[] args = new Object[2];
Method method = cls.getMethod("init", SentryOptions.class);
Object[] args = new Object[1];
args[0] = options;
args[1] = cacheDirPath;
method.invoke(null, args);
} catch (ClassNotFoundException exc) {
options.getLogger().log(SentryLevel.ERROR, "Failed to load SentryNdk.");
} catch (ClassNotFoundException e) {
options.setEnableNdk(false);
options.getLogger().log(SentryLevel.ERROR, "Failed to load SentryNdk.", e);
} catch (Exception e) {
options.setEnableNdk(false);
options.getLogger().log(SentryLevel.ERROR, "Failed to initialize SentryNdk.", e);
}
}

options.addEventProcessor(new DefaultAndroidEventProcessor(context, options));
options.setSerializer(new AndroidSerializer(options.getLogger()));
}

private static void createsEnvelopeDirPath(SentryOptions options, Context context) {
Expand All @@ -56,4 +53,8 @@ private static void createsEnvelopeDirPath(SentryOptions options, Context contex
}
options.setCacheDirPath(envelopesDir.getAbsolutePath());
}

private static boolean isNdkAvailable() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we settling on the fact that NDK support will not be available on API < 21?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ninniuz for the first release, probably yes, we have a system blocker and trying to find a workaround.
JFYI dl_iterate_phdr method only available API >= 21
https://android.googlesource.com/platform/bionic/+/master/docs/status.md

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, tried to build the NDK feature branch on API 14 and got an error on that method :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might have a workaround, parsing /proc/PID/maps by hand and getting what we need.
but still thinking if it's a good idea.

return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ManifestMetadataReader {
static final String DSN_KEY = "io.sentry.dsn";
static final String DEBUG_KEY = "io.sentry.debug";
static final String AUTO_INIT = "io.sentry.auto-init";
static final String ENABLE_NDK = "io.sentry.ndk";

public static void applyMetadata(Context context, SentryOptions options) {
if (context == null) throw new IllegalArgumentException("The application context is required.");
Expand All @@ -32,6 +33,10 @@ public static void applyMetadata(Context context, SentryOptions options) {
log(options.getLogger(), SentryLevel.DEBUG, "DSN read: %s", dsn);
options.setDsn(dsn);
}

boolean ndk = metadata.getBoolean(ENABLE_NDK, options.isEnableNdk());
log(options.getLogger(), SentryLevel.DEBUG, "NDK read: %s", ndk);
options.setEnableNdk(ndk);
}
log(
options.getLogger(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.core.ILogger
import io.sentry.core.InvalidDsnException
import io.sentry.core.Sentry
import io.sentry.core.SentryOptions
import java.io.File
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -128,6 +130,22 @@ class SentryInitProviderTest {
assertFalse(Sentry.isEnabled())
}

@Test
fun `when applicationId is defined, ndk in meta-data is set to false, NDK doesnt initialize`() {
val sentryOptions = SentryOptions()
val mockLogger = mock<ILogger>()

val mockContext = createMockContext()

val metaData = Bundle()
mockMetaData(mockContext, metaData)
metaData.putBoolean(ManifestMetadataReader.ENABLE_NDK, false)

AndroidOptionsInitializer.init(sentryOptions, mockContext, mockLogger)

assertFalse(sentryOptions.isEnableNdk)
}

private fun mockMetaData(mockContext: Context, metaData: Bundle) {
val mockPackageManager: PackageManager = mock()
val mockApplicationInfo: ApplicationInfo = mock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public static void notifyNewSerializedEnvelope(String path) {
System.out.println("envelope written to " + path);
}

public static void init(SentryOptions options, String cacheDirPath) {
public static void init(SentryOptions options) {
// Java_example
initSentryNative(cacheDirPath);
initSentryNative(options.getCacheDirPath());
}
}
3 changes: 3 additions & 0 deletions sentry-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

<meta-data android:name="io.sentry.dsn" android:value="https://[email protected]/123" />
<meta-data android:name="io.sentry.debug" android:value="true" />

<!-- <meta-data android:name="io.sentry.auto-init" android:value="false" /> // how to disable the auto-init-->

<!-- <meta-data android:name="io.sentry.ndk" android:value="false" /> // how to disable the NDK-->
</application>
</manifest>