Wonderful idea, how about writing code with ChatGPT? Let's take a look at writing a simple Android apk

foreword

The big news during this period belongs to ChatGPT. What is this? official explanation

ChatGPT is a new artificial intelligence technology-driven natural language processing tool launched by the American artificial intelligence research laboratory OpenAI. It uses the Transformer neural network architecture, which is also the GPT-3.5 architecture. This is a model for processing sequence data. Language understanding and text generation capabilities, especially it will train the model by connecting a large number of corpora, which contain dialogues in the real world, so that ChatGPT has the ability to know astronomy and geography, and can also interact according to the context of the chat , to communicate in a chat scene that is almost the same as a real human being. ChatGPT is not only a chat robot, but also can perform tasks such as writing emails, video scripts, copywriting, translation, and code.

ChatGPT is so amazing? Let's try instead of writing a program

I let it simulate being a senior programmer, writing a simple apk

The following is a sample code of a simple Android calculator application, including UI design and related logic codes. It is recommended to use Android Studio for development.

  1. Create a new Android Studio project and select the Empty Activity template.
  2. Create a new XML layout file under the layout folder to set the application interface. Here is sample code:
phpCopy code<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="40sp"
        android:text="0"/>
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:text="1"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn1"
        android:textSize="20sp"
        android:text="2"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn2"
        android:textSize="20sp"
        android:text="3"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:text="4"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn2"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn4"
        android:textSize="20sp"
        android:text="5"/>
    <Button
        android:id="@+id/btn6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn3"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn5"
        android:textSize="20sp"
        android:text="6"/>
    <Button
        android:id="@+id/btn7"
        android:android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn4" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:textSize="20sp" android:text="7"/>
mathematicaCopy code<Button
    android:id="@+id/btn8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn5"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btn7"
    android:textSize="20sp"
    android:text="8"/>
<Button
    android:id="@+id/btn9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn6"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btn8"
    android:textSize="20sp"
    android:text="9"/>
<Button
    android:id="@+id/btn0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn7"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:text="0"/>
<Button
    android:id="@+id/btnClear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btn3"
    android:textSize="20sp"
    android:text="C"/>
<Button
    android:id="@+id/btnPlus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn4"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btn9"
    android:textSize="20sp"
    android:text="+"/>
<Button
    android:id="@+id/btnMinus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn5"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btnPlus"
    android:textSize="20sp"
    android:text="-"/>
<Button
    android:id="@+id/btnMultiply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn6"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btnMinus"
    android:textSize="20sp"
    android:text="*"/>
<Button
    android:id="@+id/btnDivide"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn7"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/btn
    </LinearLayout>

The main logic of the calculator is implemented in the MainActivity.java file. The following is the code implementation:

javaCopy codepublic class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
    private Button btnPlus, btnMinus, btnMultiply, btnDivide, btnClear;
    private double num1 = 0, num2 = 0;
    private String operator = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btnPlus = findViewById(R.id.btnPlus);
        btnMinus = findViewById(R.id.btnMinus);
        btnMultiply = findViewById(R.id.btnMultiply);
        btnDivide = findViewById(R.id.btnDivide);
        btnClear = findViewById(R.id.btnClear);
        // 设置数字按钮的点击事件
        View.OnClickListener numberClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button button = (Button) v;
                textView.append(button.getText());
            }
        };
        btn0.setOnClickListener(numberClickListener);
        btn1.setOnClickListener(numberClickListener);
        btn2.setOnClickListener(numberClickListener);
        btn3.setOnClickListener(numberClickListener);
        btn4.setOnClickListener(numberClickListener);
        btn5.setOnClickListener(numberClickListener);
        btn6.setOnClickListener(numberClickListener);
        btn7.setOnClickListener(numberClickListener);
        btn8.setOnClickListener(numberClickListener);
        btn9.setOnClickListener(numberClickListener);
        // 设置运算符按钮的点击事件
        View.OnClickListener operatorClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button button = (Button) v;
                if (!TextUtils.isEmpty(textView.getText().toString())) {
                    num1 = Double.parseDouble(textView.getText().toString());
                }
                operator = button.getText().toString();
                textView.setText("");
            }
        };
        btnPlus.setOnClickListener(operatorClickListener);
        btnMinus.setOnClickListener(operatorClickListener);
        btnMultiply.setOnClickListener(operatorClickListener);
        btnDivide.setOnClickListener(operatorClickListener);
        // 设置清除按钮的点击事件
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1 = 0;
                num2 = 0;
                operator = "";
                textView.setText("");
            }
        });
        // 设置等于按钮的点击事件
        findViewById(R.id.btnEquals).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(textView.getText().toString())) {
                    num2 = Double.parseDouble(textView.getText().toString());
                }
                double result = 0;
                switch (operator) {
                    case "+":
                        result = num1 + num2;
                        break;
                    case "-":
                        result = num1 - num2;
                        break;
                    case "*":
                        result = num1 * num2;
                        break;
                    case "/":
                        result = num1 / num2;
                        break;
                }
                textView.setText(String.valueOf(result));
                num1 = result;
                num2 = 0;
                operator = "";
            }
        });
    }
}

The final step is to package the APK file. To package an APK file, we need to do the following steps:

  1. generate signing key
  2. Configure build.gradle file
  3. Execute build tasks and generate APK files

The first step is to generate a signing key. The signature key is to ensure the integrity and security of the APK file, and it is used to verify whether the APK file has been tampered with. We can generate a signing key with the following command:

keytool -genkey -v -keystore myapp.keystore -alias myapp -keyalg RSA -keysize 2048 -validity 10000

This command will generate a signing key file named myapp.keystore. When generating a signing key, we need to set information such as alias, password, and validity period.

The second step is to configure the build.gradle file. We need to configure signing key information and other build options in build.gradle file. Here is the content of a sample build.gradle file:

javaCopy codeandroid {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    defaultConfig {
        applicationId "com.example.calculator"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        release {
            storeFile file("myapp.keystore")
            storePassword "mystorepass"
            keyAlias "myapp"
            keyPassword "mykeypass"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
}
dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
}

In this build.gradle file, we define some basic properties of the application, such as application ID, minimum SDK version and target SDK version, etc. We also define the signing key information and build type (eg debug or release) etc.

The third step is to execute the build task and generate the APK file. We can execute the build task with the following command:

./gradlew assembleRelease

This command will execute a Gradle task named assembleRelease, which will generate an APK file named app-release.apk. This APK file is the application file we want to publish.

The above is the code implementation and APK packaging process of the Android calculator. If you follow the above steps to implement a calculator application and successfully package the APK file, then congratulations, you have mastered the basic Android development skills!

APK file signature

Finally, we also need to sign the generated APK file to ensure that the APK file can be recognized and trusted when running on the user's device.

To sign the APK file, we can use the following command:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore myapp.keystore app-release-unsigned.apk myapp

This command will use the myapp.keystore signing key file to sign the app-release-unsigned.apk file, and the signed APK file will be named app-release.apk. During the signing process, we need to enter the passphrase for the signing key.

Now, we have successfully generated a signed APK file, which can be uploaded to Google Play or other app stores for distribution. However, we need to do some testing and optimization before releasing it to ensure the quality and performance of the app.

summary

In general, writing an Android application and packaging it into an APK file requires multiple steps, including creating a project, writing code, laying out the interface, debugging, building, and packaging. These steps may involve multiple tools and technologies, such as Android Studio, Gradle, XML, Java, layout files, debugging tools, etc. Learning and mastering these skills takes time and effort, but once you have mastered these skills, you can develop high-quality, outstanding Android applications that bring better experience and value to users.

Since AI can write programmers, are we Android programmers going to withdraw from the stage of history?

answer: no

Look down and you will understand. It is impossible for ai to replace the position of a programmer, but only to the extent of assistance. Programmers here still need to speed up their own technology advancement. Here is a core note for Android programmers "Android Core Technology Manual". Click for reference to get the technology you want to receive categories.

Is it possible to write Android program code with AI?

The current AI technology can assist in writing Android program code to a certain extent, but it is not feasible to write the entire application program entirely by AI. Although there are already some AI code generation tools, such as OpenAI's Codex and GitHub's Copilot, they can assist developers in writing code by learning a large number of code bases and algorithm models, but their capabilities are far from enough to replace human programmers .

This is because there are still many limitations and flaws in the code generation capability of AI. For example, AI can only generate code according to existing patterns and rules, and cannot perform creative thinking and judgment, cannot understand complex business logic and user needs, and cannot Ensure the quality and maintainability of the generated code, etc. In addition, since AI code generation processes are based on existing code bases and algorithm models, they are also vulnerable to issues such as data bias and privacy leaks.

Therefore, although AI technology can assist in writing Android program codes in some aspects, it cannot completely replace human programmers at the current level of technology. Programmers still need to have certain programming skills and experience to be able to develop high-quality Android applications.

Guess you like

Origin blog.csdn.net/m0_71524094/article/details/130062877