我想在android中添加小C代码作为JNI,用于学习目的。 在这里,我使用Android Marshmallow并使用' aosp_arm-eng'在ubuntu 14.04。它已正确编译并正常工作。但是当我将JNI代码(带有print语句的基本c代码)添加到ZygoteInit.java文件时,我收到的信号低于SIGSEGV。
创建了一个.so库,它是一个基本的C JNI代码:
file: Android.mk:
-----------------------<<文件开头>> ------------------------------
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libctest
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := \
ctest.c
LOCAL_SHARED_LIBRARIES := \
libutils \
libcutils \
liblog
LOCAL_LDFLAGS := -Wl,--hash-style=sysv
LOCAL_CFLAGS := -DSTDC_HEADERS
LOCAL_ARM_MODE := arm
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
include $(BUILD_SHARED_LIBRARY)
-----------------------<<文件结束>> ------------------------------
file: ctest,c
-----------------------<<文件开头>> -----------------------------
#include <android/log.h>
#include <jni.h>
#include <JNIHelp.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#if 1
#include <utils/Log.h>
#define printI(fmt, ...) \
ALOGI("[ctest]: "fmt, ##__VA_ARGS__)
#else
#if 0
#define printI(fmt, ...) \
printf("[ctest]: "fmt, ##__VA_ARGS__)
#else
#define printI(fmt, ...) ;
#endif
#endif
JNIEXPORT void JNICALL Java_com_android_internal_os_ZygoteInit_ZygoteFromC (JNIEnv * env __unused, jobject obj __unused)
{
printI("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n");
printI("MAX_PATH: %d\n", MAX_PATH);
printI("%s():%d: Hello from C!\n", __func__, getpid());
printI("%s():%d: Hello from C!\n", __func__, getpid());
}
-----------------------&LT;&LT;文件结束&gt;&gt; ------------------------------
file: frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
-----------------------&LT;&LT;文件开头&gt;&gt; -----------------------------
之前&#39; public static void main(String argv []){&#39;线
public native void ZygoteFromC(); /* (1) */
static {
Log.i(TAG, "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");
try {
System.loadLibrary("ctest"); /* (2) */
} catch (UnsatisfiedLinkError e) {
Log.i(TAG, "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
e.printStackTrace();
}
Log.i(TAG, "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");
}
在preload()执行后的main()函数中
preload();
Log.i(TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
// ZygoteInit zygote_init = new ZygoteInit();
// zygote_init.ZygoteFromC();
new ZygoteInit().ZygoteFromC();
// ZygoteFromC();
Log.i(TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
SystemClock.uptimeMillis());
-----------------------&LT;&LT;文件结束&gt;&gt; ------------------------------
添加此代码后,它正在正确编译,但我在使用模拟器运行AOSP时出现以下错误:
06-18 10:28:07.541 1473 1473 W Resources: Preloaded drawable resource #0x10806f0 (android:drawable/text_select_handle_middle_material) that varies with configuration!!
06-18 10:28:07.545 1473 1473 W Resources: Preloaded drawable resource #0x10806f3 (android:drawable/text_select_handle_right_material) that varies with configuration!!
06-18 10:28:07.570 1473 1473 I Zygote : ...preloaded 342 resources in 5004ms.
06-18 10:28:07.700 1473 1473 I Zygote : ...preloaded 41 resources in 129ms.
06-18 10:28:07.701 1473 1473 D libEGL : Emulator without GPU support detected. Fallback to software renderer.
06-18 10:28:07.737 1473 1473 D libEGL : loaded /system/lib/egl/libGLES_android.so
06-18 10:28:08.216 1473 1473 I Zygote : Preloading shared libraries...
06-18 10:28:08.301 1473 1473 D Zygote : end preload
06-18 10:28:08.301 1473 1473 I Zygote : AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
--------- beginning of crash
06-18 10:28:08.307 1473 1473 F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x200c in tid 1473 (main)
06-18 10:28:08.349 65 65 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-18 10:28:08.360 65 65 F DEBUG : Build fingerprint: 'Android/aosp_arm/generic:6.0.1/MOI10E/omkar.bolla04121156:eng/test-keys'
06-18 10:28:08.360 65 65 F DEBUG : Revision: '0'
06-18 10:28:08.360 65 65 F DEBUG : ABI: 'arm'
06-18 10:28:08.361 65 65 F DEBUG : pid: 1473, tid: 1473, name: main >>> zygote <<<
06-18 10:28:08.361 65 65 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x200c
06-18 10:28:08.447 65 65 F DEBUG : r0 b4dd6a80 r1 bea3d56c r2 00430000 r3 00000001
06-18 10:28:08.447 65 65 F DEBUG : r4 000072a6 r5 710ea020 r6 12e6fb30 r7 00000003
06-18 10:28:08.447 65 65 F DEBUG : r8 7137d410 r9 b4df6500 sl 12c36200 fp 713726d8
06-18 10:28:08.447 65 65 F DEBUG : ip b4dbc000 sp bea3d560 lr 7383b381 pc 0000200c cpsr 60000010
06-18 10:28:08.463 65 65 F DEBUG :
06-18 10:28:08.463 65 65 F DEBUG : backtrace:
06-18 10:28:08.464 65 65 F DEBUG : #00 pc 0000200c <unknown>
06-18 10:28:08.464 65 65 F DEBUG : #01 pc 7383b37f /data/dalvik-cache/arm/system@framework@boot.oat (offset 0x1ed6000)
06-18 10:28:11.544 65 65 F DEBUG :
06-18 10:28:11.544 65 65 F DEBUG : Tombstone written to: /data/tombstones/tombstone_02
06-18 10:28:11.628 57 57 I ServiceManager: service 'media.audio_flinger' died
06-18 10:28:11.641 57 57 I ServiceManager: service 'media.player' died
06-18 10:28:11.641 57 57 I ServiceManager: service 'media.resource_manager' died
06-18 10:28:11.641 57 57 I ServiceManager: service 'media.camera' died
06-18 10:28:11.641 57 57 I ServiceManager: service 'media.audio_policy' died
06-18 10:28:11.641 57 57 I ServiceManager: service 'media.sound_trigger_hw' died
06-18 10:28:11.641 57 57 I ServiceManager: service 'media.radio' died
06-18 10:28:12.072 1625 1625 I Netd : Netd 1.0 starting
06-18 10:28:12.074 1625 1625 D TetherController: Setting IP forward enable = 0
06-18 10:28:14.601 1626 1626 V MediaUtils: physMem: 510377984
06-18 10:28:14.601 1626 1626 V MediaUtils: requested limit: 331745635
06-18 10:28:14.602 1626 1626 V MediaUtils: actual limit: 331745635
06-18 10:28:14.602 1626 1626 V MediaUtils: original limits: 4294967295/4294967295
06-18 10:28:14.602 1626 1626 V MediaUtils: new limits: 331745635/4294967295
06-18 10:28:14.634 1626 1626 I mediaserver: ServiceManager: 0xb608e120
06-18 10:28:14.635 1626 1626 I AudioFlinger: Using default 3000 mSec as standby time.
请帮我在模拟器中使用JNI启动我的C代码。
谢谢,
Omkar B