Android NDK Build and some bugs

Source: Internet
Author: User

First download the NDK and then in the outermost

Write the path to the NDK in the Local.properties folder

ndk.dir=/users/wyj/desktop/androidstudiosdk/android-ndk-r10d

1. New Project MyApp

2. Write well in mainactivity

1 public static native String stringfromjni ();

2. Generate the corresponding class file

Right-click Project Make Module ' MyApp ' will generate a lot of *.class in the build folder

3. Generate the native. h file

Note that Mainactivity.class is a binary file, and the current folder directory is MyApp

The terminal switches to MyApp under execution to take Android.jar, because some classes in activity require classes in Android.jar

Under Mac (:)

Javah-classpath Build/intermediates/classes/debug :/users/wyj/desktop/a/ Android.jar - D jni com.example.wyj.myapplication.MainActivity

Under Windows (;)

Javah-classpath bin\\classes; f:\\aa\\android.jar-d JNI com.example.wyj.myapplication.MainActivity

Among the contents of ANDROID.MK are:

Local_module    : = Hello     generated library name  generated  libhello.so
Local_src_files: = hello.c Specifies which files are . c files
1 # Copyright (C) The Android Open Source Project2 #3 # Licensed under the Apache License, Version 2.0 (the "License");4 # You are not a use of this file except in compliance with the License.5 # Obtain a copy of the License at6 #7 # http://www.apache.org/licenses/LICENSE-2.08 #9 # unless required by applicable or agreed to writing, softwareTen # Distributed under the License is distributed on a "as is" BASIS, One # without warranties or CONDITIONS of any KIND, either express or implied. A # See the License for the specific language governing permissions and - # Limitations under the License. - # the Local_path: = $ (call My-dir) -  - include $ (clear_vars) -  + local_module: = Hello - local_src_files: = hello.c +  AInclude $ (build_shared_library)
View Code

The contents of Com_example_wyj_myapplication_mainactivity.h are as follows

1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 /*Header for Class com_example_wyj_myapplication_mainactivity*/4 5 #ifndef _included_com_example_wyj_myapplication_mainactivity6 #define_included_com_example_wyj_myapplication_mainactivity7 #ifdef __cplusplus8 extern "C" {9 #endifTen #undefCom_example_wyj_myapplication_mainactivity_bind_above_client One #defineCom_example_wyj_myapplication_mainactivity_bind_above_client 8L A #undefCom_example_wyj_myapplication_mainactivity_bind_adjust_with_activity - #defineCom_example_wyj_myapplication_mainactivity_bind_adjust_with_activity 128L - #undefCom_example_wyj_myapplication_mainactivity_bind_allow_oom_management the #defineCom_example_wyj_myapplication_mainactivity_bind_allow_oom_management 16L - #undefCom_example_wyj_myapplication_mainactivity_bind_auto_create - #defineCom_example_wyj_myapplication_mainactivity_bind_auto_create 1L - #undefCom_example_wyj_myapplication_mainactivity_bind_debug_unbind + #defineCom_example_wyj_myapplication_mainactivity_bind_debug_unbind 2L - #undefCom_example_wyj_myapplication_mainactivity_bind_important + #defineCom_example_wyj_myapplication_mainactivity_bind_important 64L A #undefCom_example_wyj_myapplication_mainactivity_bind_not_foreground at #defineCom_example_wyj_myapplication_mainactivity_bind_not_foreground 4L - #undefCom_example_wyj_myapplication_mainactivity_bind_waive_priority - #defineCom_example_wyj_myapplication_mainactivity_bind_waive_priority 32L - #undefCom_example_wyj_myapplication_mainactivity_context_ignore_security - #defineCom_example_wyj_myapplication_mainactivity_context_ignore_security 2L - #undefCom_example_wyj_myapplication_mainactivity_context_include_code in #defineCom_example_wyj_myapplication_mainactivity_context_include_code 1L - #undefcom_example_wyj_myapplication_mainactivity_context_restricted to #defineCom_example_wyj_myapplication_mainactivity_context_restricted 4L + #undefCom_example_wyj_myapplication_mainactivity_mode_append - #defineCom_example_wyj_myapplication_mainactivity_mode_append 32768L the #undefCom_example_wyj_myapplication_mainactivity_mode_enable_write_ahead_logging * #defineCom_example_wyj_myapplication_mainactivity_mode_enable_write_ahead_logging 8L $ #undefCom_example_wyj_myapplication_mainactivity_mode_multi_processPanax Notoginseng #defineCom_example_wyj_myapplication_mainactivity_mode_multi_process 4L - #undefCom_example_wyj_myapplication_mainactivity_mode_private the #defineCom_example_wyj_myapplication_mainactivity_mode_private 0L + #undefCom_example_wyj_myapplication_mainactivity_mode_world_readable A #defineCom_example_wyj_myapplication_mainactivity_mode_world_readable 1L the #undefCom_example_wyj_myapplication_mainactivity_mode_world_writeable + #defineCom_example_wyj_myapplication_mainactivity_mode_world_writeable 2L - #undefCom_example_wyj_myapplication_mainactivity_default_keys_dialer $ #defineCom_example_wyj_myapplication_mainactivity_default_keys_dialer 1L $ #undefCom_example_wyj_myapplication_mainactivity_default_keys_disable - #defineCom_example_wyj_myapplication_mainactivity_default_keys_disable 0L - #undefCom_example_wyj_myapplication_mainactivity_default_keys_search_global the #defineCom_example_wyj_myapplication_mainactivity_default_keys_search_global 4L - #undefCom_example_wyj_myapplication_mainactivity_default_keys_search_localWuyi #defineCom_example_wyj_myapplication_mainactivity_default_keys_search_local 3L the #undefCom_example_wyj_myapplication_mainactivity_default_keys_shortcut - #defineCom_example_wyj_myapplication_mainactivity_default_keys_shortcut 2L Wu #undefCom_example_wyj_myapplication_mainactivity_result_canceled - #defineCom_example_wyj_myapplication_mainactivity_result_canceled 0L About #undefCom_example_wyj_myapplication_mainactivity_result_first_user $ #defineCom_example_wyj_myapplication_mainactivity_result_first_user 1L - #undefCom_example_wyj_myapplication_mainactivity_result_ok - #definecom_example_wyj_myapplication_mainactivity_result_ok-1l - /* A * class:com_example_wyj_myapplication_mainactivity + * Method:stringfromjni the * Signature: () ljava/lang/string; -  */ $ jniexport jstring jnicall java_com_example_wyj_myapplication_mainactivity_stringfromjni the(JNIENV *, jclass); the  the #ifdef __cplusplus the } - #endif in #endif
View Code

Create a hello.c yourself

1#include <stdio.h>2#include <string.h>3#include"Com_example_wyj_myapplication_mainactivity.h"4 5 jniexport jstring jnicall java_com_example_wyj_myapplication_mainactivity_stringfromjni6(JNIENV *env, Jclass Jclass) {7     return(*env)->newstringutf (env,"Hello from wf!");8}
View Code

4. Generate So File

The terminal executes ndk-build in the first-level directory of the JNI (the folder in the path where the Ndk-build is located must not have a space or an error is made)

5. Using the so file

Create the Jnilibs folder in main and copy the armeabi/*.so into it and then you can access it.

Or do not create a new Jnilibs folder, then in Build.gradle

Android {
sourcesets {
Main {
Jnilibs.srcdirs = [' xxx ']
}
}
}

jnilibs.srcdirs = [' xxx ']//here XXX stands for a directory. Can change the read path of the jnilibs, the default jnilibs structure is Src/main/jnilibs

Specifies that XXX is libs can be armeabi does not need to be specified in the path, it is automatically under different devices will be looked for.

6. Write in the mainactivity

     Public Static native String  stringfromjni ();     Static {        system.loadlibrary ("Hello");   Load library file    }    @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        String str=stringfromjni ();  // can be used         System.out.print (str);    }

7. Run the program and you can see the results stringfromjni.

Additional Instructions:

The functions in the. c file are

1 jniexport jstring jnicall java_com_example_wyj_myapplication_mainactivity_stringfromjni

The structure of the class is the mainactivity class under the com.example.wyj.myapplication file package Structure Stringfromjni function to be recognized and used

Put in other projects if you do not follow this structure will be an error, function can not find, of course, *.so to load.

If other projects follow the following structure , it is also possible to identify the stringfromjni function and use

Android NDK Build and some bugs

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.