调用 API 以获取处理器体系结构



作为我应用程序的一部分,我正在使用 NDK,想知道是否值得将 x86 和 mips 二进制文件与标准 ARM 二进制文件捆绑在一起。

我认为最好的方法是跟踪我的用户实际拥有的东西,是否有一个 API 调用来获取处理器架构,以便我可以将其传递回我的 Google 分析实例?

谢谢

实际上,您可以完全不需要反射即可获得架构:

String arch = System.getProperty("os.arch");

从我的测试中,它返回了armv71i686

编辑:

在MIPS架构上,它返回"mips"或"mips64"

在 64 位 ARM/Intel 上,它分别返回"aarch64"或"x86_64"。

你也可以

使用 android SDK,看看Build类:

    /** The name of the instruction set (CPU type + ABI convention) of native code. */
    public static final String CPU_ABI = getString("ro.product.cpu.abi");
    /** The name of the second instruction set (CPU type + ABI convention) of native code. */
    public static final String CPU_ABI2 = getString("ro.product.cpu.abi2");

您可以使用 Build 类:

import android.os.Build;

然后阅读:

Build.CPU_ABI

您可以使用 adb 命令

adb shell getprop ro.product.cpu.abiadb shell getprop ro.product.cpu.abi2

并参考[网站]:如何在Android棒棒糖中以编程方式知道应用程序的进程是32位还是64位?

如果您正在寻找棒棒糖 API

import android.os.Build;
Log.i(TAG, "CPU_ABI : " + Build.CPU_ABI);
Log.i(TAG, "CPU_ABI2 : " + Build.CPU_ABI2);
Log.i(TAG, "OS.ARCH : " + System.getProperty("os.arch"));
Log.i(TAG, "SUPPORTED_ABIS : " + Arrays.toString(Build.SUPPORTED_ABIS));
Log.i(TAG, "SUPPORTED_32_BIT_ABIS : " + Arrays.toString(Build.SUPPORTED_32_BIT_ABIS));
Log.i(TAG, "SUPPORTED_64_BIT_ABIS : " + Arrays.toString(Build.SUPPORTED_64_BIT_ABIS));

试试这个命令:

adb shell getprop ro.product.cpu.abi

它告诉CPU是ARM还是英特尔(分别为64或86_64)

您要查找的值是

ro.product.cpu.abi

ro.product.cpu.abi2

这些可以使用内部 api SystemProperties.get 获得,因此您必须使用 Reflection on SystemProperties。

如果你不太热衷于反射,你可以使用函数getSystemProperty。在这里查看

termux-app使用不同的方法并有一个解释:

private static String determineTermuxArchName() {
    // Note that we cannot use System.getProperty("os.arch") since that may give e.g. "aarch64"
    // while a 64-bit runtime may not be installed (like on the Samsung Galaxy S5 Neo).
    // Instead we search through the supported abi:s on the device, see:
    // http://developer.android.com/ndk/guides/abis.html
    // Note that we search for abi:s in preferred order (the ordering of the
    // Build.SUPPORTED_ABIS list) to avoid e.g. installing arm on an x86 system where arm
    // emulation is available.
    for (String androidArch : Build.SUPPORTED_ABIS) {
        switch (androidArch) {
            case "arm64-v8a": return "aarch64";
            case "armeabi-v7a": return "arm";
            case "x86_64": return "x86_64";
            case "x86": return "i686";
        }
    }
    throw new RuntimeException("Unable to determine arch from Build.SUPPORTED_ABIS =  " +
        Arrays.toString(Build.SUPPORTED_ABIS));
}

寄件人: https://github.com/termux/termux-app/blob/master/app/src/main/java/com/termux/app/TermuxInstaller.java

我的代码看起来像这样

private String cpuinfo()
{
String arch = System.getProperty("os.arch");    
String arc = arch.substring(0, 3).toUpperCase();
String rarc="";
if (arc.equals("ARM")) {
    rarc= "This is ARM";
    }else if (arc.equals("MIP")){
        rarc= "This is MIPS";
    }else if (arc.equals("X86")){
        rarc= "This is X86";
    }
    return rarc;
}

请参阅此链接: https://developer.android.com/reference/android/os/Build#SUPPORTED_ABIS

Build.SUPPORTED_32_BIT_ABIS;

最新更新