Skip to content

Commit 3108bb5

Browse files
committed
Add auto start instrumentation
1 parent 0e80dc0 commit 3108bb5

22 files changed

Lines changed: 285 additions & 187 deletions

File tree

embrace-android-sdk/api/embrace-android-sdk.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
public final class io/embrace/android/embracesdk/AutoStartInstrumentationHook {
2+
public static fun _preOnCreate (Landroid/app/Application;)V
3+
}
4+
15
public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/embracesdk/internal/api/SdkApi {
26
public static final field Companion Lio/embrace/android/embracesdk/Embrace$Companion;
37
public fun activityLoaded (Landroid/app/Activity;)V

embrace-android-sdk/embrace-proguard.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
## Keep gradle plugin hooks
5151
-keep class io.embrace.android.embracesdk.okhttp3.** { *; }
5252
-keep class io.embrace.android.embracesdk.ViewSwazzledHooks { *; }
53+
-keep class io.embrace.android.embracesdk.AutoStartInstrumentationHook { *; }
5354
-keep class io.embrace.android.embracesdk.WebViewClientSwazzledHooks { *; }
5455
-keep class io.embrace.android.embracesdk.WebViewChromeClientSwazzledHooks { *; }
5556
-keep class io.embrace.android.embracesdk.fcm.swazzle.callback.com.android.fcm.FirebaseSwazzledHooks { *; }

embrace-android-sdk/lint-baseline.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<issues format="6" by="lint 8.7.3" type="baseline" client="gradle" dependencies="false" name="AGP (8.7.3)" variant="all" version="8.7.3">
2+
<issues format="6" by="lint 8.9.1" type="baseline" client="gradle" dependencies="false" name="AGP (8.9.1)" variant="all" version="8.9.1">
3+
4+
<issue
5+
id="EmbracePublicApiPackageRule"
6+
message="Don&apos;t put classes in the io.embrace.android.embracesdk package unless they&apos;re part of the public API. Please move the new class to an appropriate package or (if you&apos;re adding to the public API) suppress this error via the lint baseline file."
7+
errorLine1="public final class AutoStartInstrumentationHook {"
8+
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
9+
<location
10+
file="src/main/java/io/embrace/android/embracesdk/AutoStartInstrumentationHook.java"
11+
line="12"
12+
column="20"/>
13+
</issue>
314

415
<issue
516
id="EmbracePublicApiPackageRule"
@@ -67,6 +78,17 @@
6778
column="88"/>
6879
</issue>
6980

81+
<issue
82+
id="UnknownNullness"
83+
message="Unknown nullability; explicitly declare as `@Nullable` or `@NonNull` to improve Kotlin interoperability; see https://developer.android.com/kotlin/interop#nullability_annotations"
84+
errorLine1=" public static void _preOnCreate(android.app.Application application) {"
85+
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~">
86+
<location
87+
file="src/main/java/io/embrace/android/embracesdk/AutoStartInstrumentationHook.java"
88+
line="17"
89+
column="37"/>
90+
</issue>
91+
7092
<issue
7193
id="UnknownNullness"
7294
message="Unknown nullability; explicitly declare as `@Nullable` or `@NonNull` to improve Kotlin interoperability; see https://developer.android.com/kotlin/interop#nullability_annotations"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.embrace.android.embracesdk;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import io.embrace.android.embracesdk.annotation.InternalApi;
6+
import io.embrace.android.embracesdk.internal.EmbraceInternalApi;
7+
8+
/**
9+
* @hide
10+
*/
11+
@InternalApi
12+
public final class AutoStartInstrumentationHook {
13+
14+
private AutoStartInstrumentationHook() {
15+
}
16+
17+
public static void _preOnCreate(android.app.Application application) {
18+
try {
19+
Embrace.getInstance().start(application);
20+
} catch (Exception exception) {
21+
logError(exception);
22+
}
23+
}
24+
25+
private static void logError(@NonNull Throwable throwable) {
26+
EmbraceInternalApi.getInstance().getInternalInterface().logInternalError(throwable);
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.embrace.test.fixtures
2+
3+
import android.app.Application
4+
5+
class TestApplication : Application() {
6+
override fun onCreate() {
7+
super.onCreate()
8+
}
9+
}

embrace-bytecode-instrumentation-tests/src/test/java/io/embrace/gradle/plugin/instrumentation/InstrumentedBytecodeTestCases.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.embrace.gradle.plugin.instrumentation
22

3+
import io.embrace.android.gradle.plugin.instrumentation.visitor.ApplicationClassAdapter
34
import io.embrace.android.gradle.plugin.instrumentation.visitor.OkHttpClassAdapter
45
import io.embrace.android.gradle.plugin.instrumentation.visitor.OnClickClassAdapter
56
import io.embrace.android.gradle.plugin.instrumentation.visitor.OnLongClickClassAdapter
@@ -27,6 +28,7 @@ import io.embrace.test.fixtures.MissingInterfaceOnLongClickListener
2728
import io.embrace.test.fixtures.MissingOverrideOnClickListener
2829
import io.embrace.test.fixtures.MissingOverrideOnLongClickListener
2930
import io.embrace.test.fixtures.NoOverrideWebViewClient
31+
import io.embrace.test.fixtures.TestApplication
3032
import io.embrace.test.fixtures.VirtualMethodRefNamedOnClick
3133
import okhttp3.OkHttpClient
3234
import org.objectweb.asm.ClassVisitor
@@ -47,6 +49,10 @@ private val okHttpFactory: ClassVisitorFactory = { visitor ->
4749
OkHttpClassAdapter(ASM_API_VERSION, visitor) {}
4850
}
4951

52+
private val applicationFactory: ClassVisitorFactory = { visitor ->
53+
ApplicationClassAdapter(ASM_API_VERSION, visitor)
54+
}
55+
5056
/**
5157
* Declares the test cases for bytecode in [InstrumentedBytecodeTest]. You should define the
5258
* input class, the expected output, and the [ClassVisitor] which will instrument the bytecode.
@@ -60,15 +66,23 @@ internal fun instrumentedBytecodeTestCases(): List<BytecodeTestParams> {
6066
.plus(onLongClickInnerTestCases)
6167
.plus(webclientTestCases)
6268
.plus(okHttpTestCases)
69+
.plus(applicationTestCases)
6370
.distinct() // filter out any unintentional duplicate test cases
6471
.sortedBy(BytecodeTestParams::simpleClzName)
6572
}
6673

74+
private val applicationTestCases = listOf(
75+
TestApplication::class
76+
).map {
77+
BytecodeTestParams(it.java, factory = applicationFactory)
78+
}
79+
6780
private val okHttpTestCases = listOf(
6881
OkHttpClient.Builder::class
6982
).map {
7083
BytecodeTestParams(it.java, factory = okHttpFactory)
7184
}
85+
7286
private val webclientTestCases = listOf(
7387
CustomWebViewClient::class,
7488
ExtendedCustomWebViewClient::class,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// class version 55.0 (55)
2+
// access flags 0x31
3+
public final class io/embrace/test/fixtures/TestApplication extends android/app/Application {
4+
5+
// compiled from: TestApplication.kt
6+
7+
@Lkotlin/Metadata;(mv={1, 8, 0}, k=1, xi=48, d1={"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0008\u0002\n\u0002\u0010\u0002\n\u0000\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002J\u0008\u0010\u0003\u001a\u00020\u0004H\u0016\u00a8\u0006\u0005"}, d2={"Lio/embrace/test/fixtures/TestApplication;", "Landroid/app/Application;", "()V", "onCreate", "", "embrace-bytecode-instrumentation-tests_release"})
8+
9+
// access flags 0x1
10+
public <init>()V
11+
L0
12+
LINENUMBER 5 L0
13+
ALOAD 0
14+
INVOKESPECIAL android/app/Application.<init> ()V
15+
RETURN
16+
L1
17+
LOCALVARIABLE this Lio/embrace/test/fixtures/TestApplication; L0 L1 0
18+
MAXSTACK = 1
19+
MAXLOCALS = 1
20+
21+
// access flags 0x1
22+
public onCreate()V
23+
ALOAD 0
24+
INVOKESTATIC io/embrace/android/embracesdk/AutoStartInstrumentationHook._preOnCreate (Landroid/app/Application;)V
25+
L0
26+
LINENUMBER 7 L0
27+
ALOAD 0
28+
INVOKESPECIAL android/app/Application.onCreate ()V
29+
L1
30+
LINENUMBER 8 L1
31+
RETURN
32+
L2
33+
LOCALVARIABLE this Lio/embrace/test/fixtures/TestApplication; L0 L2 0
34+
MAXSTACK = 1
35+
MAXLOCALS = 1
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.app
2+
3+
import android.app.Application
4+
5+
class ApplicationFixture : Application() {
6+
override fun onCreate() {
7+
8+
}
9+
}

embrace-gradle-plugin-integration-tests/src/test/java/io/embrace/android/gradle/integration/testcases/BytecodeInstrumentationTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class BytecodeInstrumentationTest {
2222
"/com/example/app/OnLongClickListenerFixture",
2323
"/okhttp3/OkHttpClient\$Builder",
2424
"/com/example/app/FcmServiceFixture",
25+
"/com/example/app/ApplicationFixture"
2526
)
2627
private val defaultArgs = listOf("-x", "lintVitalRelease")
2728

embrace-gradle-plugin-integration-tests/src/test/resources/bytecode-instrumentation-enabled.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
"embraceHook": "invoke-static {p1}, Lio/embrace/android/embracesdk/fcm/swazzle/callback/com/android/fcm/FirebaseSwazzledHooks;->_onMessageReceived(Lcom/google/firebase/messaging/RemoteMessage;)V"
4646
}
4747
]
48+
},
49+
{
50+
"className": "ApplicationFixture",
51+
"methods": [
52+
{
53+
"signature": "onCreate()V",
54+
"embraceHook": "invoke-static {p0}, Lio/embrace/android/embracesdk/AutoStartInstrumentationHook;->_preOnCreate(Landroid/app/Application;)V"
55+
}
56+
]
4857
}
4958
]
5059
}

0 commit comments

Comments
 (0)