# Nodle SDK - Android Integration

## Step 1: Generate Public Key

* Go and create a wallet -> <https://learn.nodleprotocol.io/wallets/how-to-create-a-nodle-cash-wallet> Make sure to save your private key
* In the extension copy the `public_key`
* Proceed forward with next steps.

## Step 2: Add the Maven Repository

Add the code below in your project's `build.gradle` to add the Nodle repository

```groovy
buildscript {
    repositories {
        google()
        maven { url 'http://maven.nodle.io/io/home/runner/.m2/repository/' }
    }
}

allprojects {
    repositories {
        google()
        maven { url 'http://maven.nodle.io/io/home/runner/.m2/repository/' }
    }
}

```

If you are using Android Studio Arctic Fox and newer AGP 7.0+ and GP 7.0+ please use the following. We are currently migrating the repository to HTTPS.

```groovy
buildscript {
    repositories {
        google()
        maven {
            url "http://maven.nodle.io/io/home/runner/.m2/repository/"
            allowInsecureProtocol = true
        }
    }
}

allprojects {
    repositories {
        google()
        maven {
            url "http://maven.nodle.io/io/home/runner/.m2/repository/"
            allowInsecureProtocol = true
        }
    }
}
```

If you are using Android Studio Bumblebee and newer AGP 7.0.2+ and GP 7.0.2+ please use the following in your `settings.gradle`&#x20;

```groovy
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven {
            url "http://maven.nodle.io/io/home/runner/.m2/repository/"
            allowInsecureProtocol = true
        }
    }
}
```

If you are using Java as your language of choice please make sure to use the default Java Library version which is JDK 11 as of Android Studio **Fox or Bumblebee** update. Like that you can make sure your project works as expected between the Kotlin-Java and vice-versa compatibility. You can do that by following this path: **Project Structure -> SDK Location -> Gradle Settings -> Gradle Projects -> Gradle JDK** -> Select 11 or later.

## Step 3: Add the NodleSDK dependency

In your app module's `build.gradle` simply add the Nodle SDK dependency in gradle. Please node that nodlesdk depends by default on google play service.  If your app runs on devices that doesn't have google play service, you can use a different flavour of the nodlesdk that doesn't depend on google play services. Our previous release supports **API** **30** with **AGP** **7.0.2**+ and **GP** **7.1**:

{% tabs %}
{% tab title="Default (depends on Google Play Services)" %}

```groovy
// Top level gradle
buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.8'    // Google Services plugin
    }
}

// Module Gradle
dependencies {
    implementation 'io.nodle:nodlesdk-rc-lp:cbe8a42b18'
}
```

{% endtab %}

{% tab title="Without Google Play Services dependency" %}

```groovy
dependencies {
    implementation 'io.nodle:nodlesdk-rc-lg:cbe8a42b18'
}
```

{% endtab %}
{% endtabs %}

Our latest release provide full support for **Android 12** API **31** with AGP **7.1.2+** and GP **7.2** you can simply add the Nodle SDK dependency in your   `build.gradle`&#x20;

{% tabs %}
{% tab title="Default (depends on Google Play Services)" %}

```groovy
// Top level gradle
buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'    // Google Services plugin
    }
}

// Module Gradle
dependencies {
    implementation 'io.nodle:nodlesdk-rc-lp:381d19b7b5'
}
```

{% endtab %}

{% tab title="Without Google Play Services dependency" %}

```groovy
dependencies {
    implementation 'io.nodle:nodlesdk-rc-lg:381d19b7b5'
}
```

{% endtab %}
{% endtabs %}

If you are using the Google Play Services version please make sure to add the plugin. You can use the libraries we are using or the newest ones. We would try to support always the latest libraries. The **minAPI-19** and **maxAPI-31**. We are also on the latest version of **AGP** **7.1.2+**

The latest version of the **stable-production** SDK is  `381d19b7b5`

## Step 4: Initialize the Nodle SDK

First you need to declare your application class in your **ApplicationManifest.xml.** And declare the required permissions for Nodle to be able to run:

{% tabs %}
{% tab title="Java" %}

```java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nodle.dummy">
    
    <!-- Required permissions NodleSDK -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    
    <!-- Required permissions NodleSDK Android 12  -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    
     <!-- Put your application class name below -->
    <application
        android:name="App">
    </application>

</manifest>

```

{% endtab %}

{% tab title="Kotlin" %}

```java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nodle.dummy">
    
    <!-- Required permissions NodleSDK -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    
    <!-- Required permissions NodleSDK Android 12  -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    
     <!-- Put your application class name below -->
    <application
        android:name="App">
    </application>

</manifest>

```

{% endtab %}
{% endtabs %}

In the `onCreate` method of your application class you should initialize Nodle like this:

{% tabs %}
{% tab title="Java" %}

```java
import io.nodle.sdk.android.Nodle

class App : Application() {
    override fun onCreate() {
        super.onCreate();
        Nodle.init(this);
    }
}

```

{% endtab %}

{% tab title="Kotlin" %}

```java
import io.nodle.sdk.android.Nodle

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Nodle.init(this)
    }
}

```

{% endtab %}
{% endtabs %}

After you have verified that you have the required permissions in the manifest and you are initializing Nodle then you can proceed to next steps. When passing (this) to Nodle you will pass your application Context. Like this Nodle will be able to stay alive in your global application state.&#x20;

## Step 5: Run the Nodle SDK

In the `onCreate` method of your launcher activity start Nodle by giving it the `ss58:public_key` generated in Step 1:

{% tabs %}
{% tab title="Java" %}

```java
import io.nodle.sdk.android.Nodle

// start the sdk
Nodle().start("ss58:public_key");
```

{% endtab %}

{% tab title="Kotlin" %}

```java
import io.nodle.sdk.android.Nodle

// start the sdk
Nodle().start("ss58:public_key")
```

{% endtab %}
{% endtabs %}

&#x20;You can find more info here: [Nodle SDK - Android API and Configuration](https://nodle.gitbook.io/nodle-sdk/nodle-sdk-nightly-android-api-and-configuration)

## Step 6 - Check Permission

The SDK expects a certain number of permission to be set. You must make sure that to request the following permissions from the user:&#x20;

* INTERNET
* BLUETOOTH
* BLUETOOTH\_ADMIN
* ACCESS\_FINE\_LOCATION&#x20;
* ACCESS\_COARSE\_LOCATION&#x20;
* ACCESS\_BACKGROUND\_LOCATION - **API 29 and ABOVE**
* BLUETOOTH\_SCAN - **API 31 (ANDROID 12)**
* BLUETOOTH\_ADVERTISE - **API 31 (ANDROID 12)**
* BLUETOOTH\_CONNECT - **API 31 (ANDROID 12)**

In order for NodleSDK to be able to work while in the background we require the **ACCESS\_BACKGROUND\_LOCATION** which Google Play Store has updated it's location policy and require extra steps for verification in Google Play Stores requiring the application developer to submit a video of the permissions usage. If you can't provide the requested usage description you can always strip the permission like this:

```java
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" tools:node="remove" />
```

You may use third party libraries to request the permission to the user. For instance you can use the [*vanniktech library*](https://github.com/vanniktech/RxPermission) with RxPermission :

```java
RealRxPermission.getInstance(this)
    .requestEach(
            Manifest.permission.INTERNET,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION)
    .reduce(true, (c, p) -> c && (p.state() == Permission.State.GRANTED))
    .subscribe((granted) -> {
            if (granted) {
               Log.d("Nodle","all the permissions was granted by user");
               Nodle.start("ss58:public_key"); 
            } else {
               Log.d("Nodle","some permission was denied by user");
            }
   });
```

You can still use the same third-party library for Android 12 by adding the following permissions:

```java
RealRxPermission.getInstance(this)
    .requestEach(
            Manifest.permission.INTERNET,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_ADVERTISE,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION)
    .reduce(true, (c, p) -> c && (p.state() == Permission.State.GRANTED))
    .subscribe((granted) -> {
            if (granted) {
               Log.d("Nodle","all the permissions was granted by user");
               Nodle.start("ss58:public_key"); 
            } else {
               Log.d("Nodle","some permission was denied by user");
            }
   });
```

**And there you have it! You’re good to go!**

## **Want to check your SDK rewards?**&#x20;

Currently we have our dashboard **under development** and rewards are not available. If you want see your rewards we have a workaround for you to do that.  Please follow the steps:

* Install <https://polkadot.js.org/extension/>
* Configure it by creating a dummy account
* Go to <https://nodleprotocol.io/#/chainstate>
* Select System -> Accounts

You should see something like this if you have setup the extension properly:

![](https://357248741-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lm5umGHRUTSRYfWgJkH%2F-MeTYZx--3SYDQxQVISC%2F-MeTYhl86EJCO5vZG5vZ%2FScreenshot%202021-07-13%20at%2009.28.26.png?alt=media\&token=872ac88a-d0a2-4999-b52a-19421808cd27)

* Then you should see you dummy accountId
* Click on the dummy accountId and paste your public\_key

![](https://357248741-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lm5umGHRUTSRYfWgJkH%2F-MeTYZx--3SYDQxQVISC%2F-MeTYuhHuqCT7p63XQqB%2FScreenshot%202021-07-13%20at%2009.28.48.png?alt=media\&token=c0ae0f06-ac67-43dc-850e-a4da671865be)

* Then you should only see your public\_key

![](https://357248741-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lm5umGHRUTSRYfWgJkH%2F-MeTYZx--3SYDQxQVISC%2F-MeT_0SFHGe3Q8t7KNqV%2FScreenshot%202021-07-13%20at%2009.35.31.png?alt=media\&token=6c0a036a-97a3-4f6e-a54e-1ab81f8aa154)

* Then click the green + on the right of the screen
* Wait a second your rewards should be shown soon

![](https://357248741-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lm5umGHRUTSRYfWgJkH%2F-MeTYZx--3SYDQxQVISC%2F-MeTZLl9Sr7r5ND1sFL-%2FScreenshot%202021-07-13%20at%2009.29.07.png?alt=media\&token=930bb5d2-6bd7-43d3-a134-550906641c4d)

That's it you should see your rewards. Make sure to add all permissions to the SDK in order to see your rewards. We have a lot of traffic so please bear with us since rewards might take a bit of time. But if you allow all the rules in SDK you should see the packets coming to the dashboard. Then rewards should be visible.
