程式語言 - Java - Android - OpenGL ES - Get Version



settings.gradle

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "hello-android"
include(":app")

build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:8.2.2"
    }
}

執行如下命令:

$ gradle wrapper
$ ./gradlew
$ mkdir -p app/src/main/java/com/example/hello
$ mkdir -p app/src/main/res

app/build.gradle

plugins {
    id "com.android.application"
}

android {
    namespace "com.example.hello"
    compileSdk 34

    defaultConfig {
        applicationId "com.example.hello"
        minSdk 21
        targetSdk 34
        versionCode 1
        versionName "1.0"
    }
}

app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="HelloApp">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

app/src/main/java/com/example/hello/MainActivity.java

package com.example.hello;

import android.app.Activity;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MainActivity extends Activity {

    private TextView infoText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GLSurfaceView glView = new GLSurfaceView(this);
        glView.setEGLContextClientVersion(2);

        infoText = new TextView(this);
        infoText.setTextColor(0xffffffff);
        infoText.setTextSize(24);

        FrameLayout layout = new FrameLayout(this);
        layout.addView(glView);
        layout.addView(infoText);
        setContentView(layout);

        glView.setRenderer(new GLSurfaceView.Renderer() {
            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                String vendor = GLES20.glGetString(GLES20.GL_VENDOR);
                String renderer = GLES20.glGetString(GLES20.GL_RENDERER);
                String version = GLES20.glGetString(GLES20.GL_VERSION);

                String info = "VENDOR: " + vendor + "\n"
                            + "RENDERER: " + renderer + "\n"
                            + "VERSION: " + version;

                runOnUiThread(() -> infoText.setText(info));

                GLES20.glClearColor(0f, 0f, 0f, 1f);
            }

            @Override
            public void onSurfaceChanged(GL10 gl, int width, int height) {
                GLES20.glViewport(0, 0, width, height);
            }

            @Override
            public void onDrawFrame(GL10 gl) {
                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            }
        });
    }
}

編譯

$ ./gradlew assembleDebug
$ file app/build/outputs/apk/debug/app-debug.apk
    app/build/outputs/apk/debug/app-debug.apk: Android package (APK), with gradle app-metadata.properties, with APK Signing Block

完成