ndk编写android终端hello world

 

在windows下面编译使用ndk,不是一个容易的事情,但是在ndk-r8后,似乎方便了很多。具体方法如下:
1. 下载安装android ndk,解压到一个目录,最好是比较简单的英文路径,防止各种坑。然后将ndk加入到路径中。比如
set path=%paht%;H:\gp\Android\android-ndk-r9
set path=%path%;H:\gp\Android\android-studio\sdk\platform-tools
这两句话就是把ndk-build和adb命令加入到cmd的命令环境中,方便后面的使用。如果常用,建议加入到环境变量path中
2. 编写相关代码
注意,需要在项目目录下面建立一个jni目录,在jni目录下建立文件。
target.c

#include <stdio.h> int flag = 1; int count = 0; int main() { char* str = "abcdef"; while(flag) { printf("Target is running:%d\n", count); count++; sleep(3); } return 0; }

Android.mk

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := target LOCAL_SRC_FILES := target.c include $(BUILD_EXECUTABLE)

代码很简单,就是一个循环打印语句。值得注意的是Android.mk,根据需要修改LOCAL_XXX的名字就可以了,如果有多个文件,添加到SRC_FILES后面就可以了
在jni目录运行命令
ndk-build

"Compile thumb : target <= target.c Executable : target Install : target => libs/armeabi/target

会在项目目录的libs/armeabi目录下面生产目标代码
下面的操作方法是将target上传到调试机器上,我一般使用机器,因为模拟器太慢了,折磨人。你的机器需要设置为usb development,并且需要有root权限,否则,只能在模拟器中测试。
adb remount
adb push target /data/local/tmp
adb shell
su
cd /data/local/tmp
chmod 751 target
./target

root@android:/data/local/tmp # ./target ./target Target is running:0 Target is running:1 Target is running:2 Target is running:3 Target is running:4 Target is running:5

需要注意的地方是android下面busybox的chmod u+x target不能工作,只能用751这种数字方式来设置程序的可运行。

希望你好运,能够运行一个helloword的ndk程序


转载:http://blog.163.com/tod_zhang/blog/static/10255221420139198629861/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# NDK Camera [![Build Status](https://travis-ci.org/luncliff/NdkCamera.svg?branch=master)](https://travis-ci.org/luncliff/NdkCamera) > If there is an issue with this library, please mail to luncliff@gmail.com Simplified [Android Camera 2 API](https://www.youtube.com/watch?v=Bi4QjMfSOE0). Available for both Java/JNI. - API level: 24+ - NDK ### Reference - Personal Template Project: https://github.com/luncliff/Muffin - [API Reference](https://developer.android.com/ndk/reference/group/camera) - [Android Camera Overview](https://source.android.com/devices/camera) - Camera HAL3: https://source.android.com/devices/camera/camera3 - HAL Subsystem: https://source.android.com/devices/camera/camera3_requests_hal - Multi-Camera Support: https://source.android.com/devices/camera/multi-camera - Version Support: https://source.android.com/devices/camera/versioning - Android Media - https://source.android.com/devices/media/framework-hardening ## How to ### Build For **Windows** environment, latest [Android Studio](https://developer.android.com/studio/) is recommended. For **Linux/MacOS**, [Gradle 4.10.2](https://gradle.org/) will be enough. ```console $ git clone https://github.com/luncliff/NdkCamera $ cd ./NdkCamera $ gradle assemble # Build: libndk_camera.so & NdkCamera.aar ``` ### Test Connect your device and run the test with Gradle. Please reference the [test codes](./android/test/ndcam/). ```console $ gradle connectedAndroidTest # Run test ``` ### Use The following code shows working with `SurfaceView` class. ```java package dev.example; // ... import dev.ndcam.*; // Expect we already have a camera permission public class SurfaceDisplay implements SurfaceHolder.Callback { SurfaceView surfaceView; Surface surface; ndcam.Device camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ... // Load/Init library ndcam.CameraModel.Init(); surfaceView = findViewById(R.id.surfaceView); SurfaceHolder holder = surfaceView.getHolder(); holder.setFixedSize(1920, 1080); holder.setFormat(ImageFormat.YUV_420_888); holder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { // Get all devices in array form for(ndcam.Device device : ndcam.CameraModel.GetDevices()) if(device.facing() == CameraCharacteristics.LENS_FACING_BACK) camera = device; } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { // Make a repeating caputre request with surface surface = surfaceHolder.getSurface(); camera.repeat(surface); } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { // No more capture if(camera != null) camera.stopRepeat(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值