澶辨晥閾炬帴澶勭悊 |
Android java鍜孋浜掔浉璋冪敤 PDF 涓嬭澆
鏈珯鏁寸悊涓嬭澆錛?nbsp;
閾炬帴錛?a target="_blank">https://pan.baidu.com/s/1k325UbaEulmweOgpErPJKQ
鎻愬彇鐮侊細(xì)c5ij
鐩稿叧鎴浘錛?/strong>
![]()
涓昏鍐呭錛?/strong>
涓€錛孋MakeLists
鑻辨枃鏂囨。錛歨ttps://cmake.org/documentation/
涓枃鏂囨。錛歨ttps://www.zybuluo.com/khan-lau/note/254724
1錛岃礬寰勯厤緗紝鍦╞uildGradle鐨刟ndroid灞傜洰涓繘琛岄厤緗緥濡?
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
path閰嶇疆cmake鐨勫紩鐢ㄨ礬寰勶紝version閰嶇疆cmake鐨勭増鏈?/div>
2錛屾坊鍔犲簱錛屽湪cmakeList涓皟鐢╝dd_library鍑芥暟
閲岄潰浼犲叆涓変釜鍙傛暟
絎竴涓槸瑕佸紩鍏ョ殑搴撳埆鍚嶏紝絎簩涓槸搴撶殑綾誨瀷錛屾槸闈?rùn)鎬佸簱榪樻槸鍔ㄦ€佸簱銆傚湪Android涓槸涓嶈兘寮曠敤闈?rùn)鎬佸簱鐨勪篃灝辨槸.dll鍚庣紑鐨勫簱錛屾墍浠ョ浜屼釜鍙傛暟鍙鏄紩鍏ョ涓夋柟鐨?so搴撻兘鏄啓“SHARED”錛岀涓変釜鏄€氳繃浠€涔堟牱鏂瑰紡寮曞叆榪涙潵錛岀涓夋柟鐨勪竴鑸兘鏄€氳繃鍖呭惈榪涙潵錛屾墍浠ョ涓変釜鍙傛暟鍩烘湰涔熸槸鍥哄畾鐨勯兘鏄啓“IMPORTED”銆?/div>
add_library鍚庯紝灝辮璁劇疆.so鐨勮緇嗚礬寰勪簡(jiǎn)錛岄€氳繃set_target_properties()鍑芥暟鏉ヨ緗?璇ュ嚱鏁頒篃鏄浼犲叆涓夊弬鏁版潵鎸囧畾.so搴撶殑璺緞銆傜涓€涓弬鏁板拰add_library鐨勭涓€涓弬鏁頒竴鏍鳳紝涓嶈繃榪欓噷鐨勫簱鍒悕瑕佸拰add_library鐨勫簱鍒悕瑕佷竴鑷達(dá)紝瑕佷笉鐒跺湪緙栬瘧鏃朵細(xì)鎶ユ壘涓嶅埌搴撶殑閿欒銆傜浜屼釜鍙傛暟鏄浐瀹氱殑錛岄兘鏄啓“ PROPERTIES IMPORTED_LOCATION”涓昏鐢ㄦ潵鎸囧畾搴撶殑寮曞叆鏂瑰紡銆傞兘鏄€氳繃鏈湴寮曞叆銆傜涓変釜灝辨槸搴撶殑鍏蜂綋璺緞錛岃繖涓笉鑳藉啓閿欙紝濡傛灉鍐欓敊浜?jiǎn)锛尵~栬瘧鏃朵篃鍚屾牱浼?xì)鎵句笉鍒板簱鐨勩€傚彧瑕佹槸寮曞叆絎笁鏂圭殑搴撲嬌鐢╝dd_library灝辮浣跨敤set_target_propeties榪欎釜緇勫悎錛屾墍浠ュ畠浠槸鎴愬鍑虹幇鐨勩€?/div>
3錛岀紪璇戞垜浠嚜宸卞湪欏圭洰涓啓鐨刢/c++鏂囦歡
璋冪敤鏂瑰紡鍜屼笂闈㈢殑add_library鏂規(guī)硶綾諱技錛屽墠涓や釜鍙傛暟鍜屼笂闈紶鍏ョ殑鏄竴鏍風(fēng)殑錛屾渶鍚庝竴涓弬鏁板啓瀵瑰簲鎴戜滑鐨刢++鐨勬枃浠惰礬寰勶紝鍙互鍐欏涓礬寰勶紝濡備笅:
add_library( # Sets the name of the library.
main-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
main-lib.cpp
runoobtest.cpp)
榪欓噷灝變笉鐢╯et_target_propeties()
4,涓嬮潰涓€涓嚱鏁版槸find_library
榪欎釜鏂規(guī)硶鏄敤鏉ュ鎵劇郴緇熷簱鐨勶紝濡傛灉鏈夌敤鐢ㄥ埌緋葷粺搴撳氨瑕佽皟鐢ㄨ繖涓嚱鏁?/div>
絎竴涓弬鏁版槸搴撶殑鍒悕錛岀浜屼釜鍙傛暟琛ㄧず璇ュ簱鍦ㄧ郴緇焠dk涓殑鍚嶅瓧濡備笅:
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
5,涓嬩竴涓柟娉曟槸target_link_libraries,渚濊禆鐨勫簱
濡傛灉鏄郴緇熺殑搴撹鐢ㄨ繖涓牸寮?{搴撶殑鍚嶅瓧}銆?/div>
濡備笅:
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
target_link_libraries( # Specifies the target library.
main-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
浜?java璋冪敤灞侰浠g爜嫻佺▼
絎竴縐嶆柟寮忛潤(rùn)鎬佹敞鍐?
1,棣栧厛鍔犺澆鍏變韓搴擄紝鍔犺澆搴撲竴鑸槸鍦ㄥ湪闈?rùn)鎬佷唬鐮佸潡涓繘琛岋細(xì)
static {
System.loadLibrary("native-lib");
}
2錛屽湪Java涓0鏄巒ative鏂規(guī)硶
public native String stringFromJNI();
3錛屽疄鐜板師鐢熸柟娉?/div>
#include <jni.h>
#include <string>
#include <android/log.h>
extern "C" JNIEXPORT jstring JNICALL
Java_com_test_jni_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
__android_log_print(ANDROID_LOG_ERROR,"TEST","HELLO ....... native lib");
return env->NewStringUTF(hello.c_str());
}
涓轟簡(jiǎn)鎵懼埌c浠g爜涓搴旂殑鏂規(guī)硶錛岄渶瑕佽Java鍏抽敭瀛楀紑澶達(dá)紝鍦ㄦ柟娉曞墠闈㈠姞涓婂寘鍚嶏紝騫朵笖浠ヤ笅鍒掔嚎闅斿紑銆備笂闈㈢殑stringFromJjni澹版槑鍦ㄤ簡(jiǎn)com.test.jni榪欎釜鍖呭悕鐨凪ainActivity閲岄潰銆?/div>
榪欐牱鍦╦ava浠g爜閲屽氨鍙互璋冪敤stringFromJni榪欎釜鏂規(guī)硶浜?/div>
絎簩縐嶆柟寮?鍔ㄦ€佹敞鍐?/div>
1錛屽拰闈?rùn)鎬佹敞鍐屼竴鏍鳳紝鍏堝姞杞藉叡浜簱
static {
System.loadLibrary("dynamic-lib");
}
2錛屽湪java涓0鏄巒ative鏂規(guī)硶
public native int sum(int x, int y);
public native String getNativeString();
3錛屽湪c浠g爜涓腑娣誨姞鍥炶皟鍑芥暟JNI_OnLoad鏂規(guī)硶錛宭oadLibrary浼?xì)鎵ц璇ユ栆?guī)硶銆?/div>
鐒跺悗鍦↗NI_OnLoad鏂規(guī)硶涓皟鐢ㄦ柟娉昍egisterNatives灝卞畬鎴愭敞鍐屼簡(jiǎn)濡備笅:
JNIEXPORT int JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
|