javlibrary最新域名的新域名,不是jav2lib,这个也不管用了

document.write('&&&&');
请输入你要查询的地址:
更新TKD缓存
查询太频繁了,休息一下吧。
最近查询:
其他功能:
& 爱站网 版权所有
About AizhanWhich Is the Hottest GUI Framework in the Java World: JSF or JavaFX? | Beyond Java
Popular Articles
CategoriesThis chapter describes SWIG's support of Android.
18.1 Overview
The Android chapter is fairly short as support for Android is the same as for Java, where the Java Native Interface (JNI) is
used to call from Android Java into C or C++ compiled code.
Everything in the
applies to generating code for access from Android Java code.
This chapter contains a few Android specific notes and examples.
18.2 Android examples
18.2.1 Examples introduction
The examples require the
which can be installed as per instructions in the links.
The Eclipse version is not required for these examples as just the command line tools are used (shown for Linux as the host, but Windows will be very similar, if not identical in most places).
Add the SDK tools and NDK tools to your path and create a directory somewhere for your Android projects (adjust PATH as necessary to where you installed the tools):
$ export PATH=$HOME/android/android-sdk-linux_x86/tools:$HOME/android/android-sdk-linux_x86/platform-tools:$HOME/android/android-ndk-r6b:$PATH
$ mkdir AndroidApps
$ cd AnrdoidApps
The examples use a target id of 1. This might need changing depending on your setup.
After installation of the Android SDK, the available target ids can be viewed by running the command below.
Please adjust the id to suit your target device.
$ android list targets
The following examples are shipped with SWIG under the Examples/android directory and include a Makefile to build and install each example.
18.2.2 Simple C example
This simple C example shows how to call a C function as well as read and modify a global variable.
First we'll create and build a pure Java Android app. Afterwards the JNI code will be generated by SWIG and built into the app.
First create and build an app called SwigSimple in a subdirectory called simple using the commands below.
Adjust the --target id as mentioned earlier in the .
on the Android developer's site is a useful reference for these steps.
$ android create project --target 1 --name SwigSimple --path ./simple --activity SwigSimple --package org.swig.simple
$ cd simple
$ ant debug
Modify src/org/swig/simple/SwigSimple.java from the default to:
package org.swig.
import android.app.A
import android.os.B
import android.view.V
import android.widget.B
import android.widget.TextV
import android.widget.ScrollV
import android.text.method.ScrollingMovementM
public class SwigSimple extends Activity
TextView outputText =
ScrollView scroller =
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputText = (TextView)findViewById(R.id.OutputText);
outputText.setText("Press 'Run' to start...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView)findViewById(R.id.Scroller);
public void onRunButtonClick(View view)
outputText.append("Started...\n");
nativeCall();
outputText.append("Finished!\n");
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
/** Calls into C/C++ code */
public void nativeCall()
The above simply adds a Run button and scrollable text view as the GUI aspects of the program.
The associated resources need to be created, modify res/layout/main.xml as follows:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/RunButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run..."
android:onClick="onRunButtonClick"
&ScrollView
android:id="@+id/Scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/OutputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
&/ScrollView&
&/LinearLayout&
Rebuild the project with your changes:
$ ant debug
Although there are no native function calls in the code, yet, you may want to check that this simple pure
Java app runs before adding in the native calls.
First, set up your Android device for hardware debugging, see
on the Android developer's site.
When complete your device should be listed in those attached, something like:
$ adb devices
List of devices attached
A32-6DBE000-015D62C3- device
This means you are now ready to install the application...
$ adb install bin/SwigSimple-debug.apk
95 KB/s (4834 bytes in 0.049s)
pkg: /data/local/tmp/SwigSimple-debug.apk
The newly installed 'SwigSimple' app will be amongst all your other applications on the home screen. Run the app and it will show a Run button text box below it.
Press the Run button to see the simple text output.
The application can be uninstalled like any other application and in fact must be uninstalled before installing an updated version. Uninstalling is quite easy too from your host computer:
$ adb uninstall org.swig.simple
Now that you have a pure Java Android app working, let's add some JNI code generated from SWIG.
First create a jni subdirectory and then create some C source code in jni/example.c:
/* File : example.c */
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
while (x & 0) {
Create a SWIG interface file for this C code, jni/example.i:
/* File : example.i */
%module example
%inline %{
extern int
gcd(int x, int y);
extern double F
Invoke SWIG as follows:
$ swig -java -package org.swig.simple -outdir src/org/swig/simple -o jni/example_wrap.c jni/example.i
SWIG generates the following files:
src/org/swig/simple/exampleJNI.java
src/org/swig/simple/example.java
jni/example_wrap.c
Next we need to create a standard Android NDK build system file jni/Android.mk:
# File: Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE
:= example
LOCAL_SRC_FILES := example_wrap.c example.c
include $(BUILD_SHARED_LIBRARY)
for more on the NDK build system and getting started with the NDK.
A simple invocation of ndk-build will compile the .c files and generate a shared object/system library. Output will be similar to:
$ ndk-build
Compile thumb
: example &= example_wrap.c
Compile thumb
: example &= example.c
SharedLibrary
: libexample.so
: libexample.so =& libs/armeabi/libexample.so
Now that the C JNI layer has been built, we can write Java code to call into the this layer.
Modify the nativeCall method in src/org/swig/simple/SwigSimple.java to call the JNI code as follows and add the static constructor to load the system library containing the compiled JNI C code:
/** Calls into C/C++ code */
public void nativeCall()
// Call our gcd() function
int x = 42;
int y = 105;
int g = example.gcd(x,y);
outputText.append("The greatest common divisor of " + x + " and " + y + " is " + g + "\n");
// Manipulate the Foo global variable
// Output its current value
double foo = example.getFoo();
outputText.append("Foo = " + foo + "\n");
// Change its value
example.setFoo(3.1415926);
// See if the change took effect
outputText.append("Foo = " + example.getFoo() + "\n");
// Restore value
example.setFoo(foo);
/** static constructor */
System.loadLibrary("example");
Compile the Java code as usual, uninstall the old version of the app if still installed and re-install the new app:
$ ant debug
$ adb uninstall org.swig.simple
$ adb install bin/SwigSimple-debug.apk
Run the app again and this time you will see the output pictured below, showing the result of calls into the C code:
18.2.3 C++ class example
The steps for calling C++ code are almost identical to those in the previous C code example.
All the steps required to compile and use a simple hierarchy of classes for shapes are shown in this example.
First create an Android project called SwigClass in a subdirectory called class.
The steps below create and build a the JNI C++ app.
Adjust the --target id as mentioned earlier in the .
$ android create project --target 1 --name SwigClass --path ./class --activity SwigClass --package org.swig.classexample
$ cd class
Now create a jni subdirectory and then create a C++ header file jni/example.h which defines our
hierarchy of shape classes:
/* File : example.h */
class Shape {
nshapes++;
virtual ~Shape() {
nshapes--;
move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
class Circle : public Shape {
Circle(double r) : radius(r) { };
virtual double area(void);
virtual double perimeter(void);
class Square : public Shape {
Square(double w) : width(w) { };
virtual double area(void);
virtual double perimeter(void);
and create the implementation in the jni/example.cpp file:
/* File : example.cpp */
#include "example.h"
#define M_PI 3.
/* Move the shape to a new location */
void Shape::move(double dx, double dy) {
int Shape::nshapes = 0;
double Circle::area(void) {
return M_PI*radius*
double Circle::perimeter(void) {
return 2*M_PI*
double Square::area(void) {
return width*
double Square::perimeter(void) {
Create a SWIG interface file for this C++ code in jni/example.i:
/* File : example.i */
%module example
#include "example.h"
/* Let's just grab the original header file here */
%include "example.h"
Invoke SWIG as follows, note that the -c++ option is required for C++ code:
$ swig -c++ -java -package org.swig.classexample -outdir src/org/swig/classexample -o jni/example_wrap.cpp jni/example.i
SWIG generates the following files:
src/org/swig/classexample/Square.java
src/org/swig/classexample/exampleJNI.java
src/org/swig/classexample/example.java
src/org/swig/classexample/Circle.java
src/org/swig/classexample/Shape.java
jni/example_wrap.cpp
Next we need to create an Android NDK build system file for compiling the C++ code jni/Android.mk.
The -frtti compiler flag isn't strictly needed for this example, but is needed for any code that uses C++ RTTI:
# File: Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE
:= example
LOCAL_SRC_FILES := example_wrap.cpp example.cpp
LOCAL_CFLAGS
include $(BUILD_SHARED_LIBRARY)
A simple invocation of ndk-build will compile the .cpp files and generate a shared object/system library. Output will be similar to:
$ ndk-build
Compile++ thumb
: example &= example_wrap.cpp
Compile++ thumb
: example &= example.cpp
StaticLibrary
: libstdc++.a
SharedLibrary
: libexample.so
: libexample.so =& libs/armeabi/libexample.so
Now that the C JNI layer has been built, we can write Java code to call into this layer.
Modify src/org/swig/classexample/SwigClass.java from the default to:
package org.swig.
import android.app.A
import android.os.B
import android.view.V
import android.widget.B
import android.widget.TextV
import android.widget.ScrollV
import android.text.method.ScrollingMovementM
public class SwigClass extends Activity
TextView outputText =
ScrollView scroller =
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputText = (TextView)findViewById(R.id.OutputText);
outputText.setText("Press 'Run' to start...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView)findViewById(R.id.Scroller);
public void onRunButtonClick(View view)
outputText.append("Started...\n");
nativeCall();
outputText.append("Finished!\n");
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
/** Calls into C/C++ code */
public void nativeCall()
// ----- Object creation -----
outputText.append( "Creating some objects:\n" );
Circle c = new Circle(10);
outputText.append( "
Created circle " + c + "\n");
Square s = new Square(10);
outputText.append( "
Created square " + s + "\n");
// ----- Access a static member -----
outputText.append( "\nA total of " + Shape.getNshapes() + " shapes were created\n" );
// ----- Member data access -----
// Notice how we can do this using functions specific to
// the 'Circle' class.
c.setX(20);
c.setY(30);
// Now use the same functions in the base class
Shape shape =
shape.setX(-10);
shape.setY(5);
outputText.append( "\nHere is their current position:\n" );
outputText.append( "
Circle = (" + c.getX() + " " + c.getY() + ")\n" );
outputText.append( "
Square = (" + s.getX() + " " + s.getY() + ")\n" );
// ----- Call some methods -----
outputText.append( "\nHere are some properties of the shapes:\n" );
Shape[] shapes = {c,s};
for (int i=0; i&shapes. i++)
outputText.append( "
" + shapes[i].toString() + "\n" );
outputText.append( "
= " + shapes[i].area() + "\n" );
outputText.append( "
perimeter = " + shapes[i].perimeter() + "\n" );
// Notice how the area() and perimeter() functions really
// invoke the appropriate virtual method on each object.
// ----- Delete everything -----
outputText.append( "\nGuess I'll clean up now\n" );
// Note: this invokes the virtual destructor
// You could leave this to the garbage collector
c.delete();
s.delete();
outputText.append( Shape.getNshapes() + " shapes remain\n" );
outputText.append( "Goodbye\n" );
/** static constructor */
System.loadLibrary("example");
Note the static constructor and the interesting JNI code is in the nativeCall method.
The remaining code deals with the GUI aspects which are identical to the previous C simple example. Modify res/layout/main.xml to contain the xml for the 'Run' button and scrollable text view:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/RunButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run..."
android:onClick="onRunButtonClick"
&ScrollView
android:id="@+id/Scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/OutputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
&/ScrollView&
&/LinearLayout&
Compile the Java code as usual, uninstall the old version of the app if installed and re-install the new app:
$ ant debug
$ adb uninstall org.swig.classexample
$ adb install bin/SwigClass-debug.apk
Run the app to see the result of calling the C++ code from Java:
18.2.4 Other examples
The Examples/android directory contains further examples which can be run and installed in a similar manner to the previous two examples.
Note that the 'extend' example is demonstrates the directors feature.
Normally C++ exception handling and the STL is not available by default in the version of g++ shipped with Android, but this example turns these features on as described in the next section.
18.3 C++ STL
Should the C++ Standard Template Library (STL) be required, an Application.mk file needs to be created
in the same directory as the Android.mk directory containing information about the STL to use.
See the NDK documentation in the $NDKROOT/docs folder especially CPLUSPLUS-SUPPORT.html.
Below is an example of the Application.mk file to make the STLport static library available for use:
# File: Application.mk
APP_STL := gnustl_staticandroid 移植ffmpeg后so库的使用 -
- ITeye技术网站
博客分类:
今天折腾了一天,可算是有所收获,成功的用jni调用了libffmpeg中的一个方法-----avcodec_version(),至于avcodec_version()是干什么用的我不大清楚,应该是获取版本信息吧,没有深入的去研究ffmpeg。
这里主要是想把折腾一天所获取的经验记录下来,以免时间长全忘了,也希望能给其他人一点借鉴,不至于和我一样一点头绪都没有连猜带蒙的,本文纯属个人心得,高手可以无视....
要在android上用ffmpeg首先得奖ffmpeg工程移植到android上,这里就要用到ndk把这个开源工程编译成一个后缀为so的库,这个步骤这里就不多说了 网上的资料也挺多的,我是按照:在ubantu环境下编译的,你按照教程上一步一步来应该都没有问题,顺便给下在windows下编译ffmpeg的教程:(这个要用非ie浏览器打开)。以上两篇文章给了我很大的指引,在此谢过。。。都是牛人啊~~~
编译完以后你会获得一个libffmpeg.so的文件,那么问题来了,怎么用呢。我在百度,google搜了半天也没有一个详细的教程,总是东一句西一句的,但思路是明确的,就是还得编译一个so文件,这个so里的是jni方法,可以由java层调用的,而这些jni方法里用到的函数则就是来至libffmpeg.so了。思路是有了,但是具体怎么做呢?又经过一顿摸索,n次的编译,终于编译成功了。我是拿一个标准的ndk例子来做的测试就是ndk samples文件夹里的hello-jni工程。进入该工程的jni目录,将ffmpeg的源代码拷到该目录下,做这部的原因是你要编译的so文件里需要调用ffmpeg的方法,自然要引用ffmpeg里的h文件,然后将libffmpeg.so文件拷到ndk目录下的platforms/android-5/arch-arm/usr/lib目录下(你会发现platfroms里有好几个android文件夹如 -3 -4 -5分别代表不同的版本,以防万一我每个目录都拷了,呵呵,应该是只要拷指定目录的),因为等等系统编译的时候要用。接下来就编辑android.mk和hello-jni.c文件了 代码如下
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)
PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
LOCAL_LDLIBS := -lffmpeg
LOCAL_MODULE
:= hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
这行是定义一个变量,也就是ffmpeg源码的路径
LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)这行是指定源代码的路径,也就是刚才拷过去的ffmpeg源码,$(LOCAL_PATH)是根目录,如果没有加这行那么引入ffmpeg库中的h文件编译就会出错说找不到该h文件。
LOCAL_LDLIBS := -lffmpeg这行很重要,这是表示你这个so运行的时候依赖于libffmpeg.so这个库, 再举个例子:如果你要编译的so不仅要用到libffmpeg.so这个库还要用的libopencv.so这个库的话,你这个参数就应该写成
LOCAL_LDLIBS := -lffmpeg -lopencv
其他的参数都是正常的ndk编译用的了,不明白的话google一下。
hello-jni.c
* 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.
#include &string.h&
#include &stdio.h&
#include &android/log.h&
#include &stdlib.h&
#include &jni.h&
#include &ffmpeg/libavcodec/avcodec.h&
/* 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
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
char str[25];
sprintf(str, "%d", avcodec_version());
return (*env)-&NewStringUTF(env, str);
#include &ffmpeg/libavcodec/avcodec.h&这行是因为下面要用到avcodec_version()这个函数。
改完这两个文件以后就可以编译了~~用ndk-build命令编译完后在工程的libs/armeabi目录底下就会有一个libhello-jni.so文件了!(两行眼泪啊~终于编译成功了)
编译完成后就可以进行测试了,记得将libffmpeg.so也拷到armeabi目录底下,并在java代码中写上
System.loadLibrary("ffmpeg");
System.loadLibrary("hello-jni");
HelloJni.java
* 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.
package com.example.
import android.app.A
import android.widget.TextV
import android.os.B
public class HelloJni extends Activity
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
tv = new TextView(this);
tv.setText( "1111" );
//System.out.println();
setContentView(tv);
tv.setText(String.valueOf(stringFromJNI()));
/* A native method that is implemented by the
* 'hello-jni' native library, which is packaged
* with this application.
public native String
stringFromJNI();
/* This is another native method declaration that is *not*
* implemented by 'hello-jni'. This is simply to show that
* you can declare as many native methods in your Java code
* as you want, their implementation is searched in the
* currently loaded native libraries only the first time
* you call them.
* Trying to call this function will result in a
* java.lang.UnsatisfiedLinkError exception !
public native String
unimplementedStringFromJNI();
/* 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.
System.loadLibrary("ffmpeg");
System.loadLibrary("hello-jni");
到此就完成了,将程序装到手机可看到打印出“3426306”,google搜索“ffmpeg 3426306”得知果然是ffmpeg的东西,证明成功的调用了libffmpeg.so库里的方法了。欣慰啊~~
接下来要做的就是学习ffmpeg库里的各种函数的使用方法,以实现自己想要的功能了。
浏览 20690
浏览: 1483177 次
来自: 天津
你好,我在Windows 7用android-ndk-r8b编 ...
我说你说了这么多,就不能把so库放上来么
成功了,非常感谢!!
tgwiloveyou 写道好假啊,没人留言,浏览量居然到了1 ...
好假啊,没人留言,浏览量居然到了16万

我要回帖

更多关于 javlibrary域名 的文章

 

随机推荐