硬件访问服务2之让Android应用程序访问C库
16lz
2021-01-26
安卓应用程序访问c库流程:
1、编译so文件
上传hardcontrol.c到服务器然后
执行:
arm-linux-gcc -fPIC -shared hardcontrol.c -o libhardcontrol.so -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/
得到.so文件
2、将.so文件放入app/libs下建armeabi子目录
3、修改build.gradle
加上:
sourceSets{ main{ jniLibs.srcDirs = ['libs'] }}
4、找不到”libc.so.6”
解决方法:
修改编译选项
arm-linux-gcc -fPIC -shared hardcontrol.c -o libhardcontrol.so -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/-nostdlib /work/android-5.0.2/prebuilts/ndk/9/platforms/android-19/arch-arm/usr/lib/libc.so
5、在c库中添加打印信息:
#include <android/log.h> /*liblog*/ __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen ...");
然后修改编译选项:
arm-linux-gcc -fPIC -shared hardcontrol.c -o libhardcontrol.so -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -nostdlib /work/android-5.0.2/prebuilts/ndk/9/platforms/android-19/arch-arm/usr/lib/libc.so -I /work/android-5.0.2/prebuilts/ndk/9/platforms/android-19/arch-arm/usr/include /work/android-5.0.2/prebuilts/ndk/9/platforms/android-19/arch-arm/usr/lib/liblog.so
程序源码:
MainActivity.java
package com.example.dongxiem.app_0001_leddemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;import android.view.View;import android.widget.CheckBox;import android.widget.Checkable;import android.widget.Toast;import com.example.dongxiem.hardlibrary.*;public class MainActivity extends AppCompatActivity { private Button button = null; private boolean ledon = false; private CheckBox checkBoxLed1 = null; private CheckBox checkBoxLed2 = null; private CheckBox checkBoxLed3 = null; private CheckBox checkBoxLed4 = null; class MyButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { ledon = !ledon; if (ledon) { button.setText("ALL OFF"); checkBoxLed1.setChecked(true); checkBoxLed2.setChecked(true); checkBoxLed3.setChecked(true); checkBoxLed4.setChecked(true); for (int i = 0; i < 4; i++) HardControl.ledCtrl(i,1); } else { button.setText("ALL ON"); checkBoxLed1.setChecked(false); checkBoxLed2.setChecked(false); checkBoxLed3.setChecked(false); checkBoxLed4.setChecked(false); for (int i = 0; i < 4; i++) HardControl.ledCtrl(i,0); } } } public void onCheckboxClicked(View view) { // Is the view now checked? boolean checked = ((CheckBox) view).isChecked(); // Check which checkbox was clicked switch(view.getId()) { case R.id.LED1: if (checked) { // Put some meat on the sandwich Toast.makeText(getApplicationContext(), "LED1 ON", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(0,1); } else { // Remove the meat Toast.makeText(getApplicationContext(), "LED1 OFF", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(0,0); } break; case R.id.LED2: if (checked) { // Put some meat on the sandwich Toast.makeText(getApplicationContext(), "LED2 ON", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(1,1); } else { // Remove the meat Toast.makeText(getApplicationContext(), "LED2 OFF", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(1,0); } break; case R.id.LED3: if (checked) { // Put some meat on the sandwich Toast.makeText(getApplicationContext(), "LED3 ON", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(2,1); } else { // Remove the meat Toast.makeText(getApplicationContext(), "LED3 OFF", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(2,0); } break; case R.id.LED4: if (checked) { // Put some meat on the sandwich Toast.makeText(getApplicationContext(), "LED4 ON", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(3,1); } else { // Remove the meat Toast.makeText(getApplicationContext(), "LED4 OFF", Toast.LENGTH_SHORT).show(); HardControl.ledCtrl(3,0); } break; // TODO: Veggie sandwich } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.BUTTON); HardControl.ledOpen(); checkBoxLed1 = (CheckBox)findViewById(R.id.LED1); checkBoxLed2 = (CheckBox)findViewById(R.id.LED2); checkBoxLed3 = (CheckBox)findViewById(R.id.LED3); checkBoxLed4 = (CheckBox)findViewById(R.id.LED4); button.setOnClickListener(new MyButtonListener());/* button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click ledon = !ledon; if (ledon) button.setText("ALL OFF"); else button.setText("ALL ON"); } }); */ }}
HardControl.java:
package com.example.dongxiem.hardlibrary;public class HardControl { public static native int ledCtrl(int which, int status); public static native int ledOpen(); public static native int ledClose(); static { try { System.loadLibrary("hardcontrol"); } catch (Exception e) { e.printStackTrace(); } }}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dongxiem.app_0001_leddemo.MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Garmen!This is my LedDemo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/BUTTON" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ALL ON" /> <CheckBox android:id="@+id/LED1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="LED1" android:onClick="onCheckboxClicked" /> <CheckBox android:id="@+id/LED2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="LED2" android:onClick="onCheckboxClicked" /> <CheckBox android:id="@+id/LED3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="LED3" android:onClick="onCheckboxClicked" /> <CheckBox android:id="@+id/LED4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="LED4" android:onClick="onCheckboxClicked" /></LinearLayout>
Hardcontrol.c:
#include <jni.h> /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */#include <stdio.h>#include <stdlib.h>#include <android/log.h> /*liblog*/#if 0typedef struct { char *name; /* Java里调用的函数名 */ char *signature; /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */ void *fnPtr; /* C语言实现的本地函数 */} JNINativeMethod;#endifjint ledOpen(JNIEnv *env, jobject cls){ __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen ..."); return 0;}void ledClose(JNIEnv *env, jobject cls){ __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledClose ..."); return 0;}jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status){ __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledCtrl : %d, %d", which, status); return 0;}static const JNINativeMethod methods[] = { {"ledOpen", "()I", (void *)ledOpen}, {"ledClose", "()V", (void *)ledClose}, {"ledCtrl", "(II)I", (void *)ledCtrl},};/* System.loadLibrary */JNIEXPORT jint JNICALLJNI_OnLoad(JavaVM *jvm, void *reserved){ JNIEnv *env; jclass cls; if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) { return JNI_ERR; /* JNI version not supported */ } cls = (*env)->FindClass(env, "com/example/dongxiem/hardlibrary/HardControl"); if (cls == NULL) { return JNI_ERR; } /* 2. map java hello <-->c c_hello */ if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods) / sizeof(methods[0]) ) < 0) return JNI_ERR; return JNI_VERSION_1_4;}
更多相关文章
- ubuntu9.10下的android源码下载及编译
- android GUI反编译工具 APKDecoderV0.9
- 如何引入一个example到eclipse
- Mms模块ConversationList流程分析(1)
- Android(安卓)如何生成系统级应用
- Android(安卓)ICS创建StagefrightPlayer播放器的过程
- Android使用ProgressBar简单实现进度条
- keytool-importkeypair 使用
- 【Android(安卓)开发教程】编码实现获取与修改预设信息中的值