千家信息网

linux 命令行下怎么使用android sdk 以及ndk

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容介绍了"linux 命令行下怎么使用android sdk 以及ndk"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家
千家信息网最后更新 2026年11月02日linux 命令行下怎么使用android sdk 以及ndk

本篇内容介绍了"linux 命令行下怎么使用android sdk 以及ndk"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

  • 环境

首先保证已经安装了java,sdk,ndk 相关的一些包,并有类似如下的配置:

export NDK_ROOT=/home/develop/android-ndk-r9cexport SDK_ROOT=/home/develop/adt-bundle-linux-x86_64-20131030/sdkPATH=$PATH:$SDK_ROOT/toolsPATH=$PATH:$NDK_ROOTexport JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  • android SDK的使用

  • 命令行下创建工程

mkdir proj.androidcd proj.androidandroid create project -k wy.first -a helloandroid -n chinease -t android-17 -p ./

其中 -k 指定包名(必选)

-a 指定activity名(必选)

-t指定target(必选),例如此处android-17对于android4.2

-n指定工程名(可选) ,若不指定该选项,则默认使用activity名字作为工程名字。

-p 指定工程目录(必选)

运行完毕会自动生成src/wy/first/helloandroid.java源文件:

package wy.first;import android.app.Activity;import android.os.Bundle;public class helloandroid extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }}
  • 编译java并打包成apk

ant debug
  • 将编译出的debug版的apk包安装到目标设备上

ant installd

上面两个命令也可以ant debug install一次完成,ant的命令部分不在赘述,可自行键入ant help查看:

kimo@debian-desktop:~/proj.android$ ant helpBuildfile: /home/kimo/proj.android/build.xmlhelp:     [echo] Android Ant Build. Available targets:     [echo]    help:      Displays this help.     [echo]    clean:     Removes output files created by other targets.     [echo]               This calls the same target on all dependent projects.     [echo]               Use 'ant nodeps clean' to only clean the local project     [echo]    debug:     Builds the application and signs it with a debug key.     [echo]               The 'nodeps' target can be used to only build the     [echo]               current project and ignore the libraries using:     [echo]               'ant nodeps debug'     [echo]    release:   Builds the application. The generated apk file must be     [echo]               signed before it is published.     [echo]               The 'nodeps' target can be used to only build the     [echo]               current project and ignore the libraries using:     [echo]               'ant nodeps release'     [echo]    instrument:Builds an instrumented package and signs it with a     [echo]               debug key.     [echo]    test:      Runs the tests. Project must be a test project and     [echo]               must have been built. Typical usage would be:     [echo]                   ant [emma] debug install test     [echo]    emma:      Transiently enables code coverage for subsequent     [echo]               targets.     [echo]    install:   Installs the newly build package. Must either be used     [echo]               in conjunction with a build target (debug/release/     [echo]               instrument) or with the proper suffix indicating     [echo]               which package to install (see below).     [echo]               If the application was previously installed, the     [echo]               application is reinstalled if the signature matches.     [echo]    installd:  Installs (only) the debug package.     [echo]    installr:  Installs (only) the release package.     [echo]    installi:  Installs (only) the instrumented package.     [echo]    installt:  Installs (only) the test and tested packages (unless     [echo]               nodeps is used as well.     [echo]    uninstall: Uninstalls the application from a running emulator or     [echo]               device. Also uninstall tested package if applicable     [echo]               unless 'nodeps' is used as well.

手机上的运行效果:

哇塞,居然自带hello world,不明觉厉有木有,看来以后我们这种只会写hello world的人都没法混了,不行,作为一个专注hello world 30年的无脑码农,一定要把这个主动权夺回来!!!搜索了一番才知道其实是刚刚android create project的时候,创建了一个默认的xml界面布局文件res/layout/main.xml,默认代码如下

  • 新建工程的默认ui布局

所以为了能在代码中控制那个TextView控件,我们在TextView节点中给它加上一行id属性

android:id="@+id/myTextView"

然后修改src/wy/first/helloandroid.java文件

package wy.first;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;                                                    //addpublic class helloandroid extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        TextView myTextView = (TextView) findViewById(R.id.myTextView); //add        myTextView.setText("不写可以么"); //add    }}

再次ant debug install,运行效果如下,恩,这才是真正的hello world嘛


  • android NDK的使用

  • 在类中声明本地方法

为了个java代码增加本地jni调用,再次修改src/wy/first/helloandroid.java

package wy.first;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class helloandroid extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        TextView myTextView = (TextView) findViewById(R.id.myTextView);        myTextView.setText(stringFromJNI());//modify    }    public native String stringFromJNI();//add    static {         System.loadLibrary("testso");   //add    }}
  • 编译java源文件

键入ant debug 即可自动编译

  • 生成jni头文件

然后使用javah工具自动生成jni调用的c代码头文件

javah -classpath bin/classes   -d jni wy.first.helloandroid  错误: 无法访问android.app.Activity  找不到android.app.Activity的类文件

神马情况,居然又给我报错,折腾半小时后终于有了点眉目,总之就是找不到android.app.Activity包,需要手动指定一个参数,我这里以target为android-17为例:

javah -classpath bin/classes -bootclasspath /home/develop/adt-bundle-linux-x86_64-20131030/sdk/platforms/android-17/android.jar  -d jni wy.first.helloandroid

果断执行成功,此时我们的当前目录下自动生成了一个jni目录,以及jni/wy_first_helloandroid.h头文件。

  • 实现jni调用的c函数

现在要做的就是用c实现该头文件中申明的函数,so,创建 jni/wy_first_helloandroid.c文件

#include #include #include "wy_first_helloandroid.h"JNIEXPORT jstring JNICALL Java_wy_first_helloandroid_stringFromJNI(JNIEnv *env, jobject obj){    return (*env)->NewStringUTF(env, "hello ,么以可写不");}
  • ndk-build编译生成so

创建jni/Android.mk文件,其中LOCAL_MODULE的值应与java代码中的一致

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := testsoLOCAL_SRC_FILES := wy_first_helloandroid.cinclude $(BUILD_SHARED_LIBRARY)

然后就可以编译so了,键入ndk-build回车

kimo@debian-desktop:~/proj.android$ ndk-build [armeabi] Install        : libtestso.so => libs/armeabi/libtestso.so

果断so就生成好了,再次ant debug install,运行效果如下

"linux 命令行下怎么使用android sdk 以及ndk"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

文件 生成 编译 命令 代码 工程 运行 再次 效果 目录 自动生成 果断 内容 函数 名字 就是 布局 情况 更多 源文件 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 天津市摩尔互联网科技 互联网科技工作委员会副主任毛军 互联网科技公司的优势 北京互联网络科技有限公司 新浪科技互联网 互联网科技论坛 深圳艾佳互联网科技 互联网科技公司招聘 银川新华互联网科技学校地址 博兴的互联网电子科技公司 茂名市互联网科技产业园 成都铂石互联网科技做什么的 昊天互联网科技有限公司 中国航天科技大学国际工业互联网 畅思互联网科技 互联网新科技动漫ppt素材 工业互联网等科技实现智能化赋能 长沙互联网科技 上海京穆互联网科技有限公司 中国互联网有什么科技 中关村互联网 科技集团 苏州找大状互联网科技有限公司 互联网属科技 中国互联网科技发展 互联网科技咨询 福建科技产业互联网发展 中国互联网公司的科技投入 河南商务圈互联网科技有限公司 重庆科技产业互联网有哪些 青岛互联网科技园
0