Skip to content

Commit

Permalink
chore(mobile): clean up the manual install page
Browse files Browse the repository at this point in the history
  • Loading branch information
ally-sassman committed Aug 23, 2024
1 parent 9bf01e9 commit 3c37d4f
Showing 1 changed file with 128 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,191 +38,186 @@ redirects:
- /docs/mobile-monitoring/new-relic-mobile-android/install-configure/install-android-apps-gradle-android-studio
---

To install the Android agent, you need to use our [guided install](https://onenr.io/0kjnEoryzRo). Setting up the Android agent requires an [application token](/docs/mobile-apps/viewing-your-application-token) that authenticates each mobile app you want to monitor. The app token is only available by going through the guided install.

## Install the Android agent with guided install [#guided-install]
To install the Android agent, we recommend you follow our guided install:

1. Go to <DNT>**[one.newrelic.com](https://one.newrelic.com) > Integrations & Agents > Mobile > Android**</DNT>.
2. Follow the guided install steps to set up your Android agent.
3. Wait a few minutes, then view your data by going to <DNT>**[one.newrelic.com > All capabilities](https://one.newrelic.com/all-capabilities) > Mobile > (select your app)**</DNT>.

## Manually install the Android agent [#manual-install]

These procedures appear in our guided install. Keep in mind that even if you're updating your build files through the docs, you still need to add your app to New Relic and grab your generated app token from the guided install. You cannot capture data about your Android apps otherwise.


## Resolve and apply the New Relic Android agent Gradle plugin [#resolve-apply-plugin]

In this example, `AGENT_VERSION` represents your agent version number. See the [agent release notes](/docs/release-notes/mobile-release-notes/android-release-notes), and use the latest version.
## Manually install the Android agent [#manual-install]

Starting with version 7.6.0, the New relic Android Gradle plugin is available on the [Gradle Plugin Portal](https://plugins.gradle.org/). This greatly simplifies the resolution and loading of community plugins.
The steps below are outlined in our guided install. Note that even if you manually install the agent, you'll still need to create a New Relic app and obtain the generated application token. This token is essential for the agent to send data to New Relic.

Newer apps should follow the instructions to [Apply the plugin using the plugins DSL block](#plugin-dsl) to use agent plugin hosted on the Gradle Plugin Portal.
<Steps>
<Step>
Add the Android agent plugin to your project.

If adding the New Relic Android agent to an older app, follow the instructions to [apply the plugin using the buildscript](#buildscript). For guidance on different approaches to resolving plugins, see [Resolving plugins](https://docs.gradle.org/current/userguide/plugins.html#sec:binary_plugin_locations).
Regarding the location of the plugin, you have two options:

* Recommended: Using the Gradle plugin DSL
* Starting with agent version 7.6.0, the Android agent plugin is available on the [Gradle Plugin Portal](https://plugins.gradle.org/) as a community plugin. The Gradle plugin DSL simpifies adding plugin dependencies to apps. The Gradle plugin DSL simplifies adding plugin dependencies to apps.
* This method involves adding the agent plugin dependency to your project's plugins DSL block.
* Legacy Plugin ID (`newrelic`)
* For older projects or specific use cases, you can use the legacy plugin ID.
* This method involves adding the agent as a dependency to your project's build script, specifying the MavenCentral repository.

1. Set app permissions. Update your `AndroidManifest.xml` file to allow your app to access the internet and check network state:
Follow the detailed steps below for your chosen installation method.

```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
```
<CollapserGroup>
<Collapser
id="DSL"
title="(Recommended) Apply the plugin using the plugins DSL block"
>
The recommended approach for managing plugin dependencies in Gradle utilizes the plugins DSL block. This section details applying the fully qualified form of the plugin ID (`com.newrelic.agent.android`) within this structure.

2. Start the Android agent. In your main Activity class (`onCreate` method), import the `NewRelic` class:
1. Declare the New Relic plugin to the top-level `build.gradle(.kts)` file using the Gradle Plugin Portal plugin ID:

```java
import com.newrelic.agent.android.NewRelic;
```
```groovy
plugins {
// for binary Gradle plugins that need to be resolved
id("com.newrelic.agent.android") version "AGENT_VERSION" apply false
}
```

<Callout variant="caution">
Starting the agent in other classes is not supported.
</Callout>
Make sure to replace `AGENT_VERSION` with your agent version number. We strongly recommend you use the [latest version](/docs/release-notes/mobile-release-notes/android-release-notes/).

3. Replace `GENERATED_TOKEN` with your actual app token (obtained during guided install) and add this code:
2. Apply the plugin explicitly to the app-level build file (and any sub-modules intended for instrumentation) using the following syntax:

```java
NewRelic.withApplicationToken("GENERATED_TOKEN").start(this.getApplicationContext());
```
```groovy
plugins {
id("com.newrelic.agent.android")
}
```

4. (Optional) Configure for minification:
For detailed information on applying plugins within Gradle, refer to the [Gradle documentation](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block).

1. If you use ProGuard or Dexguard for code shrinking, create a `newrelic.properties` file in your app-level directory (`_projectname/app_`).
2. Add this line to the file, replacing `GENERATED_TOKEN` with your actual token:
```ini
com.newrelic.application_token=GENERATED_TOKEN
```
3. To finish set up for minification, follow the steps in [configure ProGuard or DexGuard for Android apps](/docs/mobile-monitoring/new-relic-mobile-android/install-configure/configure-proguard-or-dexguard-android-apps).
</Collapser>

5. Clean your project, then run your app in an emulator or device to generate traffic. Wait a few minutes as your agent captures that data.
<Collapser
id="buildscript"
title=" Apply the plugin using the buildscript{} block"
>
The Android agent plugin is available on MavenCentral and uses the legacy plugin ID `newrelic` for compatibility with older projects. If you choose to leverage this legacy ID for an existing app, you'll need to explicitly configure the classpath to MavenCentral. This ensures Gradle can locate the plugin during your build process. For further details on various methods for resolving plugins, refer to the [Gradle documentation](https://docs.gradle.org/current/userguide/plugins.html#sec:binary_plugin_locations).

6. View your app's data on New Relic by going to <DNT>**[one.newrelic.com > All capabilities](https://one.newrelic.com/all-capabilities) > Mobile > (select an app)**</DNT>.
To prefer MavenCentral over the Gradle Plugin Portal, you need to make the following changes to Gradle files:

1. Add this snippet to your top-level build.gradle(.kts) file:

## Configure the Android agent for Gradle Plugin Portal [#configure-plugin-portal]
```groovy
buildscript {
repositories {
mavenCentral()
}
Starting with agent version 7.6.0, the Android agent plugin is available on the [Gradle Plugin Portal](https://plugins.gradle.org/) as a community plugin. The Gradle plugin DSL simpifies adding plugin dependencies to apps.
// not required if plugin classpath in resolutionStrategy{} below
dependencies {
classpath "com.newrelic.agent.android:agent-gradle-plugin:AGENT_VERSION"
}
}
```

<CollapserGroup>
2. Add this snippet to your `settings.gradle(.kts)` file through the `pluginManagement {}` block:

<Collapser
id="project-level"
title="Project-level build.gradle(.kts) file"
>
In this example, `AGENT_VERSION` represents your agent version number. See the [agent release notes](/docs/release-notes/mobile-release-notes/android-release-notes), and use the latest version.
```groovy
pluginManagement {
repositories {
mavenCentral() // adds repo for NewRelic artifacts
}
resolutionStrategy {
eachPlugin {
// not required if using `classpath dependency` above
if (requested.id.id.startsWith("newrelic") || requested.id.id.startsWith("com.newrelic.agent.android"))) {
useModule("com.newrelic.agent.android:agent-gradle-plugin:${AGENT_VERSION}")
}
}
}
plugins {
id("newrelic") apply false
}
}
```

Make sure to replace `AGENT_VERSION` with your agent version number. We strongly recommend you use the [latest version](/docs/release-notes/mobile-release-notes/android-release-notes/).

```groovy
plugins {
// for binary Gradle plugins that need to be resolved
id("com.newrelic.agent.android") version "AGENT_VERSION" apply false
}
```
</Collapser>

<Collapser
id="app-level"
title="App-level build.gradle(.kts) file"
>
In this example, `AGENT_VERSION` represents your agent version number. See the [agent release notes](/docs/release-notes/mobile-release-notes/android-release-notes), and use the latest version.

```groovy
plugins {
id 'com.newrelic.agent.android'
}
3. Apply the plugin to the app-level build file, as well as any other sub-module to be instrumented:

dependencies {
implementation 'com.newrelic.agent.android:android-agent:AGENT_VERSION'
}
```
</Collapser>
</CollapserGroup>
```groovy
plugins {
id("newrelic")
}
```
</Collapser>
</CollapserGroup>

</Step>

### Apply the plugin using the `plugins` DSL block [#apply-plugin-dsl]
<Step>
In your `AndroidManifest.xml` file, add the following permissions:

The recommended approach for managing plugin dependencies in Gradle utilizes the `plugins` DSL block. This section details applying the fully qualified form of the plugin id (`com.newrelic.agent.android`) within this structure.
```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
```
</Step>

<Step>
In your main `Activity` class within the `onCreate` method, add this import statement:

1. Declare the New Relic plugin to the top-level `build.gradle(.kts)` file using the Gradle Plugin Portal plugin ID:
```java
import com.newrelic.agent.android.NewRelic;
```

```groovy
plugins {
// for binary Gradle plugins that need to be resolved
id("com.newrelic.agent.android") version "AGENT_VERSION" apply false
}
```
Note: You must add this import statement to the `Activity` class. We don’t support starting the agent in other classes.
</Step>

2. Apply the plugin explicitly to the app-level build file (and any sub-modules intended for instrumentation) using the following syntax:
<Step>
In the `onCreate` method, add the following line, making sure to replace `GENERATED_TOKEN` with your actual app token (created during the guided install):

```groovy
plugins {
id("com.newrelic.agent.android")
}
```
```java
NewRelic.withApplicationToken("GENERATED_TOKEN").start(this.getApplicationContext());
```

</Step>

For detailed information on applying plugins within Gradle, refer to the [Gradle documentation](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block).
<Step>
(Optional) Additional configurations for Proguard/Dexguard

If you use ProGuard or Dexguard for code shrinking, follow these steps:

### Apply the plugin using the `buildscript{}` block [#buildscript]
1. Create a `newrelic.properties` file in your app-level directory (ex: `/projectname/app/newrelic.properties`).
2. In this new file, add the following line, replacing `GENERATED_TOKEN ` with your actual token:

The New Relic Android agent plugin is co-hosted on MavenCentral, but uses the legacy plugin ID (`newrelic`). Customers who prefer to use the legacy plugin ID must provide the classpath to [MavenCentral](https://mvnrepository.com/artifact/com.newrelic.agent.android/agent-gradle-plugin) to resolve the location of the plugin during builds.
```java
com.newrelic.application_token=GENERATED_TOKEN
```
3. Follow the steps decribed on [Configure ProGuard or DexGuard for Android apps](/docs/mobile-monitoring/new-relic-mobile-android/install-configure/configure-proguard-or-dexguard-android-apps/).
</Step>

To prefer MavenCentral over the Gradle Plugin Portal, you need to make the following changes to Gradle files:
<Step>
Clean your project, then run your app in an emulator or device to generate traffic. Wait a few minutes as your agent captures that data.
</Step>

1. Add this snippet to your top-level `build.gradle(.kts)` file:
```groovy
buildscript {
repositories {
mavenCentral()
}
// not required if plugin classpath in resolutionStrategy{} below
dependencies {
classpath "com.newrelic.agent.android:agent-gradle-plugin:AGENT_VERSION"
}
}
```

2. Add this snippet to your `settings.gradle(.kts)` file through the `pluginManagement {}` block:
```groovy
pluginManagement {
repositories {
mavenCentral() // adds repo for NewRelic artifacts
}
resolutionStrategy {
eachPlugin {
// not required if using `classpath dependency` above
if (requested.id.id.startsWith("newrelic") || requested.id.id.startsWith("com.newrelic.agent.android"))) {
useModule("com.newrelic.agent.android:agent-gradle-plugin:${AGENT_VERSION}")
}
}
}
plugins {
id("newrelic") apply false
}
}
```

3. Apply the plugin to the app-level build file, as well as any other sub-module to be instrumented:

```groovy
plugins {
id("newrelic")
}
```
<Step>
View your app's data on New Relic by going to <DNT>**[one.newrelic.com > All capabilities](https://one.newrelic.com/all-capabilities) > Mobile > (select an app)**</DNT>.
</Step>
</Steps>

## What's next?

In the above snippets, `AGENT_VERSION` represents your agent version number. We strongly recommend you use the latest agent for set up.
Congratulations! You've successfully installed the Android agent. You have a few options for next steps:

* Configure agent behavior during Gradle builds with the [New Relic Gradle plugin](/docs/mobile-monitoring/new-relic-mobile-android/install-configure/configure-new-relic-gradle-plugin).
* [Upgrade the Android agent SDK](/docs/mobile-monitoring/new-relic-mobile-android/install-configure/upgrading-new-relic-mobiles-android-sdk) to keep your Android agent up to date.
* Having problems with your Android installation? Follow the [troubleshooting procedures](/docs/mobile-monitoring/new-relic-mobile-android/troubleshoot/no-data-appears-android).

## Android 4.x: Multidex support [#4x-multidex]

Mobile monitoring for Android versions older than Android 5.0 (API level 21) use the Dalvik runtime to execute app code. By default, Dalvik limits apps to a single `classes.dex` bytecode file per APK. To get around this limitation, you must [enable multidex support](https://developer.android.com/studio/build/multidex.html). Then you can use the [multidex support library](https://developer.android.com/tools/support-library/features.html#multidex), which becomes part of the primary DEX file of your app. The library manages access to the additional DEX files and their containing code.

<Collapser
id="error"
title="Troubleshoot `java.lang.NoClassDefFoundError`"
title="Troubleshoot the java.lang.NoClassDefFoundError error"
>
When building each DEX file for a multidex app, the build tools perform complex decision making to determine which classes are needed in the primary DEX file so that your app can start successfully. If, during start up, the required classes aren't available in the primary DEX file, your app will crash with the error `java.lang.NoClassDefFoundError`.

Expand Down Expand Up @@ -252,12 +247,4 @@ Mobile monitoring for Android versions older than Android 5.0 (API level 21) use
```

For more information, see the [Android Developers documentation](https://developer.android.com/studio/build/multidex.html#keep) on declaring classes required in the primary DEX file.
</Collapser>

## What's next?

Congratulations! You've successfully installed the Android agent. You have a few options for next steps:

* Configure agent behavior during Gradle builds with the [New Relic Gradle plugin](/docs/mobile-monitoring/new-relic-mobile-android/install-configure/configure-new-relic-gradle-plugin).
* [Upgrade the Android agent SDK](/docs/mobile-monitoring/new-relic-mobile-android/install-configure/upgrading-new-relic-mobiles-android-sdk) to keep your Android agent up to date.
* Having problems with your Android installation? Follow the [troubleshooting procedures](/docs/mobile-monitoring/new-relic-mobile-android/troubleshoot/no-data-appears-android).
</Collapser>

0 comments on commit 3c37d4f

Please sign in to comment.