Alan Android SDK¶
Alan can be integrated with Android apps developed in Java and Kotlin.
Integrating with Alan¶
To add Alan voice to an Android app, you need to do the following:
Step 1. Add the Alan SDK dependency¶
You need to add the Alan SDK dependency to the Android app. You can do it in two ways:
Add a reference as a Maven dependency
Download the aar package and include it manually
Do the following:
Open the
build.gradle
file at the module level.In the
dependencies
block, add the dependency configuration for the Alan Android SDK:implementation 'app.alan:sdk:<latest-version>'
. To get the latest version, check out the Releases page for the Alan Android SDK GitHub repository.
dependencies {
/// Add Alan SDK dependency
implementation "app.alan:sdk:4.12.0"
}
Do the following:
Download the Alan SDK from the releases section in the Alan Git repository.
Put it in your
<project>/app/libs
folder. Create thelibs
folder if needed.Open the
build.gradle
file at the module level.In the
repositories
block, add the name of the folder in which the aar package resides.In the
dependencies
block, add the dependency configuration for the Alan Android SDK. To get the latest version, check out the Releases page for the Alan Android SDK GitHub repository.
repositories {
/// Add the following line to the repositories section
flatDir {
dirs 'libs'
}
}
dependencies {
/// Add the Alan SDK dependency
implementation (name: 'AlanSdk-4.12.0', ext: 'aar')
}
Step 2. Add the Alan button to the app layout¶
To add the Alan button to the layout of your app, use the AlanButton
class. The AlanButton
class provides a view with the voice button and instance methods to communicate with Alan Studio.
In activity_main.xml
, create a new AlanButton
instance using the following XML layout:
<com.alan.alansdk.button.AlanButton
android:id="@+id/alan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:visibility="visible"
app:button_horizontal_align="right"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
Note
The layout_width
param should be set to match_parent
.
Step 3. Add the AlanConfig object¶
You need to add the AlanConfig
object to your app. This object describes the parameters that are provided for AlanButton
. In the
MainActivity.java
file (for Android Java apps) or the MainActivity.kt
file (for Android Kotlin apps), use AlanConfig.Builder
to create a config with the necessary parameters:
Name |
Type |
Description |
---|---|---|
projectId |
String |
The Alan SDK key for a project in Alan Studio. |
dialogId |
String |
(Optional) The dialog ID to connect to the necessary dialog. |
dataObject |
String or JSONObject |
(Optional) A valid JSON string or JSON object with authentication or configuration data to be sent to the voice script. For details, see authData. |
public class MainActivity extends AppCompatActivity {
/// Add the alanButton variable
private AlanButton alanButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/// Set up the Alan button
AlanConfig config = AlanConfig.builder().setProjectId("").build();
alanButton = findViewById(R.id.alan_button);
alanButton.initWithConfig(config);
}
}
class MainActivity : AppCompatActivity() {
/// Add the alanButton variable
private var alanButton: AlanButton? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/// Set up the Alan button
val config = AlanConfig.builder().setProjectId("").build()
alanButton = findViewById(R.id.alan_button)
alanButton?.initWithConfig(config)
}
}
Step 4. Connect to the Alan Studio project¶
As the last step, you need to connect the Android app to the Alan Studio project in which you create voice scripts for the app. In
setProjectId
of MainActivity
, provide the Alan SDK Key for your project in Alan Studio. To get the key, in Alan Studio, at the top of the code editor click Integrations and copy the key value from the Alan SDK Key field.
public class MainActivity extends AppCompatActivity {
private AlanButton alanButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/// Define the project key
AlanConfig config = AlanConfig.builder().setProjectId("e40fc04e9aff7b4b7d43485dbc3cb44a2e956eca572e1d8b807a3e2338fdd0dc/stage").build();
alanButton = findViewById(R.id.alan_button);
alanButton.initWithConfig(config);
}
}
class MainActivity : AppCompatActivity() {
private var alanButton: AlanButton? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/// Define the project key
val config = AlanConfig.builder().setProjectId("e40fc04e9aff7b4b7d43485dbc3cb44a2e956eca572e1d8b807a3e2338fdd0dc/stage").build()
alanButton = findViewById(R.id.alan_button)
alanButton?.initWithConfig(config)
}
}
Your app is now integrated with Alan. You can now add voice commands to the script in Alan Studio, run the app, tap the Alan button and interact with the app with voice.
Switching between logging levels¶
You can switch to the extended logging level with a static enableLogging
method of the Alan object:
Alan.enableLogging(true);
Troubleshooting¶
To troubleshoot problems you may have with your Android app, check the solutions below:
The minimum possible Android SDK version required by the Alan SDK is 21. If the version in your project is lower, you may encounter the following error:
AndroidManifest.xml Error: uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [:alan_voice]
. Open theapp/build.gradle
file, underdefaultConfig
, locateminSdkVersion
and change the version to 21.If gradle fails to get all dependencies required by the Alan SDK, you will encounter the following error:
Could not find <dependency-name> required by project :app > alan.app:sdk:x.xx.x
.Make sure
jcenter()
is added to your project:In your project, open the
settings.gradle
file.Check the repositories list. If
jcenter()
is not in the list, add it:settings.gradle¶dependencyResolutionManagement { repositories { mavenCentral() jcenter() } }
Sync and run your project.
What’s next?¶
Upon integration, your app gets the in-app voice assistant that can be activated with the Alan button displayed on top of the app’s UI.
To build a full-fledged multimodal UX, you can use Alan’s SDK toolkit:
Client API methods
Enable communication between the client app and Alan and perform actions in the app.
Alan handlers
Handle commands, understand the button state and capture events in the client app.
Example apps
Find and explore examples of voice-enabled apps on the Alan AI GitHub repository.