Android 获取处理器架构的 API 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11989629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:24:04  来源:igfitidea点击:

API call to get processor architecture

androidcpu-architecture

提问by Ljdawson

As part of my app I'm using the NDK and was wondering if it's worth bundling x86 and mips binaries alongside the standard ARM binaries.

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

I figured the best way would be to track what my users actually have, is there an API call to grab the processor architecture so I can pass this back to my Google analytics instance?

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

Thanks

谢谢

回答by 3c71

Actually, you can get the architecture without the need for reflexion at all:

实际上,您可以完全无需反思即可获得架构:

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

From my tests it returned armv71and i686.

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

EDIT:

编辑:

On MIPS architecture, it either returns 'mips' or 'mips64'

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

On 64 bit ARM/Intel, it returns 'aarch64' or 'x86_64' respectively.

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

回答by shem

You can also use android SDK, take a look on the Buildclass:

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

回答by tommybee

You can use adb command

您可以使用 adb 命令

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

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

and refer the [site]: How to know a process of an app is 32-bit or 64-bit programmatically in Android lollipop?

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

If you're looking for the Lollipop API

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

回答by and0421

You can use the Buildclass:

您可以使用Build类:

import android.os.Build;

import android.os.Build;

and then read:

然后阅读:

Build.CPU_ABI

Build.CPU_ABI

回答by Maher Abuthraa

Try this command:

试试这个命令:

adb shell getprop ro.product.cpu.abi

It tells whether cpu is ARM or Intel , 64 or 86_64

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

回答by nandeesh

The values you are looking for are

您正在寻找的值是

ro.product.cpu.abi

ro.product.cpu.abi

and

ro.product.cpu.abi2

ro.product.cpu.abi2

These can be got using internal api SystemProperties.get So you will have to use Reflection on SystemProperties.

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

You could use the function getSystemProperty if you are not too keen on reflection. Check it here

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

回答by Ratata Tata

termux-appuses a different approach and has an explanation:

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

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

回答by Robs Ned

My Code looks like this

我的代码看起来像这样

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