Google Play Android Setup

A complete guide to the basics of adding Nami to your Google Play Android app.

📘

This doc supports the Android SDK through version 2.0.
Looking for the newest Nami SDK? Check out the latest docs

Please make sure you have completed the setup of your app on the Nami Control Center for all these steps to be successful.

Adding Nami to your app has a few steps for a basic app.

  1. Add the SDK to your project
  2. Configure the SDK
  3. Show a paywall in your app
  4. React to a purchase to grant access to paid content or features

We'll run through each of these below.

Add the SDK to your project

📘

Google Play Android Requirements

  • Android SDK minimum version 26
  • SDK builds target Android 12 (API version 31)
  • SDK has been built with Java v8 and Kotlin v1.4.31

Our Android SDK is available via our Maven repository. Add the following code snippet to your project's build.gradle to add the Nami SDK to your project.

repositories {
  maven { url "https://nami-android.s3.amazonaws.com/" }
}

Then in the build.gradle for your app add the following code, replacing the version number with whichever version of the Nami SDK you would like to run.

dependencies {
  implementation "com.namiml:sdk-android:2.0.0" 
}

Add compileOptions for Java 8 compatibility in build.gradle.

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  // For Kotlin projects
  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
  }
}

Configure the SDK

📘

Use Android Studio to Get Code Documentation

The Nami SDK was built with Dokka and if you use Android Studio, you'll get helpful code completions and documentation about methods.

We recommend that you configure the Nami SDK as early in your app's launch as possible. This will ensure the SDK is ready to receive and process purchases.

The best spot to do this is in the onCreate() method in your class that creates your Application and inherits from Application(). Here's a full code example.

You'll need to go find your App Platform ID in the Control Center for this step.

import com.namiml.Nami
import com.namiml.NamiConfiguration
import com.namiml.NamiLogLevel
  
class DemoApplication : Application() {
  
  companion object {
    private const val NAMI_APP_PLATFORM_ID = "YOUR_APP_PLATFORM_ID"
  }
    
  override fun onCreate() {
    super.onCreate()
    Nami.configure(
      NamiConfiguration.build(this, NAMI_APP_PLATFORM_ID) {
        LogLevel = NamiLogLevel.INFO.takeIf { BuildConfig.DEBUG } ?: NamiLogLevel.WARN
      }
    )
  }
}
import com.namiml.Nami;
import com.namiml.NamiConfiguration;

import com.namiml.NamiLogLevel;

public class BasicApplication extends Application {
  private static final String NAMI_APP_PLATFORM_ID = "YOUR_APP_PLATFORM_ID";
  
  @Override
  public void onCreate() {
    super.onCreate();
    NamiConfiguration.Builder builder = new NamiConfiguration
      .Builder(this, NAMI_APP_PLATFORM_ID);
      
    if (BuildConfig.DEBUG) {
      builder.logLevel(NamiLogLevel.INFO);
    }

    Nami.configure(builder.build());
  }
}

Nami recommends setting the log level to WARN for apps on the store. INFO may be helpful during development to better understand what is going on. DEBUG level has a lot of information and is likely only helpful to the Nami support team.

Show a paywall

Now that you have the SDK configured, let's show a paywall in your app. This step requires that you have a Configured paywall in the Nami Control Center and that you have a live Campaign with a User-Initiated Paywall.

You may also optionally check if the SDK is able to raise the paywall at the time you are trying to display it.

import com.namiml.paywall.NamiPaywallManager
import android.util.Log
  
const val LOG_TAG = "ExampleApp"
  
class ExampleActivity : AppCompatActivity() {
  
  private fun onPaywallRaise(Activity activity) {
    NamiPaywallManager.preparePaywallForDisplay { success, error ->
      if (success) {
        NamiPaywallManager.raisePaywall(activity)
      } else {
        Log.w(LOG_TAG, "preparePaywallForDisplay failed -> $error")
      }
    }
  }
}
import com.namiml.paywall.NamiPaywallManager;
import android.util.Log;

public class ExampleActivity extends AppCompatActivity {
  private static final String LOG_TAG = "ExampleApp";
	
  private void onRaisePaywall(Activity activity) {
    NamiPaywallManager.preparePaywallForDisplay((success, error) -> {
      if (success) {
        NamiPaywallManager.raisePaywall(activity);
      } else {
        Log.w(LOG_TAG, "preparePaywallForDisplay failed --> " + error);
      }
      return Unit.INSTANCE;
    });
  }
}

Grant access to paid app features

Once a user has made a purchase, you'll need to make sure to give them access to the content and features in your app that require a purchase. This is managed on the Nami platform through our entitlement engine.

The first option is to check whether a specific entitlement is active. This is done with the following code.

if (NamiEntitlementManager.isEntitlementActive("premium_access")) {
 	// allow access to premium app features 
}
if (NamiEntitlementManager.isEntitlementActive("premium_access") {
  // allow access to premium app content 
}

Nami also triggers a callback any time there is a change to the state of an entitlement. In the callback the full list of currently active entitlements is provided. You can use this to store the state of whether a user has access to premium features locally in your app.

It is important that any callbacks are created as early in the app launch as possible. We recommend adding the callback handlers in the NamiSetup method in your AppDelegate.cs.

NamiEntitlementManager.registerEntitlementChangeListener { activeEntitlements ->
  // process active entitlements to grant access
  for (val ent in activeEntitlements) {
    val ent_id = ent.referenceId;
    // use the active entitlements reference IDs to grant
    // access to premium app features
  }
}
import com.namiml.entitlement.NamiEntitlementManager
  
NamiEntitlementManager.registerEntitlementChangeListener { activeEntitlements ->
  // process List<NamiEntitlement> to grant correct access
}

That's all the basics to get up and running with Nami for Google Play - Android apps. For more use cases, explore the rest of our docs.