# Android

{% hint style="warning" %}
**IMQA SDK Installation Guide\_React\_Native** provides instructions for applying IMQA SDK in the React Native environment.&#x20;

After applying the IMQA agent code with reference to the IMQA MPM Installation Guide, please follow further the instructions of this document.
{% endhint %}

## 1. Common

### MPM Module install&#x20;

To apply IMQA SDK in the React native environment, execute the command below to install the package.&#x20;

{% code title="When Using npm Command" %}

```
// npm install imqa-react-native-agent --save 
```

{% endcode %}

{% code title="When Using yarn Command" %}

```
yarn add imqa-react-native-agent
```

{% endcode %}

## 2. Installing Gradle plugin&#x20;

In the “app.gradle” file, add “imqa-mpm-injector” to the “dependencies” block in the "buildscript" followed by “plugin” at the top. This is typically located in “\<project\_dir>/app/build.gradle”.

{% hint style="warning" %}
The gradle file should be synchronized after updating the file.
{% endhint %}

{% code title="app.gradle(project root)" %}

```java
/// Add Build script dependencies 
buildscript { 
    repositories { 
        mavenCentral()
    } 
    
    dependencies {
    // Add dependencies.
        classpath 'io.imqa:imqa-mpm-injector:2.25.6' ... 
    } 
} 
```

{% endcode %}

* java

{% code title="app.gradle(app module) -> java" %}

```java
...
    dependencies {
    // Add dependencies.
       implementation 'io.imqa:imqa-core:2.27.3'
       implementation 'io.imqa:imqa-mpm-client:2.27.7'
       implementation 'io.imqa:imqa-crash-client:2.27.2'
    } 
} 

io.imqa.IMQAPlugin imqaPlugin = new io.imqa.IMQAPlugin()
imqaPlugin.init(project)
new io.imqa.injector.GJavacAction(project.name).setConfiguration(project)
android.applicationVariants.all { variant ->
    variant.javaCompile.doLast { task ->
        new io.imqa.injector.CompileAction(
                io.imqa.injector.util.BuildOption.BUILD_LOCATION_TYPE.javacClasses,
                project.name,
                io.imqa.injector.GJavacAction.convertBuildType(variant.getBuildType()),
                io.imqa.injector.GJavacAction.makeFlavor(variant.getBuildType().name,
                        variant)
        ).execute(task)
    }

```

{% endcode %}

* kotlin

{% code title="app.gradle(app module) -> kotlin" %}

```kotlin
...
    dependencies {
    // Add dependencies.
       implementation 'io.imqa:imqa-core:2.27.3'
       implementation 'io.imqa:imqa-mpm-client:2.27.7'
       implementation 'io.imqa:imqa-crash-client:2.27.2'
    } 
} 

io.imqa.IMQAPlugin imqaPlugin = new io.imqa.IMQAPlugin()
imqaPlugin.init(project)
new io.imqa.injector.GJavacAction(project.name).setConfiguration(project)
android.applicationVariants.all { variant ->
    variant.javaCompile.doLast { task ->
        new io.imqa.injector.CompileAction(
                io.imqa.injector.util.BuildOption.BUILD_LOCATION_TYPE.kotlin,
                project.name,
                io.imqa.injector.GJavacAction.convertBuildType(variant.getBuildType()),
                io.imqa.injector.GJavacAction.makeFlavor(variant.getBuildType().name,
                        variant)
        ).execute(task)
    }
}
```

{% endcode %}

* java + kotlin

{% code title="app.gradle(app module) -> java + kotlin" %}

```java
...

    dependencies {
    // Add dependencies.
       implementation 'io.imqa:imqa-core:2.27.3'
       implementation 'io.imqa:imqa-mpm-client:2.27.7'
       implementation 'io.imqa:imqa-crash-client:2.27.2'
    } 
} 

io.imqa.IMQAPlugin imqaPlugin = new io.imqa.IMQAPlugin()
imqaPlugin.init(project)
new io.imqa.injector.GJavacAction(project.name).setConfiguration(project)
android.applicationVariants.all { variant ->
    variant.javaCompile.doLast { task -> {
        new io.imqa.injector.CompileAction(
                io.imqa.injector.util.BuildOption.BUILD_LOCATION_TYPE.kotlin,
                project.name,
                io.imqa.injector.GJavacAction.convertBuildType(variant.getBuildType()),
                io.imqa.injector.GJavacAction.makeFlavor(variant.getBuildType().name,
                        variant)
        ).execute(task)

        new io.imqa.injector.CompileAction(
                io.imqa.injector.util.BuildOption.BUILD_LOCATION_TYPE.javacClasses,
                project.name,
                io.imqa.injector.GJavacAction.convertBuildType(variant.getBuildType()),
                io.imqa.injector.GJavacAction.makeFlavor(variant.getBuildType().name,
                        variant)
        ).execute(task)
    }
    }
}
```

{% endcode %}

{% code title="gradle.properties" %}

```
...

project.home=Path of project (ex:/user/workspace/project1)
#Add only Kotlin project
project.kotlin.path=$PROJECT_HOME/app/build/tmp/kotlin-classes 
#Add only Java project
project.java.path=$PROJECT_HOME/app/build/intermediates/javac/debug/classes
#Path to built manifest file
project.manifest.path=$PROJECT_HOME/app/build/intermediates/merged_manifest/debug/AndroidManifest.xml
#gradle action
project.task.execute=:assemble

```

{% endcode %}

## 3. Adding authority to the Android Manifest&#x20;

You have to grant Internet authority to upload occurred crash information to AndroidManifest.xml.

{% code title="AndroidManifest.xml" %}

```xml
<uses-permission android:name="android.permission.INTERNET"/>
```

{% endcode %}

## 4. Inserting the IMQA initialization code into the source code

### Writing the IMQA start code

{% hint style="warning" %}
The code should be inserted into the application class, not activity.

At this time, insert the copied “Project Key” value into “PROJECT\_KEY”.
{% endhint %}

{% code title="MyApplication.java" %}

```java
...
@Override
public void onCreate() {
    super.onCreate();
    io.imqa.core.IMQAOption imqaOption = new io.imqa.core.IMQAOption();
    imqaOption.setBuildType(false);
    imqaOption.setUploadPeriod(true);
    imqaOption.setKeepFileAtUploadFail(false);
    imqaOption.setDumpInterval(10000);
    imqaOption.setFileInterval(5);
    imqaOption.setHttpTracing(true);
    imqaOption.setRemoteControl(true);

    io.imqa.mpm.IMQAMpmAgent.getInstance()
        .setOption(imqaOption) // Sets an option to decide the MPM operation method.
        .setContext(this, BuildConfig.FLAVOR) 
        // Initializes the application context. (Insert an empty string if there is no flavor)
        .setProjectKey("PROJECT_KEY") 
        // Sets the project key of the IMQA MPM client.
        .init(); 
        // Initializes and executes the registered option.
    ...
}
...
```

{% endcode %}

{% code title="MyApplication.kt" %}

```kotlin
...
override fun onCreate() {
    super.onCreate()
    val imqaOption : IMQAOption = io.imqa.core.IMQAOption()
    imqaOption.setBuildType(false)
    imqaOption.setUploadPeriod(true)
    imqaOption.setKeepFileAtUploadFail(false)
    imqaOption.setDumpInterval(10000)
    imqaOption.setFileInterval(5)
    imqaOption.setHttpTracing(true)
    imqaOption.setRemoteConfig(true)

    io.imqa.mpm.IMQAMpmAgent.getInstance()
        .setOption(imqaOption) 
        // Sets an option to decide the MPM operation method.
        .setContext(this, BuildConfig.FLAVOR) 
        // Initializes the application context. (Insert an empty string if there is no flavor)
        .setProjectKey("PROJECT_KEY") 
         // Sets the project key of the IMQA MPM client.
        .init(); 
         // Initializes and executes the registered option.
    ...
}
...
```

{% endcode %}

### MPM Mode Option

You can change the IMQA MPM execution options using parameters or options. Check the execution options in Android MPM Configuration Options.

### Shortcut mode

You can initialize IMQA MPM using the shortcut mode.

{% code title="MyApplication.java" %}

```java
...
override fun onCreate() {
    super.onCreate()

    io.imqa.mpm.IMQAMpmAgent.getInstance()
        .setContext(this, BuildConfig.FLAVOR) // Initializes the application context. (Insert an empty string if there is no flavor)
        .setProjectKey("PROJECT_KEY") // Sets the project key of the IMQA MPM client.
        .init() // Initializes and executes the registered option.
    ...
}
...
```

{% endcode %}

{% code title="MyApplication.kt" %}

```kotlin
...
override fun onCreate() {
    super.onCreate()

    io.imqa.mpm.IMQAMpmAgent.getInstance()
        .setContext(this, BuildConfig.FLAVOR) // Initializes the application context. (Insert an empty string if there is no flavor)
        .setProjectKey("PROJECT_KEY") // Sets the project key of the IMQA MPM client.
        .init() // Initializes and executes the registered option.
    ...
}
...
```

{% endcode %}

### ⚠️HTTPS (API level > 27)

The HTTPS request is required from Android API level 28 and above due to the enhanced network security policy. The following measures are needed to meet the requirement:

1. **Authenticated HTTPS request**\
   Request authenticated HTTPS to the IMQA server to collect the data normally.

{% code title="MyApplication.java or MyApplication.kt" %}

```kotlin
...
imqaOption.setServerUrl('https://collector.imqa.io');
...
```

{% endcode %}

2. **Forced HTTP request**

   You can allow all cleartext HTTP traffic by setting this flag.

{% code title="AndroidManifest.xml" %}

```xml
...
<application
    ...
    android:usesCleartextTraffic="true"
    />
...
```

{% endcode %}

### Using Zstandard (Zstd)

You can use Zstandard, which is an algorithm enabling more secure and efficient data compression, instead of the typical gzip. To use it, you need the following settings:

1. **Setting the MPM Mode Option**\
   You can set whether to use Zstd in the settings when inserting the IMQA startup code.

{% code title="MyApplication.java or MyApplication.kt" %}

```kotlin
...
imqaOption.setCompressZstd(true);
...
```

{% endcode %}

2. **Add Zstd Librar**y\
   In the “app.gradle” file, add the “zstd-jni” aar library to the “dependencies” block in “buildscript”.

{% code title="app.gradle(app module)" %}

```java
...
    dependencies {
    // Add dependencies.
      ...
       implementation 'com.github.luben:zstd-jni:1.5.2-3@aar'
       ...
    }
} 
```

{% endcode %}

## 5. ⚠️Issues in the current version

1. **ASM**\
   Conflict may occur with the library using ASM.<br>
2. **APK Repackaging**\
   Source line information disappears while repackaging the APK. The auto-insert function should be executed during compilation, in order to include line information.<br>
3. **Collecting library information**\
   The auto-insert function is not executed in the class file added to the library. The auto-insert function should be executed while repackaging the API, in order to collect information on what is executed in the library.

## 6. Settings required for the installation-type

Any client who selects an IMQA installation-type must set the collection server.

{% code title="MyApplication.java" %}

```java
...
@Override
public void onCreate() {
    super.onCreate();
    io.imqa.core.IMQAOption imqaOption = new io.imqa.core.IMQAOption();

    /*
     * Default value: 'https://collector.imqa.io'
     * Sets the MPM server URL.
     * String: Server information
     */
    imqaOption.setServerUrl('https://(IPAddress)');

    io.imqa.mpm.IMQAMpmAgent.getInstance()
        .setOption(imqaOption) // Sets the option to decide the MPM operation method.
        .setContext(this, BuildConfig.FLAVOR) // Initializes the application context. (Insert an empty string if there is no flavor)
        .setProjectKey("PROJECT_KEY") // Sets the project key of the IMQA MPM client.
        .init() // Initializes and executes the registered option.
    ...
}
...
```

{% endcode %}
