This page describes how to get set up with the Android SDK. If you haven't completed the prerequisites, complete them first.
Dependency
Import the "searchinapps" SDK into an Android project from GMaven. Open your
project's build.gradle
file, declare the Google Maven repository and add
the SDK dependency:
repositories {
google()
...
}
dependencies {
implementation 'com.google.android.libraries.searchinapps:searchinapps:[version]'
...
}
Then build your project using Gradle.
AndroidManifest.xml configuration
Update your Android project AndroidManifest.xml
file to add the following
metadata:
com.google.searchinapps.API_KEY
: the string value of your SDK api key (see above).com.google.searchinapps.CLIENT_ID
: the string value of your app's client identifier (see above).
Sample AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.samples.quickstart.searchinapps">
<application
...
>
<meta-data
android:name="com.google.searchinapps.API_KEY"
android:value="[your api key]"/>
<meta-data
android:name="com.google.searchinapps.CLIENT_ID"
android:value="[your client id]"/>
...
</application>
</manifest>
Initialization
To use "searchinapps" SDK's functions, in the target Activity
or any classes
that's responsible for retrieving search results, create a
SearchInAppsService
instance (you can do it in the Activity class's onCreate
function) and also pass activity or application Context into it.
Sample code
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.SearchInAppsService;
...
public class MainActivity extends AppCompatActivity {
private SearchInAppsService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import com.google.android.libraries.searchinapps.SearchInAppsService
...
class MainActivityJetpack : AppCompatActivity() {
private var service: SearchInAppsService? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SearchSuggestionsUI()
}
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
...
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
...
}
...
}