Tutorial on creating JNI in the latest Android Studio, androidjni

Source: Internet
Author: User

Tutorial on creating JNI in the latest Android Studio, androidjni

The main purpose of this article is as follows, and provides solutions for problems that may occur during creation.

For more information, see http://ph0b.com/android-studio-gradle-and-ndk-integration /.

You can directly stamp in...

Main Ideas: Create the native method in a class, compile and generate the class file of the class, and then generate the class header file. With the header file, compile the code in the c file for specific implementation, configure the sdk and ndk path, name the ndk module, and call the method.

The detailed steps are as follows: 1. Create native locally

Create an empty android project and declare the native method in the class

Suggestion: do not declare the native method in the activity. One is to simplify the design and separate functions, and the other is to avoid unnecessary troubles when creating header files.

public class JniUtil {    public native String getStringFromNative();}

 

2. Compile

To obtain the. class file with the native method class.

As shown in figure

If you are not familiar with the javah command, you can enter javah-help.

-O <file> output file (only-d or-o can be used)
-D <dir> output directory
-V-verbose enables detailed output
-H -- help -? Output this message
-Version: Output version information.
-Jni: generate a JNI-style header file (default)
-Force always writes the input file
-Classpath <path>: the path from which the class is loaded

Path introduction:E: \ android-sdks \ platforms \Android-23\ Android. jar is the sdk version that the android project depends on and is eventually locked to the android. jar file;.. \ Build \ intermediates \ classes \ debugCom. example. hitman. final. JniUtil indicates the. class file on which the header file is generated.SemicolonSeparate

After successful, you will see an extra-jni file under the main folder (some may generate the jni folder directly ). It contains compiled header files.

Possible problems:

1) Some class files cannot be foundIn more cases, the native method is directly declared in the activity. Because the android version of The AS project is relatively high, this causes e: \ android-sdks \ platforms \Android-23\ Android. jar does not have the required class, that is, javah-d cannot find the required path. The following is an example.

Import android. support. v7.app. AppCompatActivity; public class MainActivity extends AppCompatActivity {}View Code

 

In this case, appCompatActivity cannot be found because e: \ android-sdks \ platforms \Android-23\ Android. jar path does not have this class, the actual path of appCompatActivity is E: \ android-sdks \ extras \ android \ m2repository \ com \ android \ support \ appcompat-v7 \ 23.1.1 (my path)

Solution: migrate THE native METHOD out of the activity.OrChange the public class MainActivity extends AppCompatActivity to public class MainActivity extends Activity.

3.3 create a. c file, which must be in the jni folder

Note that,Rename the-jni folder to jni. Otherwise, the ndk library cannot be generated under the build File during compilation.(I have encountered this problem), rename the name and create a new. c file in the jni folder. Content:

# Include <strong> JNIEXPORT jstring JNICALL values (JNIEnv * env, jobject obj) {return (* env)-> NewStringUTF (env, "hello from nativejni ");};View Code

 

Note: Be sure to add the new header file, as shown in blue.

Possible problems: for some AS bugs, you must create two. c files to compile successfully.

For more information about the syntax of the. c file, see http://blog.chinaunix.net/uid-26000296-id-5296362.html.
4. Configure the sdk and ndk paths and compile 4.1 to open the local. propertiesZ file.

Ndk. dir = E \: \ android-sdks \ ndk-bundle
Sdk. dir = E \: \ android-sdks
4.2 After setting the path, compile it and you will be able to see the libraries in the ndk under the build price.

Possible problems

Error: Execution failed for task': app: compileDebugNdk '.
> Error: NDK integration is deprecated in the current plugin. consider trying the new experimental plugin. for details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental. set "android. useDeprecatedNdk = true "in gradle. properties to continue using the current NDK integration.

You can add android. useDeprecatedNdk = true to the gradle. properties file and re-compile it.

public class MainActivity extends AppCompatActivity {     static{         System.loadLibrary("MyLib");     }     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.content_main);         TextView txt=(TextView)findViewById(R.id.txt);         JniUtil util=new JniUtil(this);         txt.setText(util.getStringFromNative());             }         });     } }

 

So far, make sure that the compilation can run normally. The simulator is as follows:

If you have any questions, comments, or suggestions, please leave a message.

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.