1.1.1 Creating an Android Project

Source: Internet
Author: User
Tags android sdk manager

An Android project contains all the source code files that make up the Android app.

This section describes how to create a new project using Android Studio or the command line in SDK tools.

Note: Before we should have installed the Android SDK, if using Android Studio development, you should make sure that Android Studio is installed. Otherwise, please read the installing the Android SDK and follow the wizard to complete the installation steps.

Create a project using Android Studio

1. Use Android Studio to create an Android project and launch Android Studio.

    • If we haven't opened a project with Android studio yet, we'll see the Welcome page and click New Project.
    • If you have already opened a project with Android studio, click File in the menu and select New Project to create a new project.

2. Refer to Figure 1 in the popup window (Configure your new project) to fill in the content, click Next. Filling in the following values will make subsequent steps not easy to make mistakes.

    • Application Name here fill in the name of the app you want to present to the user, here we use the "My first app".
    • The company domain package name qualifier, which Android studio applies to each new Android project.
    • Package name is the app's bundle namespace (the concept of a package with Java), and the package name is unique across all installed apps on the same Android system, and we can edit the package name independently.
    • the project location operating system holds the catalog for the projects.

Figure 1 Configure Your new project

3. In the Select the form factors your app would run on window tick Phone and Tablet.

4. Minimum SDK, select API 8:android 2.2 (Froyo). The Minimum Required SDK represents the minimum Android version supported by our app, and in order to support as many devices as possible, we should set the minimum API version to support your app's core functionality. If some of the non-core features are supported only in the newer version of the API, you can turn them on only in versions that support these features (refer to compatible versions of the system), where the default values are used.

5. Do not tick other options (TV, Wear, and Glass) and click Next.

6. Select Basic activityin the Add an activity <template\> window and click Next.

7. Modify the Activity name to myactivityin the Choose options for your new file window, modify Layout name to activity_my, Title modified to myactivity, Menu Resource Name modified to menu_my.

8. Click Finish to complete the creation.

The Android project you just created is a basic Hello World project that contains some default files and we take a moment to look at the most important parts:

app/src/main/res/layout/activity_my.xml

This is the XML layout file for the new activity that was created when you built the project with Android Studio, and by the process of creating a new project, Android Studio will also present a text view and a graphical preview view of the file. The file contains some default settings and a display content of "Hello world!" The TextView element.

app/src/main/java/com.mycompany.myfirstapp/MyActivity.java

After you've created a new project with Android Studio, you can see the tab for that file in Android Studio, select the tab, and see the definition of the activity class you just created. After compiling and running the project, the activity starts and loads the layout file activity_my.xml, displaying a text: "Hello world!"

app/src/main/AndroidManifest.xml

The manifest file describes the basic features of the project and lists the components that make up the application, and the next study will take a closer look at the file and add more components to the file.

app/build.gradle

Android Studio uses Gradle to compile and run Android projects. Each module of the project, as well as the entire project, has a build.gradle file. Usually you just need to focus on the module's Build.gradle file, which holds the compilation dependency settings, including the Defaultconfig settings:

    • Compiledsdkversion is the target Android version that our app will compile, which defaults to the latest version of Android installed by your SDK (currently 4.1 or later, if you don't have an available Android version, use the SDK first Manager to complete the installation), we can still compile the project with older versions, but set the value to the latest version, use the latest Android features, and optimize the app on the latest devices to improve the user experience.
    • ApplicationID The package name that was specified when the new project was created.
    • Minsdkversion the minimum SDK version specified when creating the project, which is the minimum SDK version supported by the new app.
    • Targetsdkversion says you tested the highest Android version supported by your app (also indicated by the API level). When Android releases the latest version, we should test our app on the latest version of Android to update target at the same time. SDK to the latest version of Android to take full advantage of the features of the new Android version. For more information, please read supporting Different Platform Versions.

For more information on Gradle, read building Your Project with Gradle

Note The resources resource is also included in the/res directory:

drawable<density>/

Store various densities images of folders, mdpi,hdpi, etc., here to find the Application Runtime icon file Ic_launcher.png

layout/

Storing user interface files, such as the Activity_my.xml mentioned earlier, describes the myactivity corresponding user interface.

menu/

A file that stores the menu items defined in the app.

values/

Store other XML resource files, such as the string,color definition. String.xml defines the text "Hello world!" that appears when you run the app

To run this app, continue to the next section of the study.

To create a project using the command line

If you are not developing an Android project using Android Studio, we can use the tools provided by the SDK at the command line to create an Android project.

1. Open the command line to switch to the SDK root directory;

2. Implementation:

tools/android list targets

Will print out all of our Android SDK downloadable available Android platforms, find the target platform to create the project, record the platform corresponding ID, recommend using the latest platform. We can still make our app support older versions of platform, but setting it up to the latest version allows us to optimize our app for the latest Android devices. If you do not see any available platform, we need to complete the download installation using the Android SDK Manager, see Adding platforms and Packages.

3. Implementation:

android create project --target <target-id> --name MyFirstApp --path <path-to-workspace>/MyFirstApp --activity MyActivity --package com.example.myfirstapp

Replace with the <target-id> ID that was recorded in the previous step, replacing the <path-to-workspace> path where we want to save the project.

Tip: platform-tools/ tools/ It is easier to develop and add to environment variables PATH .

To this end, our Android project is already a basic "Hello World" program that contains some of the default files. To run it, continue to the next section of the study.

1.1.1 Creating an Android Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.