Skip to content

Commit f4cb1a8

Browse files
authored
GenerateRnSourcemapTaskRegistration improvements (#2214)
1 parent d2a5a7c commit f4cb1a8

File tree

12 files changed

+130
-86
lines changed

12 files changed

+130
-86
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id("com.android.application")
3+
id("io.embrace.swazzler")
4+
id("io.embrace.android.testplugin")
5+
}
6+
7+
repositories {
8+
google()
9+
mavenCentral()
10+
mavenLocal()
11+
}
12+
13+
integrationTest.configureAndroidProject(project)
14+
15+
embrace {
16+
autoAddEmbraceDependencies.set(true)
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-keep class com.example.app.Foo { *; }
2+
-keep class io.embrace.android.embracesdk.internal.config.instrumented.* { *; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"app_id": "abcde",
3+
"api_token": "12345123451234512345123451234512",
4+
"ndk_enabled": true
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.app;
2+
3+
public class Bar {
4+
public static String getBar() {
5+
return "bar";
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.app;
2+
3+
public class Foo {
4+
public static String getFoo() {
5+
return "foo";
6+
}
7+
}

embrace-gradle-plugin-integration-tests/src/main/java/io/embrace/android/gradle/integration/framework/SetupInterface.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class SetupInterface(
4040
enqueueResponse(endpoint, response)
4141
}
4242

43+
fun SetupInterface.setupEmptyHandshakeResponse() {
44+
val json = serializeRequestBody(NdkUploadHandshakeResponse(null))
45+
enqueueResponse(EmbraceEndpoint.NDK_HANDSHAKE, MockResponse().setBody(json))
46+
}
47+
4348
fun SetupInterface.setupMapResponseWithEmptyValues(endpoint: EmbraceEndpoint) {
4449
val response = """
4550
{

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,43 @@ class ReactNativeAndroidTest {
4848
installNodeModules(projectDir)
4949
setupMockResponses(handshakeLibs, handshakeArchs, defaultExpectedVariants)
5050
},
51-
assertions = { projectDir ->
51+
assertions = {
5252
verifyBuildTelemetryRequestSent(defaultExpectedVariants)
5353
verifyHandshakes(defaultExpectedLibs, defaultExpectedArchs, defaultExpectedVariants)
5454
verifyUploads(handshakeLibs, handshakeArchs, defaultExpectedVariants)
55-
verifyAsmInjection(projectDir)
55+
}
56+
)
57+
}
58+
59+
@Test
60+
fun `react native asm injection test`() {
61+
rule.runTest(
62+
fixture = "react-native-android",
63+
androidProjectRoot = "android",
64+
task = "assembleRelease",
65+
setup = { projectDir ->
66+
setupEmptyHandshakeResponse()
67+
installNodeModules(projectDir)
68+
},
69+
assertions = { projectDir ->
70+
verifyAsmInjection(File(projectDir, "app"), "765FB008173DC25D016D112F67250241")
71+
}
72+
)
73+
}
74+
75+
@Test
76+
fun `react native project without bundle or sourcemap`() {
77+
rule.runTest(
78+
fixture = "not-react-native-android",
79+
androidProjectRoot = "android",
80+
additionalArgs = listOf("-Pandroid.useAndroidX=true"),
81+
task = "assembleRelease",
82+
setup = {
83+
setupEmptyHandshakeResponse()
84+
},
85+
assertions = { projectDir ->
86+
verifyNoUploads()
87+
verifyAsmInjection(projectDir, null)
5688
}
5789
)
5890
}
@@ -71,10 +103,10 @@ class ReactNativeAndroidTest {
71103
}
72104
}
73105

74-
private fun verifyAsmInjection(projectDir: File) {
106+
private fun verifyAsmInjection(buildDir: File, expectedBundleId: String?) {
75107
// Read and parse the smali file containing the injected symbols
76108
val smaliFile = SmaliConfigReader().readSmaliFiles(
77-
File(projectDir, "app"),
109+
buildDir,
78110
listOf("/io/embrace/android/embracesdk/internal/config/instrumented/ProjectConfigImpl")
79111
).first()
80112

@@ -84,6 +116,6 @@ class ReactNativeAndroidTest {
84116
listOf(SmaliMethod("getReactNativeBundleId()Ljava/lang/String;"))
85117
).methods.first()
86118

87-
assertEquals("765FB008173DC25D016D112F67250241", method.returnValue)
119+
assertEquals(expectedBundleId, method.returnValue)
88120
}
89121
}

embrace-gradle-plugin/src/main/java/io/embrace/android/gradle/plugin/config/PluginBehaviorImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PluginBehaviorImpl(
5353
}
5454

5555
override val isReactNativeProject: Boolean by lazy {
56-
val rootFile = project.layout.projectDirectory.asFile.parentFile?.parentFile
56+
val rootFile = project.rootDir.parentFile
5757
if (rootFile != null) {
5858
val nodeModules = File("${rootFile.path}/node_modules")
5959
val nodeModulesEmbrace = File("${nodeModules.path}/react-native")

embrace-gradle-plugin/src/main/java/io/embrace/android/gradle/plugin/tasks/reactnative/GenerateRnSourcemapTask.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import org.gradle.api.model.ObjectFactory
1616
import org.gradle.api.tasks.InputFile
1717
import org.gradle.api.tasks.Optional
1818
import org.gradle.api.tasks.OutputFile
19+
import org.gradle.api.tasks.PathSensitive
20+
import org.gradle.api.tasks.PathSensitivity
1921
import org.gradle.api.tasks.TaskAction
2022
import java.io.File
2123
import javax.inject.Inject
@@ -29,12 +31,14 @@ abstract class GenerateRnSourcemapTask @Inject constructor(
2931

3032
private val logger = Logger(GenerateRnSourcemapTask::class.java)
3133

32-
@get:Optional
34+
@Optional
3335
@get:InputFile
36+
@get:PathSensitive(PathSensitivity.NONE)
3437
val sourcemap: RegularFileProperty = objectFactory.fileProperty()
3538

36-
@get:Optional
39+
@Optional
3740
@get:InputFile
41+
@get:PathSensitive(PathSensitivity.NONE)
3842
val bundleFile: RegularFileProperty = objectFactory.fileProperty()
3943

4044
@get:OutputFile
@@ -45,22 +49,17 @@ abstract class GenerateRnSourcemapTask @Inject constructor(
4549

4650
@TaskAction
4751
fun onRun() {
48-
val bundleFile = bundleFile.orNull?.asFile
52+
val bundleFile = bundleFile.asFile.orNull
4953
if (bundleFile == null || !bundleFile.exists()) {
50-
logger.error("Couldn't find the JSBundle. React native files were not uploaded.")
54+
logger.error("JSBundle not found. React Native files were not uploaded.")
5155
return
5256
}
5357

5458
val bundleId = calculateMD5ForFile(bundleFile)
5559

56-
/**
57-
* In old React Native Versions, the source map is not exposed as output in the task.
58-
* If the source map is not present, we will search for it in the known location
59-
*/
60-
val sourceMapFile: File? = sourcemap.orNull?.asFile
61-
60+
val sourceMapFile = sourcemap.asFile.orNull
6261
if (sourceMapFile == null || !sourceMapFile.exists()) {
63-
logger.error("Couldn't find the Source Map. React native files were not uploaded.")
62+
logger.error("Sourcemap not found. React Native files were not uploaded.")
6463
return
6564
}
6665

0 commit comments

Comments
 (0)