search_img
Sign in
search

Intergrating External Jars (i.e, Drools Jars ) to Android and Developing Android Programs:

 
print Print email Email
I.T Analyst
Intergrating External Jars (i.e, Drools Jars ) to Android and Developing Android Programs:

 

Hello I am newbie to android Programming.


My Tasks with ANDROID are: 

Developing the integration of external jars (drools jars) into the android

(OR)

Developing a sample Android Application with Drools (Rules Engine).


So here my first task is to add external jars(i.e, Drools Jars) in android i have implemented in several ways that i am listing outso that it will be useful for you to point out the error and slove the issue.


1. Adding the Drools related Jars into the Android application like general Java Application

2. Adding the Drools pack jars into the android application as class libraries

3. Adding the Drools pack jars into the assets folder provided as part of the android application.

4. Adding the jars repository to the project and providing the folder as reference to android project.

5. Adding the total drools source to the android project.

6. Adding a new drools project and giving reference of this project to android application.

7. Converting run able drools project to android project.

8. Tries to add the maven environment to pack the jar files to the android application using POM.xml.


Result:  Unable to get the classes exist in the drools APIReason: Unable to Pack the drools related jars into the application

Blocker: If developer will adds more than 1 jar to the referenced library the Android application will produce the compiler error, also unable to run the project for a long time.


The Blocker can be solved by creating a jar from multiple jars (ie, drools-api,drools-core,drools-complier etc.,) 


IMP: Here What is the Work Flow of the Android:


Android  allows developers to write managed code in the Java language, controlling the device via Google-developed Java libraries.

The Android operating system software stack consists of Java applications running on a Java based object oriented application framework on top of Java core libraries running on a Dalvik virtual machine featuring JIT compilation. 


Here Every Java file is complied to .class file, this .class file(bytecode) will convert to .dex file using the dx internally by Android SDK.


Dx Converts Java .class bytecode into Android .dex bytecode


The dx tool lets you generate Android bytecode from .class files. The tool converts target files and/or directories to Dalvik executable format (.dex) files, so that they can run in the Android environment.

It can also dump the class files in a human-readable format and run a target unit test. You can get the usage and options for this tool by using dx --help.
-------------------------------------------------------------------------------
So what ever the class files it need to convert to the .dex file so that android can understand.And here comes the problem how to compile the .drl files (Drools resource files) for that i have done the following process.
Here take a new Drools project and write the knowledgebase to a file with a sample.drl file.


Frist i write the knowledgebase object into the file by fileoutputstream as follows:


FileOutputStream fos = new FileOutputStream("C:\\knowledgebase.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos);

DroolsStreamUtils.streamOut(oos,obj);

oos.flush();

oos.close();


By Reading this  C:\\knowledgebase.dat i can execute the drools without loading the .drl files by just having the jars (drools-api, drools-core, mvel ) to execute the rules only 3 jars are enough.


FileInputStream fis = new FileInputStream(new File("C:\\knowledgebase.dat"));

ObjectInputStream ois = new ObjectInputStream(fis);

KnowledgeBase base = (KnowledgeBase)DroolsStreamUtils.streamIn(ois); StatefulKnowledgeSession fullSession = knowBase.newStatefulKnowledgeSession();

Message message = new Message();

message.setMessage("Hello World"); message.setStatus(Message.HELLO);

fullSession.insert(message);

fullSession.fireAllRules();

ois.close();

fis.close();
Here From above code is a different simple java project that is executed fine.


Reason to store object in a file: The compiled drl file is it self a drl this is executed by JVM at runtime so how can  i execute same in Android First  is android can't understand the drl. so, i stored in a file and try to load in android



NOW the above code(Java project code) that i want to load in android project.


As i already mentioned that android cant understand the class file it can read only .dex file so  i manually tried to convert the .dat file to .dex file using dx tool which is located in android sdk dir ("E":\android\platforms\android-8\tools) thru command prompt we can convert. At first set the classpath  ANROID_HOME = E:\android path = E:\android\tools;


While conversion following error occured : CMD: dx --dex --ouput=test.dex knowledgebase.dat


The Exception while converting .dex is as follows:

  trouble processing: bad class file magic (aced0005) or version (0012.7372) ...while parsing knowledgebase.dat ...while processing knowledgebase.dat 1 warning no classfiles specified
If it is a .class file then we can easily convert to .dex file.


Then comes the problem how to load or read the file (knowledgebase.dat) in android.Note = knowledgebase.dat are equal to knowledgebase.class (i changed the extension)


For this i have implemented following ways;

----------------------------------------------------------------

1) In DDMS perspective select FileExplorer tab there will be 3 dirs (data,mnt,system)


Location of files in data dir: (getFilesDir().getAbsolutePath())
    In which data/data/project_dir/files/yourfilename.txt (project_dir = com.vinay)


Location of files in mnt dir (i.e, Sdcard Memory):


/mnt/sdcard/Android/data/project_dir/files/yourfilename.txt


In these locations the files can be inserted manually by (Push a file on to the device button located at the top of DDMS perspective) or from command prompt  

E:\Android\tools>adb push C:/pushingfile.txt

/data/data/project_dir/files/knowledgebase.dat 


After successfull loading of files into the android then read the file in diff ways getting errors :

  Code :  System.err.println(getExternalFilesDir(""));

FileInputStream fis = openFileInput("/mnt/sdcard/Android/data/com.vinay/files/knowledgebase.class");

Exception: Caused by: java.lang.IllegalArgumentException: File /mnt/sdcard/Android/data/com.vinay/files/knowledgebase.class    contains a path separator

 

Code:   Put the file into res/raw/knowledgebase.class Resources res = getResources();     InputStream fis = res.openRawResource(R.raw.knowledgebase);

 

Exception : Caused by: java.lang.UnsupportedOperationException: can't load this type of class file

 

Code:   pushed the file into data dir
java.io.File filepath = new File("/data/data/com.vinay/files/knowledgebase.class");        System.out.println(filepath.isFile());        System.out.println(filepath.getAbsolutePath());        InputStream fis = new FileInputStream(filepath);

 

Exception: Caused by: java.lang.UnsupportedOperationException: can't load this type of class file

 

Code: java.io.File filepath = new File("/knowledgebase.class");        InputStream fis = new FileInputStream(filepath);

 

Exception:
java.io.FileNotFoundException: /knowledgebase.class (No such file or directory)

 


So pls help me from this issue.....!!


Thanks & Regards

Vinay Guntaka

Software Engineer

prevnew
start_blog_img
Sign Up For a Roundup of The Week's Top Bloggers
Email:
Follow SI :
SiliconIndia About Us   |   Contact Us   |   Help   |   Community rules   |   Advertise with us   |   Sitemap   |  
News:       Technology   |   Enterprise   |   Gadgets   |   Startups   |   Finance   |   Business   |   Career   |   Magazine  |   Newsletter   |   News archive  
Cities:        CEO   |     Startup   |   Mobile   |   CIO   |   Women   |   BI   |   HR   |   SME   |   Cloud   |   Marketing   |   QA   |   Java   |   Web Developer  
Community:      Members   |   Blogs   |   Indian Entrepreneurs   |   Gyan   |   Advice   |   Community   |   Find   |   CXO Insights  
Job Board:      Jobs   |   Freshers   |   Companies   |   HR Speak   |   Forum  
Online Courses:   Web Developer   |   Java Developer   |   CCNA Training   |   SEO   |   SAS   |   SQL Server 2005   |   J2EE
Education:   MBA   |   MCA   |   Engineering   |   Training Institute
Life:          Real Estate   |   Travel   |   Finance   |   Gadgets   |   Movie Reviews   |    Jokes  
Send your feedback and help us continue to improve SiliconIndia
© 2014 InfoConnect Web Technologies India Pvt Ltd. all rights reserved