41

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

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

谢谢

4

8 回答 8

77

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

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

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

编辑:

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

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

于 2012-08-22T12:26:34.703 回答
17

你也可以使用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");
于 2013-06-16T14:35:35.437 回答
14

您可以使用Build类:

import android.os.Build;

然后阅读:

Build.CPU_ABI

于 2014-12-26T02:36:29.377 回答
12

您可以使用 adb 命令

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

并参考[站点]:如何在 Android lollipop 中以编程方式知道应用程序的进程是 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));
于 2016-04-06T03:57:44.353 回答
10

试试这个命令:

adb shell getprop ro.product.cpu.abi

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

于 2017-01-24T09:22:54.770 回答
6

您正在寻找的价值观是

ro.product.cpu.abi

ro.product.cpu.abi2

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

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

于 2012-08-16T14:54:33.883 回答
4

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

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

于 2018-04-09T20:03:47.017 回答
2

我的代码看起来像这样

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;
}
于 2013-08-20T20:25:01.853 回答