The first Jni example in Ubuntu

Source: Internet
Author: User
Switch the terminal to the & lt; sdk & gt;/tools/directory and run the Android command to open AVDManager. However, if you do not add & lt; sdk & gt;/tools to your environment variable, enter the android press enter and the terminal will only prompt you for android: cannot find

Terminal Switch Run the Android command in the/tools/directory to open AVD Manager. However, if you do not add /Tools to your environment variables, enter android press enter and the terminal will only prompt you

Android: Command not found

 

You only need to add a./In front of android to solve the problem:

./Android

The simpler way is /Add the tools path to the path environment variable. It can be added to user-level environment variables or system environment variables. You can use commands or edit files. Here, I only provide one method to open the terminal and enter:

Sudo gedit/etc/environment

Press enter, In PATH = "......................" In double quotes:

: /Tools: /Platform-tools

For example:

:/Opt/android-sdk/tools:/opt/android-sdk/platform-tools

Note: It is a separator.

Restart orSource/etc/environment (effective immediately)Enter android press enter on the terminal to respond.

 

Install Android NDK

Android NDK: http://developer.android.com/sdk/ndk/index.html

Echo decompress to get the android-ndk-r6b directory. Tar-jxvf android-ndk-r6b-linux-x86.tar.bz2

 

Also add its path to the source Path

 

 

 

After completing the preceding steps, run the following command:

Ndk-bulid

The following error is displayed, instead of ndk-build not found, indicating that the ndk environment has been installed successfully.

Android NDK: cocould not find application project directory!
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/Home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk: 85: *** Android NDK: Aborting. Stop.

 

Ii. Coding

1. First, write java code

Create an Android app project HelloJni and create the HelloJni. java file:

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.hellojni;import android.app.Activity;import android.widget.TextView;import android.os.Bundle;public class HelloJni extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        /* Create a TextView and set its content.         * the text is retrieved by calling a native         * function.         */        TextView  tv = new TextView(this);        tv.setText( stringFromJNI() );        setContentView(tv);    }    /* 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.     */    static {        System.loadLibrary("hello-jni");    }}

This code is simple and the comments are clear. Here we only mention two points::

Static {
System. loadLibrary ("Hello-jni");
}

It indicates that the hello-jni will be loaded when the program starts to run, and the code declared in the static area will be executed before the onCreate method. If your program has multiple classes and if HelloJni is not the entry to your application, then hello-jni (the complete name is libhello-jni.so) this library will be loaded when HelloJni is used for the first time.

Public native String stringFromJNI ();
Public native String unimplementedStringFromJNI ();

We can see that the declaration of the two methods contains the native keyword, which indicates that the two methods are local methods, that is, these two methods are implemented through local code (C/C ++, in java code, it is just declaration.

Compile the project with eclipse to generate the corresponding. class file. This step must be completed before the next step, because the corresponding. class file is used to generate a. h file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.