获取处理器架构的API调用

Ljd*_*son 34 android cpu-architecture

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

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

谢谢

3c7*_*c71 66

实际上,您可以在不需要反射的情况下获得架构:

String arch = System.getProperty("os.arch");
Run Code Online (Sandbox Code Playgroud)

从我的测试中它返回armv71并且i686.

编辑:

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

在64位ARM/Intel上,它分别返回'arch64'或'x86_64'.


she*_*hem 16

你也可以使用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");
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用CPU_ABI*常量而使用SUPPORTED_*ABIS,但这些常量的工作方式几乎相同,仍然是Build对象的一部分, (2认同)

tom*_*bee 10

您可以使用adb命令

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

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

如果您正在寻找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));
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以使用Build类:

import android.os.Build;

然后阅读:

Build.CPU_ABI


Mah*_*raa 7

试试这个命令:

adb shell getprop ro.product.cpu.abi
Run Code Online (Sandbox Code Playgroud)

它告诉cpu是ARM还是Intel,64或86_64

  • adb 不是为从设备调用而设计的。它基本上是从 PC 到设备/模拟器的调试工具。您需要通过应用程序访问控制台。我认为有限制,您可能需要 root .. 如果您需要从应用程序获取此信息,我建议您使用其他方法 (2认同)

nan*_*esh 6

您正在寻找的值是

ro.product.cpu.abi

ro.product.cpu.abi2

这些可以使用内部api SystemProperties.get获得所以你必须在SystemProperties上使用Reflection.

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


Rat*_*ata 5

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));
}
Run Code Online (Sandbox Code Playgroud)

来自: https: //github.com/termux/termux-app/blob/master/app/src/main/java/com/termux/app/TermuxInstaller.java