Android - Using a method from a Library - android

I have setup my own android library and now want to use the methods within..
For some reason I am having issues understanding how this works, looking at other questions/ internet has not helped.
In my library class i have the following.
public void testMethod(){
Toast s = Toast.makeText(this, "test!", Toast.LENGTH_SHORT).show();
}
I want to be able to reference this in my main for example, how do i do this?
I have found several links which do look at this however it has not helped as it is different to what I want to do. Am I being very stupid and missing something?
I am guessing an interface needs to be setup on my Main Class? I am not sure.
By the way I have already setup the library and have referenced in manifest etc..

You should import to your project the package name you used to create your library, then, create an object of the class you want to use from your library, and finally, call the method.
Supose your library package name it's: "com.mylibrary"
Your library class it's called: "MyClass"
Your method it's called: "MyMethod"
In Your Project you should do:
//Import Your Library Package
import com.mylibrary;
//Instantiate The Library Class
MyClass mytest = new MyClass();
//Call The Library Class Method You Want To Use.
mytest.MyMethod();

Related

How to use a class in an imported jar?

What I am doing: Call the method inside the class in a very simple jar from main Activity. (The jar will be used as sdk to make connection with a server but right now is for testing)
Error message
This is all the code in my jar, just 1 class
I have tried this, and changed the class into singleton pattern and this, and make everything in the class public. I also found this one but same error different issue
Create class as public class to make it accessible. In java if you don't provide any access modifier it becomes default.
Refer Java Access Modifiers here
public class Jartest{
//Your Implementation
}
In addition to create the class with public modifier, all classes must be warped inside a package. Otherwise it becomes default package and there is no way to access it beside reflection. In another word, when you create the jar, create the class inside a package and Android studio should be able to import the package and use the class inside the jar.

Compiling Java code under Android in runtime

I have a class name String and a String which containing the class code. For example, "Example" is the name of the class, and
public class Example {
public void example () {System.out.println ("Hello world!"); }
}
The class code.
I looked at the Dexmaker library, but I did not understand if it's possible to compile the generated code into it. And the question is just how to compile the code string under Android?
Not sure if possible at all the compilation within the embedded system but definitelly you can parse and run the code using beanshell:
http://www.beanshell.org/
it is lightweight and easily to embed in your app. Then you can instance the generated class and run whatever you put inside.
There is only one true way: using DexMaker. All examples you can find on DexMaker wiki and especially for current problem (runtime generation code on android).

how to import class of another package from different project in AIDL file?

I have a AIDL file that implemented in the package under ProjectA, and I am trying to import a Parcelable class (Foo) from another package under ProjectB. Below is the way how I implemented the MyService.AIDL file:
package com.packageA.projectA
import com.packageB.projectB.Foo
interface MyService{
void getSomething(Foo foo);
}
However, I get this compilation error "couldn't find import for class com.example.projectB.Foo". If I copied the packageB to packageA, then I will get no compilation error.
Is there a way to import parcelable class from package under different project? I know there're multiple questions on stackoverflow and elsewhere (like google group) about importing parcelable under the same project, but none from different projects. Thank you for your time.
You mean to say that you defined the class for the parcelable class and you are not able to use that class in the aidl ?
Try the below solution.
you have your MyService.AIDL in your src/xxx path.
Now create Foo.aidl (name should be same)in the same path and define that Foo.aidl as below.
package com.packageB.projectB
parcelable Foo
now remove the import statement from MyService.AIDL and re-type it (its for refreshing , else it will show same error)
now that import error must be gone.
I know this is old but I had the same problem and found the solution very ugly.
I had two classes defined in the package:
com.lni.codephg.inter
I had another class defined in the package
com.pcha.androidbtmanager
The actual AIDL interfaces were defined in the package
com.pcha.proprietary.handler
The client would be looking for remote services implementing methods in the package com.pcha.proprietary.handler.
So what did my AIDL file hierarchy have to look like to make this work?
src\main\aidl\com\lni\codephg\inter
MetricIntermediary.aidl
MdsIntermediary.aidl
src\main\aidl\com\pcha\androidbtmanager
PhdInformation.aidl
src\main\aidl\com\pcha\proprietary\handler
IConnectionCallback.aidl
IIntermediaryCallback.aidl
IProprietaryDeviceHandler.aidl
IStatusEventCallback.aidl
The 'one-liner' files defining the custom classes like MdsIntermediary.aidl look like this
// MdsIntermediary.aidl
package com.lni.codephg.inter;
parcelable MetricIntermediary;
I have to admit I do understand why these one-liner files must exist in such a weird form.
Then the interface AIDL files that reference them (for example IIntermediaryCallback.aidl) look like this
// IIntermediaryCallback.aidl
package com.pcha.proprietary.handler;
// Declare any non-default types here with import statements
import com.lni.codephg.inter.MdsIntermediary;
import com.lni.codephg.inter.MetricIntermediary;
interface IIntermediaryCallback
{
void onMdsIntermediary(in MdsIntermediary mds);
void onReceiveMetricIntermediaries (in List<MetricIntermediary> metricList, in
MdsIntermediary mds);
}
Of course I had to implement the Parcelable methods on the said custom classes. However, as ugly as that was, Android Studio seemed to do it for me. Since I know nothing about Parcelable I don't know if it is good enough or if I have to do some massaging.
This was painfully difficult. Hope this will save someone hours of frustration.

Reference Application Class from Project Library

I am trying to reference a class that is in my application (MainActivity.class) from my Android project library.
To give an outline of the structure, it appears as so:
ExampleApp (src/com.example.recorder):
--->MainActivity
then inside ExampleLibrary (src/main/src/com.example.library):
---> FragManage
so from within FragManage:
// Get FragManage's tag for reference.
String myTag = getTag();
MainActivity.setTagFragManage(myTag);
but in Android studio I receive the error:
Cannot resolve symbol 'MainActivity'
Is there a way to create a reference to the class from the library? Also, I cannot just hardcode the path to MainActivity as I share this library with a Free version of my app.
I guess you should be referencing the other way round, objects from the library should be referenced from the application.
Anyway, what you could do is to create an interface or a base class in your library and use that in the library code. Then in your main app you could write the implementation of the interface or a child class.

not understanding compile errors in AIDL files

I'm taking my first foray into Android services and having trouble with compile errors in AIDL files. I am using Eclipse (with Android Development Tools) and Android 4.1. I have the following AIDL files:
Weather.aidl
package ws.hamacher.weatherservice.service;
parcelable ws.hamacher.weatherservice.dto.Weather;
In this file, I get "interface ws.hamacher.weatherservice.dto.Weather should be declared in a file called ws\hamacher\weatherservice\service\ws.aidl." on the parcelable line, but this refers to my Java class!
IWeatherService.aidl
package ws.hamacher.weatherservice.service;
import ws.hamacher.weatherservice.service.Weather;
interface IWeatherService {
void addToWeatherService(in Weather weather);
void deleteFromWeatherService(in Weather weather);
List<Weather> getLocations();
}
Here again, the import statement gives a similar error "interface ws.hamacher.weatherservice.dto.Weather should be declared in a file called ws\hamacher\weatherservice\service\ws.aidl." This should be referring to the first file above right?
Along with that, the method declarations all have errors, to the tune of "unknown type Weather".
Any help would be appreciated.
If you want to send custom object such as Weather class, you should creat a package:
ws.hamacher.weatherservice.dto
write :
Weather.java
in package: ws.hamacher.weatherservice.dto like this:
public class Weather implements Parcelable {
....
}
Then, write Weather.aidl:
package ws.hamacher.weatherservice.dto;
parcelable Weather;
Please see AndroidMusicPlayer and AndroidMusicPlayerClient for real code.
Solved it. My Weather.aidl had to be in the same directory as my parcelable Weather class.

Categories

Resources