1. Documents
  2. Android SDK

(Deprecated) Developing with the LINE SDK for Android v1

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.

Deprecated version

This information is for a deprecated version of the LINE SDK for Android (v1) and should be used for reference purposes only. For information on the latest version, see Using the LINE SDK for Android.

Prerequisites

The following information is required to use the LINE SDK.

Information Description
Channel ID Identifier issued when creating your Channel. For information on how to create Channels, refer to Creating a Channel.
Scheme Used to receive request tokens and other information from the LINE app. Must follow this naming convention: “line.[Channel ID]”. For example, if your Channel ID is “12345”, the scheme will be “line.12345″.

Downloading the SDK

The LINE SDK for Android can be downloaded from the Channel Console. To download the SDK archive file, 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.

capture_023

step3

Click the link of the SDK archive file.

capture_024

Save the archive file into any directory.
Note: the download link is displayed only after the Channel status becomes DEVELOPING.

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 import the SDK into your app, follow the steps below:

Install the SDK

step1

Download an archive file of the LINE SDK for Android.

step2

Extract the archive file to any directory.

capture_019

step3

Copy the line-android-sdk-3.*.*.jar file to the libs directory in your Android project.

capture_020

step4

Copy the libs directory to your project directory.

capture_021

Settings

For the SDK to send information via the internet, add the following to the AndroidManifest.xml file. Specify an android:screenOrientation attribute value appropriate for your app.

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

To receive results of the authentication and authorization process, add the following activities to the AndroidManifest.xml file

<activity android:name="jp.line.android.sdk.activity.LineAuthCompleteActivity"
          android:configChanges="keyboard|orientation"
          android:exported="true"
          android:screenOrientation="sensor">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="line.12345" />
    </intent-filter>
</activity>

Specify the scheme in the android:scheme attribute of the data element. The android:exported property value must be set to true. If false, your app will not launch after authentication and authorization.

To authenticate and authorize users who do not have LINE installed on their device, add the following activity definitions to the AndroidManifest.xml file.

<activity android:name="jp.line.android.sdk.activity.WebLoginActivity"
          android:exported="false"
          android:configChanges="keyboard|orientation"
          android:screenOrientation="sensor">
</activity>

Add the following metadata elements to the AndroidManifest.xml file.

<meta-data
    android:name="jp.line.sdk.ChannelId"
    android:value="12345" />
<meta-data
    android:name="jp.line.sdk.AuthScheme"
    android:value="line.12345" />

The metadata are described below:

Name Type Description
jp.line.sdk.ChannelId int Channel ID
jp.line.sdk.AuthScheme String Authentication scheme

Initialization

The following settings can be made when using the SDK.

  • Initialize LineSdkContextManager
  • Get LineSdkContext instance

The LINE SDK for Android must be initialized before it can be used. To initialize the SDK, call the LineSdkContextManager.initialize(Context applicationContext) method. We recommend doing the initialization in Application.onCreate.

import android.app.Application;
import jp.line.android.sdk.LineSdkContextManager;

public class SampleLineSdkApplication extends Application {
    public void onCreate() {
        super.onCreate();
        ...
        LineSdkContextManager.initialize(this);
    }
    ...
}

It will take a moment for the SDK to initialize but no ANR will occur because initialization is executed asynchronously.

After the SDK is initialized, the LineSdkContext instance is created. Retrieve the configuration values for each API in the LineSdkContext instance from the LineSdkContextManager.

LineSdkContext sdkContext = LineSdkContextManager.getSdkContext();

Main methods on the LineSdkContext interface are as follows:

Method Description
ApiClient getApiClient() Retrieve the ApiClient instance to use each API.
LineAuthManager getAuthManager() Retrieve the LineAuthManager instance for authentication and authorization.
int getChannelId() Retrieve the Channel ID.
String getSdkVersion() Retrieve the version number of the LINE SDK for Android.

Login

The authentication and authorization process (the user logs in and agrees to the permissions) is called using the LineAuthManager instance. Either the login() or loginByAccount() method of the LineAuthManager class can be used.

Method Case
login(Activity activity)
login(Activity activity,Locale locale)
If LINE is installed on the user’s device, the SDK delegates authentication and authorization to LINE. If LINE is not installed, the SDK performs the authentication and authorization process.

Locale defines the language for the login screen. If Locale is not defined, the system language of the device is used. List of available languages.
loginByAccount(Activity activity)
loginByAccount(Activity activity,Locale locale)
The SDK shows the login and permissions screens whether LINE is installed or not.

Locale defines the language for the login screen. If Locale is not defined, the system language of the device is used. List of available languages.

For example:

LineSdkContext sdkContext = LineSdkContextManager.getSdkContext();
LineAuthManager authManager = sdkContext.getAuthManager();
Activity activity = ...; // Use "this" when performing in any Activit class
LineLoginFuture loginFuture = authManager.login(activity);
loginFuture.addFutureListener(new LineLoginFutureListener() {
    @Override
    public void loginComplete(LineLoginFuture future) {
        switch(future.getProgress()) {
            case SUCCESS: Login successfully
                // Do something...
                break;
            case CANCELED: // Login canceled by user
                // Do something...
                break;
            default: // Error
                // Do something...
                break;
        }
    }
});

The LINE SDK for Android automatically determines whether LINE is installed on the user’s device. If LINE is installed, the SDK delegates the login process to LINE. If not, the SDK will display the login screen and authenticate the user using the user’s email address and password.

If the login process fails due to an error, the appropriate error handling process must be run. Use LineSdkLoginFuture to find the exception that occurred. Getting a LineSdkLoginException indicates that the login process could not be performed successfully. This does not include server errors or network errors.

Throwable cause = future.getCause();
if (cause instanceof LineSdkLoginException) {
    LineSdkLoginException loginException = (LineSdkLoginException)cause;
    LineSdkLoginError error = loginException.error;
    switch(error) {
        case FAILED_START_LOGIN_ACTIVITY:
            // Failed launching LINE application or WebLoginActivity (Activity may be null)
            break;
        case FAILED_A2A_LOGIN:
            // Failed LINE login
            break;
        case FAILED_WEB_LOGIN:
            // Failed Web login
            break;
        case UNKNOWN:
            // Un expected error occurred
            break;
    }
} else {
    // Check other exceptions
}

Automatically refreshing access tokens

Every access token has an expiration date. Your app can use an access token up until the expiration date. Once expired, the access token will be rendered invalid and API calls will fail. However, you can have the access token reissued with a refresh token. The LINE SDK for Android automatically keeps access tokens up-to-date by automatically refreshing access tokens when they expire. This is executed when calling an API method.

You can use the OnAccessTokenChangedListener event listener to determine when an access token was refreshed. This is an example of how to use it:

LineAuthManager authManager = ...;
authManager.addOnAccessTokenChangedListener(new OnAccessTokenChangedListener() {
    @Override
    public void onAccessTokenChanged(final AccessToken newAccessToken) {
        // do something...
    }
});

Logout

If you want to add a logout feature to your Android app, you must make sure the information in the access token is invalidated. There are two methods to void the access token:

  • Access the API via the back-end server
  • Call the invalidation function from the SDK

To void the access token and other information from the SDK, call the logout() method of the LineAuthManager class.

LineAuthManager authManager = LineSdkContextManager.getSdkContext();
authManager.logout();

The access token cached in SharedPreference of a user’s Android device is deleted by calling the logout() method. This is the only way to delete this access token. The access token is not deleted if it has already been invalidated.

Calling APIs

Certain APIs can be used directly with the LINE SDK for Android. The LINE SDK for Android provides methods to call these APIs. These methods are defined in the ApiClient interface. You can retrieve the ApiClient instance from LineSdkContext.

Because API calls are executed asynchronously, you must register a listener to receive the results of API calls. The following shows how to call an API to retrieve a user’s profile information.

LineSdkContext sdkContext = LineSdkContextManager.getSdkContext();
ApiClient apiClient = sdkContext.getApiClient();
apiClient.getMyProfile(new ApiRequestFutureListener() {
    @Override
    public void requestComplete(ApiRequestFuture future) {
        switch(future.getStatus()) {
            case SUCCESS:
                Profile profile = future.getResponseObject();
                // Do something...
                break;
            default:
                // Failed to call API
                break;
        }
    }
});

The requestComplete() method is executed after an API call. The result of the API call can be retrieved from the ApiRequestFuture argument. The API call is successful if the result of the future.getStatus() method is SUCCESS. The getResponseObject() method returns an object that contains the result of the API call.

If the result is not SUCCESS, the API call has failed. You can determine why the API call failed by checking the exception that occurred. Use the future.getCause() method to retrieve the exception.

Throwable cause = future.getCause();
if (cause instanceof LineSdkApiException) {
    LineSdkApiException apiException = (LineSdkApiException)cause;
    if (apiException.isAccessTokenExpired()) {
        // The access token was already expired.
        // Should logout and remove the access token.
    } else {
        LineSdkApiError error = apiException.apiError;
        int httpStatusCode = apiException.httpStatusCode;
        switch(error) {
            case NOT_FOUND_ACCESS_TOKEN:
                // Access token not found.
                break;
            case SERVER_ERROR:
                // The error was returned from a server.
                LineSdkApiServerError serverError = apiException.serverError;
                int statusCode = serverError.statusCode;
                String statusMessage = serverError.statusMessage;
                // Do something...
                break;
            case ILLEGAL_RESPONSE:
                // A format of the response was unusual.
                break;
            case UNKNOWN:
                // Unexpected error occurred.
                break;
        }
    }
} else {
    // Check other exceptions
}

The isAccessTokenExpired() method of LineSdkApiException determines whether the access token has expired or not. If the isAccessTokenExpired() method returns true, your app will call the logout() method of the LineAuthManager class to clear the access token.

If there is a LineSdkApiException and the isAccessTokenExpired() method returns false, then the API server has returned an error. You can retrieve the error code from LineSdkApiException using the LineSdkApiException.apiError field value. The error codes are listed below:

Error code Description
NOT_FOUND_ACCESS_TOKEN Access token not found. Access token needs to be reissued.
SERVER_ERROR Error returned from server.
ILLEGAL_RESPONSE Format of the response irregular.
UNKNOWN Unexpected error occurred.

If the API server returns an error, you can retrieve the error information using LineSdkApiException.serverError. Your app must then handle the errors appropriately.

Proper error handling must also be implemented for other exceptions (for example: network errors).

Two ways of calling APIs: From the SDK and from the server

With the LINE SDK for Android versions 3 and higher, you can use certain APIs directly without accessing a server. This means you don’t need to worry about access tokens because the SDK handles it automatically.

For APIs that cannot be used directly from the SDK, a registered server is required. To use these APIs, access tokens must be transferred from the SDK to the server.

APIs are called as follows:

  1. The app calls the login function provided by the SDK.
  2. The SDK displays the login and permission pages. User enters login information and agrees to permissions.
  3. The LINE Platform issues an access token. The SDK receives and stores the access token.
  4. For APIs which can be called directly, the app calls methods to call APIs.
  5. For APIs which cannot be called directly, the app sends the access token to a back-end server. The server calls REST APIs with the access token.