实现自己的HAL-8 通过Android 系统编译自己的app,用于控制hal

通过Android 系统编译自己的app,用于控制hal

  • 系统app 是在 Z:\itop-3399_8.1\packages\apps 这个路径下
  • 在Z:\itop-3399_8.1\packages\apps 创建 HelloWorld
  • HelloWorld 下创建res、src文件夹,以及 Android.mk
    如图
  • 将Android studio 中 或则 eclipse 中生成的AndroidManifest.xml 拷贝过来
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myapplication">

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       >
       <activity
           android:name="com.example.myapplication.MainActivity"
           android:label="@string/app_name"
           >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>
  • Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_PACKAGE_NAME := HelloWorld

LOCAL_CERTIFICATE := platform
#LOCAL_PRIVILEGED_MODULE := true

include $(BUILD_PACKAGE)

# Use the following include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
  • res 文件

HelloWorld文件的结构图

  • 将Android studio下 和 eclipse 下的 res 拷贝过来
  • 我的只对stirng.xml 做了修改
<resources>
    <string name="app_name">My Application</string>
    <string name="action_settings">Settings</string>
    <!-- Strings used for fragments for navigation -->
    <string name="first_fragment_label">First Fragment</string>
    <string name="second_fragment_label">Second Fragment</string>
    <string name="next">Next</string>
    <string name="previous">Previous</string>

    <string name="hello_first_fragment">Hello first fragment</string>
    <string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
    <string name="add">相加</string>
    <string name="server_name">hello_world_service</string>
	<string name="init">初始化</string>
</resources>
  • src 中 MainActivity.java 文件
package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.app.HelloWorldServiceManger;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tvResult;
    private int result;
    private boolean isInit;
    public static final String TAG = "hello_world_activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult = findViewById(R.id.tv_result);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                HelloWorldServiceManger service = (HelloWorldServiceManger) getSystemService(getString(R.string.server_name));
                isInit = service.init();
                if (isInit) {
                    Log.d(TAG, "onClick: 成功");
                } else {
                    Log.d(TAG, "onClick: 失败");
                }
            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HelloWorldServiceManger service = (HelloWorldServiceManger) getSystemService(getString(R.string.server_name));
                result = service.addVal(3, 4);
                tvResult.setText("addVal" + result);
            }
        });


    }
}
  • 然后编译 HelloWorld 文件夹

mmm packages/apps/HelloWorld/

  • 编译成功后会在 Z:\itop-3399_8.1\out\target\product\rk3399_mid\system\app\HelloWorld 路径下生成 HelloWorld.apk 文件
  • 生成system.img 镜像 将 HelloWorld.apk 打包进入

make snod

系统编译的HelloWorld.app 源码下载

将 system.img 刷入系统

  • 刷完后打开 名字为 my_application 的app
    app
  • 打开cmd ,看Android 的log日志

adb logcat

  • 点击初始化, 看到如下日志, hal 初始化成功
  • 点击相加按钮 看到如下日志,app 调用 hal 成功
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。那么我来回答你的问题: 首先,HAL(Hardware Abstraction Layer)层是Android系统中的一层抽象,它为上层提供了一组标准的接口,使得上层代码不需要关心具体硬件的实现,从而提高了代码的可移植性和复用性。 而JNI(Java Native Interface)是Java语言调用本地方法的一种方式。通过JNI,我们可以在Java代码中调用C/C++代码,而C/C++代码可以访问系统底层的硬件资源。 因此,你的问题可以分为三个部分: 1. 自定义实现一个HAL层接口 2. 通过JNI层回调给Framework层 3. 最后给APP调用使用 下面我分别给出每个部分的实现思路: 1. 自定义实现一个HAL层接口 首先,你需要明确你要实现HAL层接口的功能。然后,你可以根据硬件的实现,编写对应的C/C++代码。在代码中,你需要实现HAL层接口的函数,并且将这些函数封装在一个动态链接库中。通常情况下,你需要使用Android NDK来编译生成这个动态链接库。 2. 通过JNI层回调给Framework层 接下来,你需要在JNI层编写代码,将HAL层接口封装成Java方法。具体而言,你需要使用JNI提供的函数,将Java方法映射到C/C++函数上。然后,在C/C++函数中,你可以通过调用HAL层接口的函数,来实现相应的功能。最后,你需要将JNI方法封装成动态链接库,并且在Android应用中加载这个动态链接库。 3. 最后给APP调用使用 最后,你可以在Android应用中调用JNI方法,从而实现HAL层接口的调用。具体而言,你需要在Java代码中使用System.loadLibrary()方法,来加载JNI动态链接库。然后,你可以通过JNI方法调用HAL层接口,来实现相应的功能。最终,你可以将这些功能封装成Android API,供其他应用调用。 总结: 通过上述步骤,你可以自定义实现一个HAL层接口,并且通过JNI层回调给Framework层,最后给APP调用使用。当然,这只是一个大致的实现思路,具体实现还需要根据具体的硬件和应用场景进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值