In this codelab, you'll learn how lớn build & run your first game app in the Java programming language. (If you're looking for the Kotlin version of this codelab, you can go here.)

What you must know already

This codelab is written for programmers & assumes that you know either the Java or Kotlin programming language. If you are an experienced programmer and adept at reading code, you will likely be able to follow this codelab, even if you don't have much experience with Java.

Bạn đang xem: Xuất File Apk Trong

What you"ll learn

How lớn use game Studio to build your app.How lớn run your ứng dụng on a device or in the emulator.How to add interactive buttons.How to lớn display a second screen when a button is pressed.

Use apk Studio and Java lớn write game apps

You write apps in the Java programming language using an IDE called Studio. Based on JetBrains' IntelliJ IDEA software, game Studio is an IDE designed specifically for apk development.

Note: This version of the codelab requires Studio 3.6 or higher.

To work through this codelab, you will need a computer that can run Studio 3.6 or higher (or already has game Studio 3.6 or higher installed).

2. Install app Studio

Note: This version of the codelab requires apk Studio 3.6 or higher.

You can tải về Studio 3.6 from the app Studio page. Studio provides a complete IDE, including an advanced code editor & app templates. It also contains tools for development, debugging, testing, and performance that make it faster và easier lớn develop apps. You can use app Studio to chạy thử your apps with a large range of preconfigured emulators, or on your own điện thoại device. You can also build production apps and publish apps on the Google Play store.

Note: apk Studio is continually being improved. For the latest information on system requirements & installation instructions, see the Studio download page. Studio is available for computers running Windows or Linux, và for Macs running macOS. The OpenJDK (Java Development Kit) is bundled with game Studio.

The installation is similar for all platforms. Any differences are noted below.

Accept the mặc định configurations for all steps, & ensure that all components are selected for installation.After the install is complete, the thiết đặt wizard downloads & installs additional components, including the SDK. Be patient, because this process might take some time, depending on your internet speed.When the installation completes, Studio starts, và you are ready lớn create your first project.

Troubleshooting: If you run into problems with your installation, see the game Studio release notes or Troubleshoot game Studio.

3. Task: Create your first project

In this step, you will create a new apk project for your first app. This simple phầm mềm displays the string "Hello World" on the screen of an virtual or physical device.

Here's what the finished tiện ích will look like:


What you"ll learn

How to create a project in game Studio.How to create an emulated apk device.How to lớn run your phầm mềm on the emulator.How to lớn run your ứng dụng on your own physical device, if you have one.

Step 1: Create a new project

Open Studio.In the Welcome to Studio dialog, click Start a new app Studio project.
Select Basic Activity (not the default). Click Next.
Give your application a name such as My First App.Make sure the Language is set to lớn Java.
Leave the defaults for the other fields.Click Finish.

After these steps, app Studio:

Creates a folder for your game Studio project called MyFirstApp. This is usually in a thư mục called below your home directory.Opens the code editor showing your project.

Step 2: Get your screen phối up

When your project first opens in game Studio, there may be a lot of windows and panes open. Lớn make it easier lớn get khổng lồ know app Studio, here are some suggestions on how to customize the layout.

If there's a Gradle window mở cửa on the right side, click on the minimize button () in the upper right corner lớn hide it.
Depending on the form size of your screen, consider resizing the pane on the left showing the project folders to take up less space.

At this point, your screen should look a bit less cluttered, similar khổng lồ the screenshot shown below.


Step 3: Explore the project structure and layout

The upper left of the game Studio window should look similar to the following diagram:


Based on you selecting the Basic Activity template for your project, app Studio has set up a number of files for you. You can look at the hierarchy of the files for your app in multiple ways, one is in Project view. Project view shows your files và folders structured in a way that is convenient for working with an project. (This does not always match the tệp tin hierarchy! to see the file hierarchy, choose the Project files view by clicking (3).)

Double-click the app (1) folder to expand the hierarchy of phầm mềm files. (See (1) in the screenshot.)If you click Project (2), you can hide or show the Project view. You might need to select View > Tool Windows lớn see this option.The current Project view selection (3) is Project >

In the Project > view you see three or four top-level folders below your app folder: manifests, java, java (generated)res. You may not see java (generated) right away.

Expand the manifests folder.

This thư mục contains This file describes all the components of your app app & is read by the app runtime system when your app is executed. 2. Expand the java folder. All your Java language files are organized here. The java folder contains three subfolders:

com.example.myfirstapp: This thư mục contains the Java source code files for your app.

com.example.myfirstapp ( This folder is where you would put your instrumented tests, which are tests that run on an app device. It starts out with a skeleton kiểm tra file.

com.example.myfirstapp (test): This thư mục is where you would put your unit tests. Unit tests don't need an device khổng lồ run. It starts out with a skeleton unit thử nghiệm file. 3. Expand the res folder. This folder contains all the resources for your app, including images, layout files, strings, icons, & styling. It includes these subfolders:

drawable: All your app's images will be stored in this folder.

layout: This folder contains the UI layout files for your activities. Currently, your ứng dụng has one activity that has a layout tệp tin called activity_main.xml. It also contains content_main.xml, fragment_first.xml, and fragment_second.xml.

menu: This folder contains XML files describing any menus in your app.

mipmap: This thư mục contains the launcher icons for your app.

navigation: This folder contains the navigation graph, which tells game Studio how khổng lồ navigate between different parts of your application.

values: This thư mục contains resources, such as strings and colors, used in your app.

Step 4: Create a virtual device (emulator)

In this task, you will use the apk Virtual Device (AVD) manager lớn create a virtual device (or emulator) that simulates the configuration for a particular type of app device.

The first step is lớn create a configuration that describes the virtual device.

In app Studio, select Tools > AVD Manager, or click the AVD Manager icon in the toolbar.
Click +Create Virtual Device. (If you have created a virtual device before, the window shows all of your existing devices and the +Create Virtual Device button is at the bottom.) The Select Hardware window shows a các mục of pre-configured hardware device definitions.Choose a device definition, such as Pixel 2, and click Next. (For this codelab, it really doesn't matter which device definition you pick).In the System Image dialog, from the Recommended tab, choose the latest release. (This does matter.)If a Download liên kết is visible next khổng lồ a latest release, it is not installed yet, và you need to download it first. If necessary, click the link to start the download, and click Next when it's done. This may take a while depending on your connection speed.

Note: System images can take up a large amount of disk space, so just download what you need.

In the next dialog box, accept the defaults, & click Finish.

The AVD Manager now shows the virtual device you added.

If the Your Virtual Devices AVD Manager window is still open, go ahead and close it.

Xem thêm: Kể Truyện Đêm Khuya - Nhân Quả Và Số Phận Con Người

Step 5: Run your phầm mềm on your new emulator

In apk Studio, select Run > Run ‘app' or click the Run icon in the toolbar.
The icon will change when your ứng dụng is already running.

If you get a dialog box stating "Instant Run requires that the platform corresponding khổng lồ your target device ( N...) is installed" go ahead và click Install and continue.

In Run > Select Device, under Available devices, select the virtual device that you just configured. This thực đơn also appears in the toolbar.


The emulator starts và boots just like a physical device. Depending on the speed of your computer, this may take a while. You can look in the small horizontal status bar at the very bottom of app Studio for messages khổng lồ see the progress.

Messages that might appear briefly in the status bar

Gradle build running


Waiting for target device to come on line


Installing APK


Launching activity


Once your phầm mềm builds & the emulator is ready, game Studio uploads the app to the emulator & runs it. You should see your phầm mềm as shown in the following screenshot.


Note: It is a good practice lớn start the emulator at the beginning of your session. Don't close the emulator until you are done testing your app, so that you don't have khổng lồ wait for the emulator khổng lồ boot again. Also, don't have more than one emulator running at once, to reduce memory usage.

Step 6: Run your app on a device (if you have one)

What you need:

An app device such as a phone or tablet.A data cable khổng lồ connect your game device to your computer via the USB port.

Run your tiện ích on a device

To let Studio communicate with your device, you must turn on USB Debugging on your app device.

On apk 4.2 and higher, the options screen is hidden by default. To show options and enable USB Debugging:

On your device, open Settings > About phone & tap Build number seven times.Return khổng lồ the previous screen (Settings). options appears at the bottom of the list. Tap options.Enable USB Debugging.

Now you can connect your device and run the phầm mềm from app Studio.

Connect your device khổng lồ your development machine with a USB cable. On the device, you might need to agree lớn allow USB debugging from your development device.In game Studio, click Run
in the toolbar at the đứng top of the window. (You might need khổng lồ select View > Toolbar to see this option.) The Select Deployment Target dialog opens with the list of available emulators và connected devices.Select your device, và click OK. Studio installs the app on your device and runs it.

Note: If your device is running an apk platform that isn't installed in app Studio, you might see a message asking if you want khổng lồ install the needed platform. Click Install and Continue, then click Finish when the process is complete.


If you're stuck, quit app Studio and restart it.

If game Studio does not recognize your device, try the following:

Disconnect your device from your development machine và reconnect it.Restart Studio.

If your computer still does not find the device or declares it "unauthorized":

Disconnect the device.On the device, open Settings-> Options.Tap Revoke USB Debugging authorizations.Reconnect the device khổng lồ your computer.When prompted, grant authorizations.

If you are still having trouble, kiểm tra that you installed the appropriate USB driver for your device. See the Using Hardware Devices documentation.

Check the troubleshooting section in the game Studio documentation.

Step 7: Explore the phầm mềm template

When you created the project and selected Basic Activity, Studio set up a number of files, folders, and also user interface elements for you, so you can start out with a working app & major components in place. This makes it easier to lớn build your application.

Looking at your ứng dụng on the emulator or your device, in addition to the Next button, notice the floating kích hoạt button with an thư điện tử icon.

If you tap that button, you'll see it has been phối up lớn briefly show a message at the bottom of the screen. This message space is called a snackbar, và it's one of several ways to lớn notify users of your phầm mềm with brief information.


At the đứng đầu right of the screen, there's a thực đơn with 3 vertical dots.

If you tap on that, you'll see that Studio has also created an options menu with a Settings item. Choosing Settings doesn't bởi anything yet, but having it mix up for you makes it easier to địa chỉ user-configurable settings lớn your app.

Later in this codelab, you'll look at the Next button and modify the way it looks và what it does.

4. Task: Explore the layout editor

Generally, each screen in your app is associated with one or more fragments. The single screen displaying "Hello first fragment" is created by one fragment, called FirstFragment. This was generated for you when you created your new project. Each visible fragment in an game app has a layout that defines the user interface for the fragment. Apk Studio has a layout editor where you can create và define layouts.

Layouts are defined in XML. The layout editor lets you define & modify your layout either by coding XML or by using the interactive visual editor.

Every element in a layout is a view. In this task, you will explore some of the panels in the layout editor, and you will learn how khổng lồ change property of views.

What you"ll learn

How lớn use the layout editor.How lớn set property values.How to add string resources.How to showroom color resources.

Step 1: open the layout editor

Find and mở cửa the layout thư mục (app > res > layout) on the left side in the Project panel.
Double-click fragment_first.xml.

Troubleshooting: If you don't see the tệp tin fragment_first.xml, confirm you are running app Studio 3.6 or later, which is required for this codelab.

The panels khổng lồ the right of the Project view comprise the Layout Editor. They may be arranged differently in your version of game Studio, but the function is the same.


On the left is a Palette (1) of views you can add to your app.

Below that is a Component Tree (2) showing the views currently in this file, và how they are arranged in relation khổng lồ each other.

In the center is the Design editor (3), which shows a visual representation of what the contents of the file will look like when compiled into an apk app. You can view the visual representation, the XML code, or both.

In the upper right corner of the thiết kế editor, above Attributes (4), find the three icons that look like this:
These represent Code (code only), Split (code + design), and Design (design only) views.Try selecting the different modes. Depending on your screen form size and work style, you may prefer switching between CodeDesign, or staying in Split view. If your Component Tree disappears, hide & show the Palette.

Split view:


At the lower right of the thiết kế editor you see + and - buttons for zooming in và out. Use these buttons khổng lồ adjust the kích thước of what you see, or click the zoom-to-fit button so that both panels fit on your screen.


The Design layout on the left shows how your app appears on the device. The Blueprint layout, shown on the right, is a schematic view of the layout.

Practice using the layout thực đơn in the vị trí cao nhất left of the thiết kế toolbar lớn display the design view, the blueprint view, & both views side by side.

Depending on the kích cỡ of your screen và your preference, you may wish lớn only show the Design view or the Blueprint view, instead of both.

Use the orientation icon to lớn change the orientation of the layout. This allows you to chạy thử how your layout will fit portrait và landscape modes.
Use the device menu to lớn view the layout on different devices. (This is extremely useful for testing!)


On the right is the Attributes panel. You'll learn about that later.

Step 2: Explore và resize the Component Tree

In fragment_first.xml, look at the Component Tree. If it's not showing, switch the mode to Design instead of Split or Code.


This panel shows the view hierarchy in your layout, that is, how the views are arranged in relation to each other. 2. If necessary, resize the Component Tree so you can read at least part of the strings. 3. Click the Hide icon at the đứng top right of the Component Tree.


The Component Tree closes. 4. Bring back the Component Tree by clicking the vertical label Component Tree on the left.


Step 3: Explore view hierarchies

In the Component Tree, notice that the root of the view hierarchy is a ConstraintLayout view.

Every layout must have a root view that contains all the other views. The root view is always a view group, which is a view that contains other views. A ConstraintLayout is one example of a view group. 2. Notice that the ConstraintLayout contains a TextView, called textview_first and a Button, called button_first.


If the code isn't showing, switch khổng lồ Code or Split view using the icons in the upper right corner.In the XML code, notice that the root element is . The root element contains a element & a element.

Step 4: Change property values

In the code editor, examine the properties in the TextView element.Click on the string in the text property, & you'll notice it refers khổng lồ a string resource,"