用 C 或 C++ 为 Android 编写应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1002164/
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 02:35:13  来源:igfitidea点击:

Write applications in C or C++ for Android?

c++candroid

提问by Robert Gould

I'm trying to develop/port a game to Android, but it's in C, and Android supports Java, but I'm sure there must be a way to get a C app on there, anyone knows of a way to accomplish this?

我正在尝试将游戏开发/移植到 Android,但它是用 C 语言编写的,而 Android 支持 Java,但我确信必须有一种方法可以在那里安装 C 应用程序,有人知道实现这一目标的方法吗?

采纳答案by Andy Johnson

For anyone coming to this via Google, note that starting from SDK 1.6 Android now has an official native SDK.

对于通过 Google 访问此内容的任何人,请注意从 SDK 1.6 开始,Android 现在拥有官方的原生 SDK。

You can download the Android NDK (Native Development Kit) from here: https://developer.android.com/ndk/downloads/index.html

您可以从这里下载 Android NDK(原生开发工具包):https: //developer.android.com/ndk/downloads/index.html

Also there is an blog post about the NDK:
http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html

还有一篇关于 NDK 的博客文章:http:
//android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html

回答by MannyNS

The Android NDKis a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

Android的NDK是一个工具集,可以让你实现在本地代码你的应用程序的一部分,使用的语言,如C和C ++。对于某些类型的应用程序,这可以帮助您重用用这些语言编写的代码库。

For more info on how to get started with native development, follow this link.

有关如何开始本机开发的更多信息,请访问此链接

Sample applications can be found here.

可以在此处找到示例应用程序。

回答by psihodelia

Normally, you have to:

通常,您必须:

  1. Install Google Android NDK. It contains libs, headers, makfile examples and gcc toolchain
  2. Build an executable from your C code for ARM, optimize and link it with provided libs if required
  3. Connect to a phone using provided adb interface and test your executable
  1. 安装谷歌安卓 NDK。它包含库、头文件、makfile 示例和 gcc 工具链
  2. 从您的 C 代码为 ARM 构建一个可执行文件,优化并将其与提供的库链接(如果需要)
  3. 使用提供的 adb 接口连接到手机并测试您的可执行文件

If you are looking to sell an app:

如果您想销售应用程序:

  1. Build a library from your C code
  2. Create simple Java code which will use this library
  3. Embed this library into application package file
  4. Test your app
  5. Sell it or distribute it for free
  1. 从您的 C 代码构建一个库
  2. 创建将使用此库的简单 Java 代码
  3. 将此库嵌入到应用程序包文件中
  4. 测试您的应用
  5. 出售或免费分发

回答by Marc

Google has released a Native Development Kit (NDK) (according to http://www.youtube.com/watch?v=Z5whfaLH1-Eat 00:07:30).

Google 已经发布了一个原生开发工具包 (NDK)(根据http://www.youtube.com/watch?v=Z5whfaLH1-E在 00:07:30)。

Hopefully the information will be updated on the google groups page (http://groups.google.com/group/android-ndk), as it says it hasn't been released yet.

希望信息会在 google 群组页面 ( http://groups.google.com/group/android-ndk)上更新,因为它说它尚未发布。

I'm not sure where to get a simple download for it, but I've heard that you can get a copy of the NDK from Google's Git repository under the donut branch.

我不确定从哪里获得它的简单下载,但我听说您可以从 Google 的 Git 存储库的甜甜圈分支下获取 NDK 的副本。

回答by Andy Johnson

The official position seemsto be that this isn't something you'd ever "want to do". See this threadon the Android Developers list. Google envisage android running on a variety of different devices (CPUs, displays, etc). The best way to enable development is therefore to use (portable) managed code that targets the Dalvik VM. For this reason, the Android SDK doesn't support C/C++.

官方立场似乎是,这不是你“想做”的事情。请参阅Android 开发人员列表上的此主题。Google 设想 android 可以在各种不同的设备(CPU、显示器等)上运行。因此,启用开发的最佳方法是使用针对 Dalvik VM 的(可移植的)托管代码。因此,Android SDK 不支持 C/C++。

BUT, take a look at this page:

但是,看看这个页面

Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework.

Android 包含一组由 Android 系统的各个组件使用的 C/C++ 库。这些功能通过 Android 应用程序框架向开发人员公开。

The managed application framework appears to be layered on-top of these libraries. The page goes on to list the C/C++ libs: standard C library, media, 3D, SQL lite, and others.

托管应用程序框架似乎位于这些库之上。该页面继续列出 C/C++ 库:标准 C 库、媒体、3D、SQL lite 等。

So all you need is a compiler chain that will compile C/C++ to the appropriate CPU (ARM, in the case of the G1). Some brief instructions on how to do this are here.

所以你所需要的只是一个编译器链,它将 C/C++ 编译到适当的 CPU(ARM,在 G1 的情况下)。此处提供了有关如何执行此操作的一些简要说明。

What I don't know is where to find descriptions of the APIs that these libraries provide. I'd guess there may be header files buried in the SDK somewhere, but documentation may be sketchy/missing. But I think it can be done!

我不知道在哪里可以找到这些库提供的 API 的描述。我猜想 SDK 中的某个地方可能有头文件,但文档可能很粗略/丢失。但是我觉得可以做到!

Hope thats useful. For the record, I haven't written any native android apps - just a few simple managed ones.

希望那有用。郑重声明,我没有编写任何原生 android 应用程序 - 只是一些简单的托管应用程序。

Andy

安迪

回答by rjmunro

You can use nestedvmto translate C (or other GCC languages) into Java bytecode, and use that as the basis of your port. For example, see the Android port of Simon Tathams portable puzzle collection.

您可以使用nestedvm将C(或其他GCC 语言)转换为Java 字节码,并将其用作移植的基础。例如,参见Simon Tathams 便携式拼图合集Android 移植

I expect this method is made obsolete by the NDK, but it might not be in if some networks or something don't allow people to upgrade their phones.

我希望这种方法已被 NDK 淘汰,但如果某些网络或某些东西不允许人们升级手机,则可能不会采用这种方法。

回答by Jeegar Patel

Google has already launched Google I/O 2011: Bringing C and C++ Games to Androidsession which is available at http://www.youtube.com/watch?v=5yorhsSPFG4

Google 已经推出了Google I/O 2011:将 C 和 C++ 游戏引入 Android会议,可从http://www.youtube.com/watch?v=5yorhsSPFG4 获得

which is good to understand the use of NDK for writing application in c and c++ for android.

这很好理解使用 NDK 在 c 和 c++ 中为 android 编写应用程序。

If you just want to cross compile any console based native game and run them on android then this Articlehas shown 3 methods for the same.

如果您只想交叉编译任何基于控制台的本机游戏并在 android 上运行它们,那么本文已经展示了 3 种方法。

1: Static compilation using standalone toolchain

1:使用独立工具链进行静态编译

2: Cross compilation using Android NDK's toolchain

2:使用Android NDK的工具链交叉编译

3: Cross compilation using AOSP source code

3:使用AOSP源代码交叉编译

回答by joepal

Maybe you are looking for this?

也许你正在寻找这个?

http://www.mosync.com/

http://www.mosync.com/

It is a middle layer for developing for several mobile platforms using c++.

它是使用 C++ 为多个移动平台开发的中间层。

回答by backslash17

Looking at this it seems it is possible:

看这似乎是可能的:

"the fact is only Java language is supported doesn't mean that you cannot develop applications in other languages. This have been proved by many developers, hackers and experts in application development for mobile. The guys at Elements Interactive B.V., the company behind Edgelib library, succeeded to run native C++ applications on the Android platform, even that at this time there is still many issues on display and sound … etc. This include the S-Tris2 game and a 3D animation demo of Edgelib."

“事实上,仅支持 Java 语言并不意味着您不能用其他语言开发应用程序。许多开发人员、黑客和移动应用程序开发专家已经证明了这一点。Edgelib 背后的公司 Elements Interactive BV 的人库,成功在Android平台上运行原生C++应用程序,即使此时在显示和声音等方面仍然存在很多问题……包括S-Tris2游戏和Edgelib的3D动画演示。”

回答by zkunov

Since 2009 there is a development on this matter.
Necessitas- Qt(C++ framework) for Android
Getting started video.

自 2009 年以来,这件事有了进展。
Necessitas- 适用于 Android 的 Qt(C++ 框架)
入门视频