Android中aidl如何import文件

如果你做Android开发,那就应该会知道aidl工具的,但是会使用aidl命令行工具的人有吗有吗有吗,如果有的话你们为神马不出来写写怎么用!!!!

用aidl命令行的如果不出意外肯定会遇见类似这样的错误

$ aidl IRemoteServiceCallback.aidl 
IRemoteServiceCallback.aidl:19: couldn'
t find import for class android.location.Location

遇到错误,自然要先看下aidl的帮助是怎么写的

$ aidl
usage: aidl OPTIONS INPUT [OUTPUT]
       aidl --preprocess OUTPUT INPUT...

OPTIONS:
   -I<
DIR>
    search path for
 import statements.
   -d<
FILE>
   generate dependency file.
   -p<
FILE>
   file created by --preprocess to import.
   -o<
FOLDER>
 base output folder for
 generated files.
   -b         fail when trying to compile a parcelable.

看到如此简洁不明了的介绍你有何感想?

多写一句会死啊!!!我讨厌这种假人,以为开发Android的人理解能力都很强吗,不仅help此般不负责任,tutorial也是如此,只有一句

Add the .aidl file to your makefile - (the ADT Plugin for Eclipse manages this for you). Android includes the compiler, called AIDL, in the tools/ directory.

其实这个介绍更废物,它不告诉你是哪个tools,我想当然的以为是sdk下的tools文件夹,怎么都找不到aidl的所在

难道所有的人都在用Eclipse开发Android吗,我去google了一下,问这个问题的大有人在,Google为什么也不修正一下文档。

google在批判apple时说

如果 Google 不行动起来,那我们都将面临一个残酷的未来:一个人、一家公司、一款设备、一个运营商将是我们唯一的选择。

而现在google做的呢,它虽然没有强迫我们只用Eclipse,但它实际上就是这么做的,如同Adobe非强制的要求你用Flash来开发ActionScript一样

牢骚完了,让我们来一步步解决这个猪血问题


当文档不能帮你的时候,看Android代码吧。如何获取代码,Google的文档写的很清楚,其实Google大部分的文档都还是够明了的TAT

以下ANDROID 代表android代码树的根

ANDROID/frameworks/base/tools/aidl 里是aidl工具的C++代码,我没从中获得有用信息。或许很可能是我对C++代码不太有感。。。

一炮打不响就换一个洞,既然Eclipse可以处理aidl文件,我就来上Eclipse的Android插件ADT吧。aidl相关的文件是这个

ANDROID/sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/PreCompilerBuilder.java

你问我如何找到的吗?Linux上的经典工具要经常用才能算是经典

$ grep -ri aidl Android/android-source/sdk/eclipse/plugins/com.android.ide.eclipse.adt/

880行有一个private boolean handleAidl 函数,看名字就是这地方了。一路摸下去,直到890行

command[index++
] = "
-p"

 +
 Sdk
.getCurrent().getTarget(getProject()).getPath( //
$NON-NLS-1$

                IAndroidTarget
.ANDROID_AIDL
);

看到吗,在这儿发现了-p参数,但还不知道它是什么,顺着此条代码追踪,我们找到了这样一个文件

ANDROID/sdk/sdkmanager/libs/sdklib/src/com/android/sdklib/IAndroidTarget.java

现在找到30行,我们终于知道,原来这个-p所指的file created by --preprocess to import 就是一个名叫framework.aidl 的文件

/**
 OS Path to the "framework.aidl" file. */


    public
 final
 static
 int
 ANDROID_AIDL
        = 2
;

哈哈,让我来验一验你的真身!!!

$ find Android/android-sdk-linux_86/ -name framework.aidl
Android/android-sdk-linux_86/platforms/android-3/framework.aidl
Android/android-sdk-linux_86/platforms/android-4/framework.aidl
Android/android-sdk-linux_86/platforms/android-6/framework.aidl
...

这个framework.aidl 文件也很脑残,虽说注释不是很重要,但是这个代码根本就没有半点自我解释能力,也不写个注释说明自己有什么用途--

到此,-p参数确认

革命远未结束,要想解决这个猪血问题,还要知道-I参数的写法。

继续看上面PreCompilerBuilder.java 文件的896行

for
 (IPath
 p : sourceFolders) {
  IFolder
 f = wsRoot.getFolder(p);
  command[index++
] = "
-I"

 +
 f.getLocation().toOSString(); //
$NON-NLS-1$

}

我想你能看出这是什么意思来了,就是你项目的source目录,一般来说是src那个目录

至此,问题解决,最後总结下使用aidl命令行工具的方法就是

$ aidl -I/path/to/project/src -p/path/to/framework.aidl Interface.aidl

谢谢国家,打完收工

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Android AIDL实例: 1. 在你的Android Studio项目,创建一个新的模块,并选择“Android Library”类型。 2. 在该模块,创建一个名为“IMyAidlInterface”的AIDL接口文件,并在其定义要使用的方法。例如: ``` // IMyAidlInterface.aidl package com.example.myaidl; // Declare any non-default types here with import statements interface IMyAidlInterface { int add(int a, int b); } ``` 3. 在同一模块,创建一个实现AIDL接口的类。例如: ``` // MyAidlInterfaceImpl.java package com.example.myaidl; import android.os.RemoteException; public class MyAidlInterfaceImpl extends IMyAidlInterface.Stub { @Override public int add(int a, int b) throws RemoteException { return a + b; } } ``` 4. 在你的应用程序,创建一个Service,并在其使用上面定义的AIDL接口和实现类。例如: ``` // MyService.java package com.example.myaidl; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { private MyAidlInterfaceImpl myAidlInterfaceImpl; @Override public void onCreate() { super.onCreate(); myAidlInterfaceImpl = new MyAidlInterfaceImpl(); } @Override public IBinder onBind(Intent intent) { return myAidlInterfaceImpl; } } ``` 5. 在你的应用程序,通过bindService()方法绑定MyService,并使用IMyAidlInterface接口的方法。例如: ``` // MainActivity.java package com.example.myaidl; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private IMyAidlInterface myAidlInterface; private boolean isBound = false; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { myAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); isBound = true; } @Override public void onServiceDisconnected(ComponentName componentName) { myAidlInterface = null; isBound = false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button addButton = findViewById(R.id.add_button); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isBound) { try { int a = 1; int b = 2; int result = myAidlInterface.add(a, b); Log.d(TAG, "Result: " + result); } catch (RemoteException e) { e.printStackTrace(); } } } }); } @Override protected void onStart() { super.onStart(); Intent intent = new Intent(this, MyService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); if (isBound) { unbindService(connection); isBound = false; } } } ``` 这就是一个简单的Android AIDL实例,它演示了如何在应用程序使用AIDL接口。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值