Learning "Android Studio Development Practice" (1) - Hello World


background

During this period of time, I was learning Android Studio to develop App 1. The experience and experience in the practice process are recorded here.

Android Studio installation

Android Studio is a free Android development environment 2 based on IntelliJ IDEA . Its latest version (Android Studio Dolphin | 2021.3.1) can be downloaded from Google's official website at: Download Android Studio and SDK tools | Android Developers .

Download a file in a tar package android-studio-2021.3.1.17-linux.tar.gz, unzip it and enter the bin directory, and then run

tar -zxf android-studio-2021.3.1.17-linux.tar.gz
cd android-studio/bin/
./studio.sh

Then download each component according to the prompts in the pop-up dialog box. If a component fails to download, you can exit and run it several times ./studio.sh.

You can also enter commands sudo ./studio.sh, so that you can avoid reporting errors due to directory read and write permissions when generating apk later. It's best to stick to one of the two commands, since the resulting AndroidStudioProjects folder resides in a different location.

Getting Android Studio up and running

Follow the previous step, open Android Studio, and the welcome interface will appear when you run it for the first time.
Learning "Android Studio Development Practice" (1) - Hello World - Android Studio welcome interface
Next, click New Project to create a new project, a dialog box appears, select the Phone and Tablet panel Empty Activity, write the project name HelloWorld, select the language Java, select Minimum SDK API 16: Android 4.1 (Jelly Bean)(about 100% of the mobile phones can run), and then click Finish to reach its project interface .

Run the small application Hello World

On the left side of the Android Stuido project interface is the directory structure of the current project, including three folders manifests, java, and res. At the beginning, two files are mainly used, one is the code file, located under the folder; the other is the MainActivity.javalayout java/com.example.myapplicationfile activity_main.xml, located res/layoutunder the folder.

MainActivity.javacode show as below:

package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xmlcode show as below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        <!-- 测试中文输入 -->
        android:text="你好世界!"	
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The app generated in this way is displayed vertically on the mobile phone by default. If you want to display it horizontally, you need to edit manifests/AndroidManifest.xmlthe file and <activity>add an attribute android:screenOrientation="landscape" 3 to the tag .

manifests/AndroidManifest.xmlThe whole code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HelloWorld"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>
</manifest>

Then you need to prepare an Android mobile phone. The mobile phone emulator that comes with Android Studio is not recommended, because usually there will be no response for half an hour, and it will eventually be simulated on a real phone, so it is best to use Android Studio to generate an apk file and then transfer it. Install and run on your phone.

How to generate an APK file

Select Build - Generate Signed Bundle / APK from the menu bar. At this time, the dialog box asks whether to choose Android APP Bundle or APK, and choose APK. The next step requires an APK signature file, if not, you can create a new one, record the password (including alias and password) and the path where the file is stored, and fill it in the dialog box.

Click Next on the APK signature window page, select debug for Build Variants debugging, select release for release, and click Finish. View AndroidStudioProjectsthe appfolders, and you'll find one more debugfolder in it, which was generated app-debug.apk.

Then transfer the apk file to the phone, install and run it. The running results are as follows:
Learning "Android Studio Development Practice" (1) - Hello World - screenshot of running results
In this way, the Hello World App is successfully produced.


  1. Ouyang Shen. Android Studio Development Practice. Tsinghua University Press. 2017 ↩︎

  2. Chen Fu, Android Studio Application. Computer Knowledge and Technology, 2014. ↩︎

  3. How to set the landscape screen in android studio↩︎

Guess you like

Origin blog.csdn.net/quanet_033/article/details/128191128