1. Documents
  2. Android SDK

Using the LINE SDK for Android

You can integrate your Android apps with LINE using the LINE SDK for Android. This page explains how to embed the SDK in your Android apps.

Requirements

The following are required to use the LINE SDK for Android.

  • minSdkVersion set to 15 or higher (Android 4.0.3 or higher)
  • Channel ID of your Channel
  • Complete required fields on the technical configuration page

Note: Do not use resource IDs that start with “linesdk_” as this may cause conflicts with the resources that exist within the SDK.

Downloading the SDK

To download the LINE SDK for Android from the Channel Console, follow the steps below.

Download the LINE SDK for Android

step1

Select your Channel from the list on the left.

capture_011

step2

Click Download SDK.

Download SDK

step3

Click the link of the SDK file and save to any directory.

Download Android SDK

Note: We highly recommend using the latest version of the SDK. The previous versions of the SDK have been deprecated.

LINE starter application

The LINE starter application provides you with all the elements you need to start integrating the LINE SDK into your app.

Download the LINE starter application from the following link.

Installing the SDK

To install the LINE SDK for Android, follow the steps below.

Importing the library into your project

  1. Copy the SDK’s .aar file (line-sdk-4.0.0.aar) to your application’s libs folder.
    Android .aar file
  2. Add the libs folder as a repository to your top-level build.gradle file.
allprojects {
    repositories {
        ...
        flatDir {
            dirs 'libs'
        }
		...
    }
}
  1. Add the compile dependency to the LINE SDK in your module-level build.gradle file.
dependencies {
	...
    compile(name:'line-sdk-4.0.0', ext:'aar')
	...
}

Adding required Android support libraries

The LINE SDK requires the following Android support libraries to run properly:

  1. v7 appcompat library
  2. Custom Tabs

Make sure these are added to your module-level build.gradle file’s dependencies:

dependencies {
	...
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:customtabs:23.2.1'
	...
}

Android manifest file settings

The LINE SDK sends information via the internet, so you must add the INTERNET permission to your AndroidManifest.xml file.

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

Channel Console settings

To use the Android SDK, you need to get the Channel ID from the Basic information section of the Channel Console. The following fields must also be set in the Technical configuration section:

  • Android Package Name: Set the application’s package name.
  • Android Package Signature: Set as any value. If this value is not set, the login process will fail.

Login

To log in, your application must first get a login intent using either the getLoginIntent() or the getLoginIntentWithoutLineAppAuth() method. For both of these methods, you must pass the Channel ID and the context.

Logging in with Auto Login

getLoginIntent()

If you use getLoginIntent() to get the login intent, the application will behave as follows when the login activity is started.

  • If the LINE app is installed on the device, the application uses the credentials that the LINE app is using to log the user in without prompting them for their login credentials. This process is called LINE Auto Login.
  • If the LINE app is not installed on the device, the user is taken to the LINE Login screen in their browser and prompted for their LINE Login credentials (email address and password).

Example of calling getLoginIntent()

Intent loginIntent = LineLoginApi.getLoginIntent(v.getContext(), <CHANNEL ID>);

Logging in via the LINE Login screen

getLoginIntentWithoutLineAppAuth()

If you use getLoginIntentWithoutLineAppAuth() to get the login intent, your application does not attempt to use the LINE app to perform LINE Auto Login and goes straight to the LINE Login screen to prompt the user for their LINE login credentials (email address and password).

Example of calling getLoginIntentWithoutLineAppAuth()

Intent loginIntent = LineLoginApi.getLoginIntentWithoutLineAppAuth(v.getContext(), <CHANNEL ID>);

Starting authentication

After getting the login intent using your desired method, you can start the authentication process by calling startActivityForResult() and passing the login intent as a parameter.

startActivityForResult(loginIntent, REQUEST_CODE);

REQUEST_CODE is an integer value that is used to identify your request. Any integer can be used as the REQUEST_CODE.

After startActivityForResult is called, the application behavior differs depending on whether the LINE app is installed on the device or not.

After the user has logged in using one of the two methods above, the login result will be returned in the activity’s onActivityResult() method. Your application must override this method to handle the login result. Use the LineLoginResult object’s getResponseCode() method to determine if the login was successful. If getResponseCode() returns “SUCCESS”, the login was successful. Any other value indicates a failure. You can also determine the type of error based on the response code.

Response code Description
SUCCESS The login was successful.
CANCEL The login failed because the user canceled the login process.
SERVER_ERROR The login failed due to a server-side error.
NETWORK_ERROR The login failed because the SDK could not connect to the LINE Login servers.
INTERNAL_ERROR The login failed due to an unknown error.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode != REQUEST_CODE) {
        return;
    }
    LineLoginResult result = LineLoginApi.getLoginResultFromIntent(data);

    switch (result.getResponseCode()) {

        case SUCCESS:
			// Login is successful
			// Do something...
			break;
        case CANCEL:
			// Login was cancelled by the user
			// Do something...
                        break;
        default:
			// Login was cancelled by the user
			// Do something...
    }
}

Getting the access token

The login result contains a LineCredential() object which contains the user’s access token. Retrieve the access token from this object as follows.

String accessToken = result.getLineCredential().getAccessToken().getAccessToken();

Getting user profile information immediately after login

The LINE SDK automatically gets a user’s profile information upon logging in. Access this information by calling the LineLoginResult object’s getProfileInfo() method. The following example demonstrates how to get a user’s profile data from the login result and pass it into an intent.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


	...    

    result = LineLoginApi.getLoginResultFromIntent(data);

    switch (result.getResponseCode()) {

        case SUCCESS:

            Intent transitionIntent = new Intent(this, PostLoginActivity.class);

            transitionIntent.putExtra("display_name", result.getLineProfile().getDisplayName());
            transitionIntent.putExtra("status_message", result.getLineProfile().getStatusMessage());
            transitionIntent.putExtra("user_id", result.getLineProfile().getUserId());
            transitionIntent.putExtra("picture_url", result.getLineProfile().getPictureUrl().toString());
 
            ...
    }
}

Calling APIs

In addition to performing LINE authentication, the SDK can be used to call several APIs from your Android application. The APIs are called through LineApiClient. Create a static variable of the object and then use it to call various methods.

private static LineApiClient lineApiClient;

Initialize the LineApiClient variable on your activity’s onCreate() method as shown below. The Channel ID and the context are required for initialization.

LineApiClientBuilder apiClientBuilder = new LineApiClientBuilder(getApplicationContext(), <Channel ID>);
lineApiClient = apiClientBuilder.build();

APIs and the UI thread

All of the APIs in the Android SDK perform network operations and will cause NetworkOnMainThreadExceptions if called on the main thread. Therefore, you must run the APIs using AsyncTask.

Log out

LineApiResponse<?> logout()

The logout method invalidates the access token that is currently being used by the SDK. Use this method to add logout functionality to your Android application.

lineApiClient.logout();

Refresh access token

LineApiResponse<LineAccessToken> refreshAccessToken()

The refreshAccessToken() method refreshes the access token that is currently being used by the SDK. This API can be used for up to 10 days after the access token has expired. If the API call fails, have the user log out and log in again.

lineApiClient.refreshAccessToken();

Get current access token

LineApiResponse<LineAccessToken> getCurrentAccessToken()

This method retrieves the access token that is currently being used by the SDK.

String accessToken = lineApiClient.getCurrentAccessToken().getResponseData().getAccessToken();

Verify access token

LineApiResponse<LineCredential> verifyToken()

You can check to see if the access token is valid or not using the verifyToken() method. This method returns a LineApiResponse object that contains the result. You can call the object’s isSuccess() method to check if the token is valid. If isSuccess() is true, the token is valid. Otherwise the token is invalid, expired, or the verify API failed in some manner.

If isSuccess() is FALSE, you can use the LineApiResponse.getErrorData() method to get more information about why the verify method failed. Note that if isSuccess() is FALSE, getResponseData() will return NULL.

LineApiResponse verifyResponse = lineApiClient.verifyToken();
 
if (verifyResponse.isSuccess()) {
 
    Log.i(TAG, "getResponseData: " + verifyResponse.getResponseData().toString());
    Log.i(TAG, "getResponseCode: " + verifyResponse.getResponseCode().toString());
 
    return true;
 
} else {
 
    Log.i(TAG, "getResponseCode: " + verifyResponse.getResponseCode());
    Log.i(TAG, "getErrorData: " + verifyResponse.getErrorData());
 
    return false;
 
}

You can also get a list of permissions that are associated with the access token by calling LineApiResponse.getPermission(). The following example demonstrates how to display a list of an access token’s permissions in a toast.

...
  
protected void onPostExecute(LineApiResponse response){
  
    if (response.isSuccess()){
        StringBuilder toastStringBuilder = new StringBuilder("Access Token is VALID and contains the permissions: ");
         
        for (String temp : response.getResponseData().getPermission()) {
            toastStringBuilder.append(temp + ", ");
        }
        Toast.makeText(getApplicationContext(), toastStringBuilder.toString(), Toast.LENGTH_SHORT).show();
    }
    ...
}

Get profile information

LineApiResponse<LineProfile> getProfile()

This method retrieves the logged-in user’s profile information. The following is an example of how to save the profile information into strings.

LineApiResponse verifyResponse = lineApiClient.getMyProfile();
  
String displayName = apiResponse.getResponseData().getDisplayName();
String pictureURL = apiResponse.getResponseData().getPictureUrl().toString();
String userID = apiResponse.getResponseData().getUserId().toString();
String statusMessage = apiResponse.getResponseData().getUserId();