Creating and Configuring Your Project
To start off the tutorial, you need to create a new project and add the client library for the backend API. Then you need to configure the project for all the required dependencies, as we'll describe below.
Creating an Android Endpoints Project
To create a new project:
-
Start Android Studio. (In Linux, you do this by invoking
studio.sh
.) - Click New Project in the Quickstart pane.
-
Fill out the
New Project
form:
-
Supply
Hello Endpoints
as the Application name . -
Supply supply
HelloEndpoints
as the Module name . -
Supply supply
com.google.devrel.samples.helloendpoints
as the Package name . - Select API 9: Android 2.3 (Gingerbread) for the Minimum required SDK , select API 19: KitKat for the Target SDK , and select API 19: KitKat for Compile with . You don't have to use these API levels, but these are the ones we are using for this tutorial.
- Leave all of the other settings as is, accepting the defaults.
-
Supply
- Click Next .
- Click Next to accept the defaults when prompted to supply an image file and other settings.
- Accept the default Blank Activity by clicking Next .
- Accept the default activity name MainActivity by clicking Finish . Note that the HelloEndpoints project appears in the left panel, with the project subdirectories located under the project name.
-
Open a terminal window and change directory to
HelloEndpoints/HelloEndpoints
and create a subdirectory namedlibs
. - Locate the backend API client library Jar, which you generated following the instructions in setup .
-
Copy the backend client library Jar to the project's
/libs
directory: -
Return to the HelloWorld project in Android Studio, and select the library you just added, right-click, then select Add As Library to your project.
This completes the project creation, so let's proceed to configuring the
your project via the
build.gradle
file.
Configuring the Project
In Android Studio, your project uses the
build.gradle
file for dependencies
and other settings. This file is located at
HellowWorldProject/HelloWorld/src/build.gradle
:
To configure
build.gradle
:
-
Locate
HelloWorldProject/HelloWorld/src/build.gradle
in the Android Studio left pane, and double-click build.gradle to open it. -
Replace all of the contents with the following code:
apply plugin: 'android' android { compileSdkVersion 19 buildToolsVersion "19.0.0" defaultConfig { minSdkVersion 9 targetSdkVersion 19 versionCode 1 versionName "1.0" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } } dependencies { compile 'com.android.support:appcompat-v7:+' compile 'com.android.support:support-v4:19.0.+' // BEGIN Google APIs // Play Services will validate the application prior to allowing OAuth2 access. compile(group: 'com.google.android.gms', name: 'play-services', version: '4.2.+') // The following lines implement maven imports as defined at: // https://code.google.com/p/google-api-java-client/wiki/Setup // Add the Google API client library. compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.17.0-rc') { // Exclude artifacts that the Android SDK/Runtime provides. exclude(group: 'xpp3', module: 'xpp3') exclude(group: 'org.apache.httpcomponents', module: 'httpclient') exclude(group: 'junit', module: 'junit') exclude(group: 'com.google.android', module: 'android') } // Add the Android extensions for the Google API client library. // This will automatically include play services as long as you have download that library // from the Android SDK manager. // Add the Android extensions for the Google API client library. compile(group: 'com.google.api-client', name: 'google-api-client-android', version: '1.17.0-rc') { // Exclude play services, since we're not using this yet. exclude(group: 'com.google.android.google-play-services', module: 'google-play-services') } // END Google APIs // The following client libraries make HTTP/JSON on Android easier. // Android extensions for Google HTTP Client. compile(group: 'com.google.http-client', name: 'google-http-client-android', version: '1.17.0-rc') { exclude(group: 'com.google.android', module: 'android') } // This is used by the Google HTTP client library. compile(group: 'com.google.guava', name: 'guava', version: '14.0.+') compile fileTree(dir: 'libs', include: ['*.jar', '*.aar']) }
In this configuration, notice, under
dependencies
, the line:compile 'com.android.support:support-v4:19.0.+
Using v4 support lets you target API 8 and above, providing more APIs than v7.
-
Click File > Save All and then exit Android Studio and restart it.
-
Click Build > Rebuild Project .
With setup and configuration complete, you are now ready to start create the UI and add other code for a simple Android app.
Next
When you are finished with these tasks, continue on to Creating a Simple Android App .