---
title: "JNI应用程序开发实例1"
author: "mmdev"
date: "2011-12-10T09:22:00Z"
tags: []
slug: "jni-1"
draft: false
lastmod: "2011-12-10T16:18:50Z"
id: 1301532
url: "https://mmdev.iteye.com/blog/1301532"
---

文件1：

com\\teleca\\robin\\HelloActivity.java

package com.teleca.robin;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import com.teleca.robin.R;;

public class HelloActivity extends Activity {

/\** Called when the activity is first created. \*/

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

TextView textView=(TextView)findViewById(R.id.textView1);

textView.setText(getStringFromJni());

}

native String getStringFromJni();

/* this is used to load the 'hello-jni' library on application

\* startup. The library has already been unpacked into

\* /data/data/com.example.HelloJni/lib/libhello-jni.so at

\* installation time by the package manager.

\*/

static {

System.loadLibrary("hello-jni");

}

}

文件2：

jni\\hello-jni.c

\*/

#include &lt;string.h&gt;

#include&lt;jni.h&gt;

/* This is a trivial JNI example where we use a native method

\* to return a new VM String. See the corresponding Java source

\* file located at:

\*

\* apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java

\*/

jstring

Java\_com\_teleca\_robin\_hellojni\_HelloActivity\_getStringFromJni( JNIEnv* env,

jobject thiz )

{

return (\*env)-&gt;NewStringUTF(env, "Hello robin,this world is from JNI !");

}

注意：该jni文件要为于jni目录下。

文件3：

jni\\Android.mk

\# Copyright (C) 2009 The Android Open Source Project

\#

\# Licensed under the Apache License, Version 2.0 (the "License");

\# you may not use this file except in compliance with the License.

\# You may obtain a copy of the License at

\#

\# http://www.apache.org/licenses/LICENSE-2.0

\#

\# Unless required by applicable law or agreed to in writing, software

\# distributed under the License is distributed on an "AS IS" BASIS,

\# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

\# See the License for the specific language governing permissions and

\# limitations under the License.

\#

LOCAL\_PATH := $(call my-dir)

include $(CLEAR\_VARS)

LOCAL\_MODULE := hello-jni

LOCAL\_SRC\_FILES := hello-jni.c

include $(BUILD\_SHARED\_LIBRARY)

注意1：LOCAL\_MODULE是你要生成的so档案文件名。

注意2：LOCAL\_SRC\_FILES为你要编译的jni代码文件，文件之间用分号";"隔开。

文件4：

res\\values\\strings.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;resources&gt;

&lt;string name="hello"&gt;Hello World, HelloActivity!&lt;/string&gt;

&lt;string name="app\_name"&gt;JiniHello&lt;/string&gt;

&lt;/resources&gt;

文件5：

res\\layout\\main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

&gt;

&lt;TextView

android:id="@+id/textView0"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="@string/hello"

/&gt;

&lt;TextView

android:id="@+id/textView1"

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="@string/hello"

/&gt;

&lt;/LinearLayout&gt;

文件6：

AndroidManifest.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.teleca.robin"

android:versionCode="1"

android:versionName="1.0"&gt;

&lt;application android:icon="@drawable/icon" android:label="@string/app\_name"&gt;

&lt;activity android:name=".HelloActivity"

android:label="@string/app\_name"&gt;

&lt;intent-filter&gt;

&lt;action android:name="android.intent.action.MAIN" /&gt;

&lt;category android:name="android.intent.category.LAUNCHER" /&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;/application&gt;

&lt;uses-sdk android:minSdkVersion="7" /&gt;

&lt;/manifest&gt;