素晴らしいアイデアです。ChatGPT でコードを書いてみませんか? 簡単な Android apk の作成を見てみましょう

序文

この時期のビッグニュースといえば、ChatGPTの話ですが、これは何ですか?公式説明

ChatGPT は、米国の人工知能研究所である OpenAI が発表した新しい人工知能技術主導の自然言語処理ツールで、GPT-3.5 アーキテクチャでもある Transformer ニューラル ネットワーク アーキテクチャを使用しており、シーケンス データを処理するためのモデルです。言語理解とテキスト生成機能、特にChatGPTが天文学と地理を知る能力を持ち、また文脈に応じて相互作用できるように、現実世界の対話を含む多数のコーパスを接続してモデルをトレーニングします。チャット 、実際の人間とほぼ同じチャット シーンで通信します。ChatGPT は単なるチャット ロボットではなく、メールの作成、ビデオ スクリプト、コピーライティング、翻訳、コード作成などのタスクを実行できます。

ChatGPTってそんなにすごいの?プログラムを書く代わりにやってみよう

シニア プログラマーとして、簡単な apk を作成するようにシミュレートします。

以下は、UI デザインと関連するロジック コードを含む、単純な Android 電卓アプリケーションのサンプル コードです。開発には Android Studio を使用することをお勧めします。

  1. 新しい Android Studio プロジェクトを作成し、空のアクティビティ テンプレートを選択します。
  2. アプリケーション インターフェイスを設定するには、layout フォルダーの下に新しい XML レイアウト ファイルを作成します。サンプルコードは次のとおりです。
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>

計算機の主なロジックは MainActivity.java ファイルに実装されています。コードの実装は次のとおりです。

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 = "";
            }
        });
    }
}

最後のステップは、APK ファイルをパッケージ化することです。APK ファイルをパッケージ化するには、次の手順を実行する必要があります。

  1. 署名鍵を生成する
  2. build.gradle ファイルを構成する
  3. ビルド タスクを実行して APK ファイルを生成する

最初のステップは、署名鍵を生成することです。署名キーは、APK ファイルの整合性とセキュリティを確保するためのものであり、APK ファイルが改ざんされていないかどうかを検証するために使用されます。次のコマンドで署名キーを生成できます。

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

このコマンドは、myapp.keystore という署名鍵ファイルを生成します。署名鍵を生成する際には、エイリアス、パスワード、有効期限などの情報を設定する必要があります。

2 番目のステップは、build.gradle ファイルを構成することです。build.gradle ファイルで署名キー情報とその他のビルド オプションを構成する必要があります。サンプルの build.gradle ファイルの内容は次のとおりです。

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'
}

この build.gradle ファイルでは、アプリケーション ID、最小 SDK バージョン、ターゲット SDK バージョンなど、アプリケーションのいくつかの基本的なプロパティを定義します。また、署名キー情報とビルド タイプ (例: デバッグまたはリリース) なども定義します。

3 番目のステップは、ビルド タスクを実行して APK ファイルを生成することです。次のコマンドでビルド タスクを実行できます。

./gradlew assembleRelease

このコマンドは、app-release.apk という名前の APK ファイルを生成する、assemblyRelease という名前の Gradle タスクを実行します。この APK ファイルは、公開したいアプリケーション ファイルです。

上記は、Android 電卓のコード実装と APK パッケージ化プロセスです。上記の手順に従って電卓アプリケーションを実装し、APK ファイルを正常にパッケージ化したら、おめでとうございます。基本的な Android 開発スキルを習得したことになります。

APK ファイルの署名

最後に、生成された APK ファイルに署名して、APK ファイルがユーザーのデバイスで実行されているときに認識および信頼できるようにする必要もあります。

APK ファイルに署名するには、次のコマンドを使用できます。

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

このコマンドは、myapp.keystore 署名鍵ファイルを使用して app-release-unsigned.apk ファイルに署名し、署名された APK ファイルは app-release.apk という名前になります。署名プロセス中に、署名キーのパスフレーズを入力する必要があります。

これで、配布用に Google Play や​​他のアプリ ストアにアップロードできる署名付き APK ファイルが正常に生成されました。ただし、アプリの品質とパフォーマンスを確保するために、リリース前にいくつかのテストと最適化を行う必要があります。

まとめ

一般に、Android アプリケーションを作成して APK ファイルにパッケージ化するには、プロジェクトの作成、コードの記述、インターフェースのレイアウト、デバッグ、ビルド、パッケージ化など、複数の手順が必要です。これらの手順には、Android Studio、Gradle、XML、Java、レイアウト ファイル、デバッグ ツールなど、複数のツールやテクノロジーが含まれる場合があります。これらのスキルを習得して習得するには時間と労力がかかりますが、これらのスキルを習得すると、ユーザーに優れたエクスペリエンスと価値をもたらす、高品質で優れた Android アプリケーションを開発できます。

AI はプログラマーを書くことができるので、私たち Android プログラマーは歴史の段階から撤退するのでしょうか?

答え: いいえ

見下ろすとわかります。AIがプログラマーの地位を代替することは不可能ですが、補助の程度にすぎません.ここでプログラマーは、自分の技術の進歩をスピードアップする必要があります.ここでは、Androidプログラマーのためのコアノート「Android Core Technology Manual」.受けたい技術カテゴリを取得するための参照。

AIでAndroidのプログラムコードを書くことは可能ですか?

現在の AI 技術は、Android プログラム コードの作成をある程度支援できますが、アプリケーション プログラム全体を AI だけで作成することは現実的ではありません。OpenAI の Codex や GitHub の Copilot などの AI コード生成ツールはすでにいくつか存在しますが、それらは多数のコード ベースとアルゴリズム モデルを学習することで開発者がコードを書くのを支援できますが、それらの機能は人間のプログラマーに取って代わるにはほど遠いものです。

これは、AI のコード生成機能には、既存のパターンやルールに従ってコードを生成することしかできない、創造的な思考や判断を行うことができない、複雑なビジネス ロジックやユーザーのニーズを理解できない、生成されたコードの品質や保守性などを保証できません。さらに、AI コード生成プロセスは既存のコード ベースとアルゴリズム モデルに基づいているため、データ バイアスやプライバシー漏洩などの問題にも脆弱です。

したがって、AI テクノロジーは Android プログラム コードの作成をある程度支援できますが、現在のテクノロジー レベルでは人間のプログラマーを完全に置き換えることはできません。プログラマーは、高品質の Android アプリケーションを開発するために、特定のプログラミング スキルと経験を持っている必要があります。

おすすめ

転載: blog.csdn.net/m0_71524094/article/details/130062877