【问题标题】:API call to get processor architecture获取处理器架构的 API 调用
【发布时间】:2012-08-16 14:39:54
【问题描述】:

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

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

谢谢

【问题讨论】:

标签: android cpu-architecture


【解决方案1】:

其实完全不需要反思就可以得到架构:

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

根据我的测试,它返回了 armv71i686

编辑:

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

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

【讨论】:

  • 基于 MIPS 的处理器返回什么?
  • 我相信它返回“mips”,不记得我是否真的在模拟器中测试过。但这就是我使用的。
  • 只是为了把事情做好——我得到的是'armv7l'而不是你说的'arm71'。有什么你能想到的理由吗?
  • 这就是为什么我只检查“arm”,因为它因设备而异
  • 这里有一个编译时常数不是更好吗,这样任何依赖它的条件都可以在编译时优化掉?不过,如果这是 JIT 编译的语言,这没什么大不了的。
【解决方案2】:

你也可以使用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");

【讨论】:

  • 比使用反射更好!不错!
  • CPU_ABI* 常量已被弃用,取而代之的是 SUPPORTED_*ABIS,但这些常量的工作方式几乎相同,并且仍然是 Build 对象的一部分,
【解决方案3】:

您可以使用Build 类:

import android.os.Build;

然后阅读:

Build.CPU_ABI

【讨论】:

  • 这是一个完整的答案,但 the same answer 已经在一年前提出。请花时间在发布之前查看以前的答案:)
  • @Leigh 这个答案与 shem 提供的答案不同(更有效,更面向未来)。 IMO 这应该是公认的答案。
【解决方案4】:

你可以使用adb命令

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

并参考[网站]:How to know a process of an app is 32-bit or 64-bit programmatically in Android lollipop?

如果您正在寻找 Lollipop 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));

【讨论】:

  • 使用如下检查来决定您将使用哪个 API:if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
【解决方案5】:

试试这个命令:

adb shell getprop ro.product.cpu.abi

它告诉cpu是ARM还是Intel(分别是64或86_64)

【讨论】:

  • 开发者如何在用户智能手机上运行adb 命令?
  • adb 并非设计为从设备调用.. 它基本上是从 PC 到设备/模拟器的调试工具。您需要通过应用程序访问控制台。我认为有限制,您可能需要 root .. 如果您需要从应用程序获取此信息,我建议您使用其他方法
【解决方案6】:

您正在寻找的值是

ro.product.cpu.abi

ro.product.cpu.abi2

这些可以使用内部 api SystemProperties.get 获得,因此您必须在 SystemProperties 上使用反射。

如果您不太热衷于反思,可以使用函数 getSystemProperty。检查它here

【讨论】:

  • 非常好,正是我想要的。
【解决方案7】:

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

【讨论】:

    【解决方案8】:

    我的代码如下所示

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

    【讨论】:

    • 你能告诉我 armeabi-v7a 和 armeabi-v7a-neon 是相同还是不同的 cpu arch??
    • NEON 是 CPU 可以支持的特定功能。如果只想检查CPU是否为ARM,请使用System.getProperty("os.arch").toLowerCase().contains("arm")
    猜你喜欢
    • 2011-02-07
    • 2021-05-05
    • 2010-12-01
    • 2014-06-21
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2020-07-17
    相关资源
    最近更新 更多