Android aidl cant import - android

I have an aidl file:
package com.xyz;
interface ICallback
{
void CallbackMsg(String type, String value1);
}
I have another aidl file:
package com.xyz;
import com.xyz.ICallback;
interface ISendMsg
{
int getPid();
void SendMsg(String value1);
void registerCallBack(ICallback cb);
void unregisterCallBack(ICallback cb);
}
It gives error as: couldn't find import for class com.xyz.ICallback
What am i doing wrong?

I was copy pasting aidl files from other source and thats why it created a problem.
Once I created the aidl files in the project itself, it stopped giving errors?

Related

Error:(42, 16) error: incompatible types required: Loader<List<String>> found:FileLoader

I'm trying to use the LoaderManager in my code however I'm getting an incompatible types error:
#Override
public Loader<List<String>> onCreateLoader(int id, Bundle args) {
return new FileLoader(MainActivity.this);
}
shouldn't this statement return new FileLoader(MainActivity.this) returns a Loader? And here is the implementation of the LoaderManager:
public class FileLoader extends AsyncTaskLoader<List<String>>{
public FileLoader(Context context){
super(context);
}
#Override
public List<String> loadInBackground() {
.........
........
return listFiles;
}
}
I had the same problem(incompatible types required), but when see your post ,it helps me to learn more.In my code, it was imported:
"import androidx.loader.content.AsyncTaskLoader;"
, so i change it to:
"import android.content.AsyncTaskLoader;"
then my problem solved.
Because "AsyncTaskLoader" : This class was deprecated in API level 28,by below ref.
https://developer.android.com/reference/android/content/AsyncTaskLoader
by this advice,"You should also consider migrating existing projects to AndroidX", the webpage below:
https://developer.android.com/jetpack/androidx/migrate/class-mappings
finally I succeeded to solve the problem ... it was that didn't Import the support library in the AsyncTaskLoader file , while using it at the Main file ...so i just removed
import android.content.AsyncTaskLoader;
and Import the following support library
import android.support.v4.content.AsyncTaskLoader;

Pass android.nfc.Tag via aidl?

I have got a simple aidl interface working:
package local.test;
interface ITest {
int[] getSupportedAidlLevels();
}
However I am struggling to pass the non primitive type android.nfc.Tag which already implements parcelable.
package local.test;
interface ITest {
int[] getSupportedAidlLevels();
void updateNfcTag(Tag tag);
}
I have tried to use an import statement like I would in java. Further I tried to create a second .aidl file like the following but non of this solved my problem.
package local.test;
import android.nfc.Tag;
parcelable Tag;
After a lot of trial and error I finally found the mistake. The in tag was missing.
package local.test;
interface ITest {
int[] getSupportedAidlLevels();
void updateNfcTag(in Tag tag);
}
Maybe this answer helps someone else ;)

Apache Felix - Using a bundle service shows "java.lang.ClassCastException"

I am embedding Apache Felix into an Android application. In that application, I am trying to use a service provided by a bundle. The bundle is successfully installed and started, but at the line that uses the service which is:
System.out.println( ((AndroidService) services [0]).startActivity());
I get the following error:
java.lang.ClassCastException: androidapi_bundle.AndroidServiceImpl cannot be cast to com.example.android_services.AndroidService
I have the following in my bundle:
1- Activator class
package androidapi_bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.example.android_services.AndroidService;
import android.util.Log;
public class Activator implements BundleActivator {
private static BundleContext context;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
System.out.println("This is a java message inside the start method of AndroidAPI_Bundle");
Log.d("Zitona Log","This is android message inside the start method of AndroidAPI_Bundle");
context.registerService(
AndroidService.class.getName(), new AndroidServiceImpl(),null);
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
Log.d("Zitona Log","AndroidAPI_Bundle stopped!");
}
}
2- com.example.android_services which has AndroidService.java interface:
package com.example.android_services;
public interface AndroidService {
public String startActivity();
}
3- its implementation class AndroidServiceImpl.java :
package androidapi_bundle;
import com.example.android_services.*;
public class AndroidServiceImpl implements AndroidService {
#Override
public String startActivity()
{
return "Activity started";
}
}
Now, in my android app I also have AndroidService.java interface, and the following is the code that uses the service:
#SuppressWarnings({ "rawtypes", "unchecked" })
ServiceTracker m_tracker = new ServiceTracker(
m_activator.getContext(),AndroidService.class.getName(), null);
m_tracker.open();
System.out.println("8");
Object[] services = m_tracker.getServices();
System.out.println("9");
System.out.println( ((AndroidService) services [0]).startActivity());
System.out.println("10");
After "9" is displayed I get the error. Where did I go wrong?
Below is my bundle MANIFEST:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: AndroidAPI_Bundle
Bundle-SymbolicName: AndroidAPI_Bundle
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: androidapi_bundle.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0", android.util, com.example.android_services
Export-Package: com.example.android_services
Update:
I realized that I have this line of code:
m_configMap.put(FelixConstants.FRAMEWORK_SYSTEMCAPABILITIES_EXTRA, "com.example.android_services");
so I changed it to:
m_configMap.put(FelixConstants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "com.example.android_services");
Now I am getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apache_felix_android/com.example.apache_felix_android.MainActivity}: java.lang.NullPointerException
The only way you can get this is if both the activator bundle and the app bundle have an internal copy of the service class and neither exports it. So you will need to show your manifests and bundle contents.
Update What on earth are you mucking with advanced options like system packages (and then using Felix classes instead of OSGi's constant class) when you seem to have very basic problems. Why not start with a clean simple bndtools, make your app work in there using an IDE, and then when it works port it to Android? This all looks awfully masochistic way to learn OSGi?

How to access jar file from Unity with different bundle identifier in Unity & package name in Java?

I have created a plugin for unity in java jar.
In Java, my package name is com.android.test.
In Unity, my Bundle Identifier is com.android.test. This is working fine with the code below.
But if I change my Bundle Identifier to com.android.test2 in Unity, the code below throws an exception Class not Found: com.android.test2.myplugin.
Is there any way to access a jar file whose package name is different from Unity Bundle Identifier name?
Here is my unity C# code to access Jar:
static IntPtr cls_Activity;
static IntPtr fid_Activity;
static IntPtr obj_Activity;
static IntPtr cls_OurAppNameActivityClass;
static IntPtr startAdsMethod;
cls_Activity = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
obj_Activity = AndroidJNI.GetStaticObjectField(cls_Activity, fid_Activity);
cls_OurAppNameActivityClass = AndroidJNI.FindClass("com/android/test/myplugin");
startAdsMethod = AndroidJNI.GetMethodID(cls_OurAppNameActivityClass, "Instantiate", "()V");
if (AndroidJNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != false)
{
Debug.Log("Activity IS a OurAppNameActivity");
jvalue[] myArray = new jvalue[1];
AndroidJNI.CallVoidMethod(obj_Activity, startAdsMethod, myArray);
Debug.Log("Activity Leaving a OurAppNameActivity");
}
your package name needs to be same in both unity and in java else they cannot be accessed.
Yes you can.
Follow the below steps
1.While creating android project give a name to your MainActivity
(instead of MainActivity Let us say MYAndroidPlugin)
2.Now in MYAndroidPlugin.java you will see the class MYAndroidPlugin,Modify it as follows`
package com.mydomain.androidplugin;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
public class MyAndroidPlugin {
public MyAndroidPlugin(){
//default constructor
}
public void makeToast(final String message, final int length) {
final Activity a = UnityPlayer.currentActivity;
a.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(a, message, length).show();
}
});
}
3.Export it as jar via gradle task and place jar file in you unityproject/Assets/Plugins/Android
4.in the c# script(in the toast method) directly refer your class
public void TestToast(){
using(AndroidJavaObject jo = new AndroidJavaObject("com.mydomain.androidplugin.MYAndroidPlugin"))
jo.Call ("makeToast", "my plugin worked", 2);
}
5.That's it call this method to make a toast
you can now directly refer a jar of any identifier

Unable to use object from library in AIDL interface

I am creating an AIDL interface that uses an object type from an android library that is part of a different project. I can import and use the type fine in my service, but I can not import it in my AIDL interface.
package com.mysite.service;
import com.othersite.library.MyObject;
interface IMyService {
int getPid();
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
MyObject getObjects();
}
The error is:
couldn't find import for class com.othersite.library.MyObject
You must create a separate .aidl file for each class you wish to use
within your service that declares that class as parcelable.
There is a blog post about this issue here.

Categories

Resources