Android NDK开发环境的搭建,无需Cygwin

1 下载JDK,这个网上很多了,下载和安装就不用多说了,并新建一个JAVA_HOME的环境变量

2 下载SDK Manager,安装之后,选择要下载的API,然后下载即可,SDK Manager下载地址:http://developer.android.com/sdk/index.html

下载完成API之后,需要使用AVD Manager来创建一个虚拟机

3 下载NDK,下载地址:http://developer.android.com/tools/sdk/ndk/index.html

4 下载Ant,下载地址:http://ant.apache.org/bindownload.cgi,选择ZIP格式的下载就可以了,之后解压,之后将解压目录下的bin目录添加到系统的环境变量PATH中。

5 在NDK的目录下,创建一个批处理文件android-cmd.bat,内容如下:

set PATH=%PATH%;%cd%;I:\android\apache-ant-1.8.3\bin;D:\DevelopTools\android-sdk\platform-tools;D:\DevelopTools\android-sdk\tools;D:\DevelopTools\JDK7\bin;
cmd.exe

从上面的内容可以看出,我的ant安装在I:\android\下面,android的sdk安装在D:\DevelopTools下面,JDK安装在D:\DevelopTools下面.

原因是,编译时需要用到ant/adb/android命令,所以需要将包含该命令的目录添加到该批处理的路径中.而ant需要JDK环境.

6 运行android-cmd.bat,使用cd命令切换到samples/hello-jni目录下,

运行android update project -p . -s,会生成build.xml文件,用于ant命令,要注意的是,编译的时候会发生错误,提示:

Error: The project either has no target set or the target is invalid.
Please provide a --target to the 'android.bat update' command.

这个时候,可以使用android list targets命令来查看当前系统中的android模拟器,我有两个,如下:

I:\android\android-ndk-r7c\samples\native-activity>android.bat list targets
Available Android targets:
----------
id: 1 or "android-8"
Name: Android 2.2
Type: Platform
API level: 8
Revision: 1
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WVGA800 (default), WVGA854
ABIs : armeabi
----------
id: 2 or "android-10"
Name: Android 2.3.3
Type: Platform
API level: 10
Revision: 2
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WVGA800 (default), WVGA854
ABIs : armeabi

如果没有的话,需要使用SDK的AVD Manager来创建一个就可以了.而我使用的是android 2.3.3的模拟器,所以我的编译命令是:

I:\android\android-ndk-r7c\samples\native-activity>android update project -p . -
t android-10 -s
Updated and renamed default.properties to project.properties
Updated local.properties
No project name specified, using Activity name 'NativeActivity'.
If you wish to change it, edit the first line of build.xml.
Added file I:\android\android-ndk-r7c\samples\native-activity\build.xml
Added file I:\android\android-ndk-r7c\samples\native-activity\proguard.cfg

从上面看出,已经生成了build.xml文件了.

运行ant debug命令,输出如下:

I:\android\android-ndk-r7c\samples\hello-jni>ant debug
Buildfile: I:\android\android-ndk-r7c\samples\hello-jni\build.xml

-set-mode-check:

-set-debug-files:

-set-debug-mode:

-debug-obfuscation-check:

-setup:
[echo] Gathering info for HelloJni...
[setup] Android SDK Tools Revision 16
[setup] Project Target: Android 2.2
[setup] API level: 8
[setup]
[setup] ------------------
[setup] Resolving library dependencies:
[setup] No library dependencies.
[setup]
[setup] ------------------
[setup]
[setup] WARNING: Attribute minSdkVersion in AndroidManifest.xml (3) is lower
than the project target API level (8)

-build-setup:
[echo] Creating output directories if needed...

-pre-build:

-code-gen:
[echo] ----------
[echo] Handling aidl files...
[aidl] No AIDL files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[renderscript] No RenderScript files to compile.
[echo] ----------
[echo] Handling Resources...
[aapt] Found Deleted Target File
[aapt] Generating resource IDs...

-pre-compile:

-compile:
[javac] Compiling 2 source files to I:\android\android-ndk-r7c\samples\hello
-jni\bin\classes

-post-compile:

-obfuscate:

-dex:
[dex] Converting compiled files and external libraries into I:\android\and
roid-ndk-r7c\samples\hello-jni\bin\classes.dex...

-crunch:
[crunch] Crunching PNG Files in source dir: I:\android\android-ndk-r7c\sample
s\hello-jni\res
[crunch] To destination dir: I:\android\android-ndk-r7c\samples\hello-jni\bin
\res
[crunch] Crunched 0 PNG files to update cache

-package-resources:
[aapt] Creating full resource package...
[aapt] Warning: AndroidManifest.xml already defines debuggable (inhttp://s
chemas.android.com/apk/res/android); using existing value in manifest.

-package:
[apkbuilder] Current build type is different than previous build: forced apkbuil
der run.
[apkbuilder] Creating HelloJni-debug-unaligned.apk and signing it with a debug k
ey...

-do-debug:
[zipalign] Running zip align on final apk...
[echo] Debug Package: I:\android\android-ndk-r7c\samples\hello-jni\bin\Hell
oJni-debug.apk

debug:
[propertyfile] Creating new property file: I:\android\android-ndk-r7c\samples\he
llo-jni\bin\build.prop
[propertyfile] Updating property file: I:\android\android-ndk-r7c\samples\hello-
jni\bin\build.prop
[propertyfile] Updating property file: I:\android\android-ndk-r7c\samples\hello-
jni\bin\build.prop
[propertyfile] Updating property file: I:\android\android-ndk-r7c\samples\hello-
jni\bin\build.prop

BUILD SUCCESSFUL
Total time: 5 seconds

从上面,可以看出,生成了HelloJni-debug.apk,那么使用adb命令安装到当前的虚拟机上就可以了.

运行adb install bin/HelloJni-debug.apk命令,如果出现:error: device not found,说明你的android模拟器尚未start.

运行后,出现如下提示:

185 KB/s (85934 bytes in 0.453s)
pkg: /data/local/tmp/HelloJni-debug.apk
Success

说明该apk,已经安装到android模拟器上了,单击模拟器上的主页按钮,可以看到HelloJni程序已经有了.如下:


点击HelloJni,进入程序,可以看到Hello Jni了。

恭喜你,大功告成,收工!


  1. [译]C++实现水波纹、火焰和血浆效果
  2. [译]C# DirectShow编程手册及实例
  3. [原]C#实现将文本转换为图片
  4. [译]C#水波纹效果控件的实现
  5. [原]Dev-C++使用wkhtmltopdf的API的配置
  6. [原]C#将文本转换为图片,支持中文字符
  7. [原]C#用firefox3.6下载yunfile的文件
  8. [原]pjCLibs:方便C语言编程而作的函数库
  9. [译]C#实现同一个对话框打开目录和文件
  10. [译]C#将Enum枚举映射到文本字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值