Help adding a library file for Sphero

453 views
Skip to first unread message

Gareth Haylings

unread,
Sep 29, 2014, 5:18:36 AM9/29/14
to app-inventor-o...@googlegroups.com

Hi

At the open source hangout on Saturday I demonstrated the new Sphero component written for AI and mentioned I am having difficultly adding one of library files required to the make the Sphero component working. I have a work around but it is not ideal would really appreciate any help to solve the issue and I am having.

The 2 file library files required for Sphero are:

  • lib\RobotLibrary.jar
  • lib\armeabi\libachievement_manager.so

I have been able to include RobotLibrary.jar without any issues but haven't been able to add libachievement_manager.so file successful in the AI source code.  The file libachievement_manager.so is used to check Sphero status when connection is established. Without this library file the app connects to sphero but then crashes. The missing file has to be located in the folder  lib\armeabi\ at build time. 

Currently I have a work around to add the missing libachievement_manager.so file outside of the AI Source code by doing the following steps using apktool

  1. I build the companion app as standard use the command build command
    ant PlayApp

  2. I then decompile companion app using apktool with the following command
    apktool d "C:\AI\appinventor-sources\appinventor\build\buildserver\mrAI Companion.apk"

    Below shows the folder structure when the companion app is decompiled



  3. Copy the missing libachievement_manager.so file required for sphero in to the correct folder needed
    cd c:\apktool
    XCOPY .\CompanionFiles\* ".\mrAI Companion\" /s /

    Below shows the folder structure with the missing library file is added



  4. I then rebuild the companion apk
    apktool b ".\mrAI Companion"

  5.  Next sign the companion apk
    cd c:\apktool\Signapk
    java -jar signapk.jar certificate.pem key.pk8 "C:\apktool\mrAI Companion\dist\mrAI Companion.apk" 
    "mrAI Companion.apk"

  6. Finally I install the new companion apk on a connected android device
    adb install -r "mrAI Companion.apk"

I use the same technique when building project Apk’s for sphero.

Due to no other component in AI using extra library files that are required to be place in lib\armeabi I have nothing to reference to.  In the AI Source code I have found roughly where this file should be added  (as shown below) but am unsure are how to add the file the correctly

appinventor-sources / appinventor / buildserver / src / com / google / appinventor / buildserver / Compiler.java

private boolean insertNativeLibraries(File buildDir){

 

    libsDir
= createDirectory(buildDir, LIBS_DIR_NAME);

   
File apkLibDir = createDirectory(libsDir, APK_LIB_DIR_NAME); // This dir will be copied to apk.

   
File armeabiDir = createDirectory(apkLibDir, ARMEABI_DIR_NAME);

   
File armeabiV7aDir = createDirectory(apkLibDir, ARMEABI_V7A_DIR_NAME);

   
/*

     * Native libraries are targeted for particular processor architectures.

     * Here, non-default architectures (ARMv5TE is default) are identified with suffixes

     * before being placed in the appropriate directory with their suffix removed.

     */


   
try {

     
for (String library : nativeLibrariesNeeded) {

       
if (library.endsWith(ARMEABI_V7A_SUFFIX)) { // Remove suffix and copy.

          library
= library.substring(0, library.length() - ARMEABI_V7A_SUFFIX.length());

         
Files.copy(new File(getResource(RUNTIME_FILES_DIR + ARMEABI_V7A_DIRECTORY +

             
File.separator + library)), new File(armeabiV7aDir, library));

       
} else {

         
Files.copy(new File(getResource(RUNTIME_FILES_DIR + library)),

             
new File(armeabiDir, library));

       
}

     
}

     
return true;

   
} catch (IOException e) {

      e
.printStackTrace();

      userErrors
.print(String.format(ERROR_IN_STAGE, "Native Code"));

     
return false;

   
}

 
}

Line 1065

 


Also I think the lib\armeabi\libachievement_manager.so file would need to be added to the following biulld files as I have already done this for the lib\RobotLibrary.jar file

  • appinventor/build.xml
  • appinventor/components/build.xml
  • appinventor/buildserver/build.xml


Decompiling and recompiling apk files outside of AI isn’t really the route I want to take as it is messy even though it works. I would really appreciate any help adding the missing lib\armeabi\libachievement_manager.so correctly via the AI Source code.  If you need any more information let know.


Thank you in advance of any help.
Gareth

Gareth Haylings

unread,
Sep 29, 2014, 3:39:01 PM9/29/14
to app-inventor-o...@googlegroups.com
Hi

I uploaded a cut down version of my mrSpero component to github here is the  link https://github.com/themadrobot/appinventor-sources/commit/1831e4943651fb551aca9641a4d1489496f28085

One question asked at the Hangout on Saturday was will Sphero work with android 1.5 . I just tested it with this level of and can confirm it does work without any issues.

Mark Friedman

unread,
Sep 29, 2014, 4:51:50 PM9/29/14
to App Inventor Open Source Development
I believe that you just need to preface your component class with the @UsesNativeLibraries annotation, like so:

@UsesNativeLibraries(libraries="libachievement_manager.so")
public class MyComponent extends ...

If you had more libraries to load you would seperate them by commas within the string.  If you needed to also have libraries in the "armeabi-v7a" directory you could also add a "v7aLibraries = ..." in the annotation.  See here for an example of the use of @UsesNativeLibraries.

-Mark

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-so...@googlegroups.com.
To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.

Jeffrey Schiller

unread,
Sep 30, 2014, 12:15:15 PM9/30/14
to app-inventor-o...@googlegroups.com
I concur with Mark, you need to add the @UsesNativeLibraries annotation.

-Jeff

--
_______________________________________________________________________
Jeffrey I. Schiller
Information Systems and Technology – MIT App Inventor
Massachusetts Institute of Technology
77 Massachusetts Avenue  Room E17-110A, 32-392
Cambridge, MA 02139-4307
617.910.0259 - Voice
_______________________________________________________________________

Gareth Haylings

unread,
Sep 30, 2014, 2:50:59 PM9/30/14
to app-inventor-o...@googlegroups.com
Hi Mark/Jeff

Thanks for the help made the change but I get a compile error. 

Here is a link to my update git with the @UsesNativeLibraries(libraries="libachievement_manager.so") as you mentioned


Below is the error I get when compiling:

     [java] INFO: The current progress is 10%
     
[java] Android Manifest: including <receiver> tag
     
[java] Sep 30, 2014 7:34:59 PM com.google.appinventor.buildserver.Compiler
generatePermissions
     
[java] INFO: usesLocation = False
     
[java] Sep 30, 2014 7:34:59 PM com.google.appinventor.buildserver.Compiler
setProgress
     
[java] INFO: The current progress is 15%
     
[java] Sep 30, 2014 7:34:59 PM com.google.appinventor.buildserver.Compiler
writeAndroidManifest
     
[java] INFO: VCode: 222
     
[java] Sep 30, 2014 7:34:59 PM com.google.appinventor.buildserver.Compiler
writeAndroidManifest
     
[java] INFO: VName: 2.22ai2
     
[java] Sep 30, 2014 7:34:59 PM com.google.appinventor.buildserver.Compiler
setProgress
     
[java] INFO: The current progress is 20%
     
[java] java.lang.NullPointerException
     
[java]     at com.google.common.base.Preconditions.checkNotNull(Preconditio
ns
.java:191)
     
[java]     at com.google.common.io.Resources$UrlByteSource.<init>(Resources
.java:77)
     
[java]     at com.google.common.io.Resources$UrlByteSource.<init>(Resources
.java:72)
     
[java]     at com.google.common.io.Resources.asByteSource(Resources.java:66
)
     
[java]     at com.google.common.io.Resources.newInputStreamSupplier(Resourc
es
.java:57)
     
[java]     at com.google.appinventor.buildserver.Compiler.getResource(Compi
ler
.java:1142)
     
[java]     at com.google.appinventor.buildserver.Compiler.insertNativeLibra
ries
(Compiler.java:1080)
     
[java]     at com.google.appinventor.buildserver.Compiler.compile(Compiler.
java
:548)
     
[java]     at com.google.appinventor.buildserver.ProjectBuilder.build(Proje
ctBuilder
.java:163)
     
[java]     at com.google.appinventor.buildserver.Main.main(Main.java:81)


BUILD FAILED
C
:\AI\appinventor-sources\appinventor\build.xml:46: The following error occurred
 
while executing this line:
C
:\AI\appinventor-sources\appinventor\buildserver\build.xml:161: Java returned:
1


Total time: 26 seconds


If comment out the @UsesNativeLibraries(libraries="libachievement_manager.so") and compile again it builds successful. Not sure what I am doing wrong. Am I define the stuff in the 3 build.xml files correctly as in the example you gave those files are not shown?



To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.

Jeffrey Schiller

unread,
Oct 1, 2014, 9:10:39 AM10/1/14
to app-inventor-o...@googlegroups.com

I'll poke at it when I get a chance. The UsesNativeLibraries annotation is new and not currently used elsewhere so it may well be broken.

-Jeff

P. S. You are welcome to debug it as well.

Gareth Haylings

unread,
Oct 1, 2014, 9:59:17 AM10/1/14
to app-inventor-o...@googlegroups.com

I will see if a can figure out if it is something I'm doing of if @UsesNativeLibraries is broken , but if you do have a chance to look at this that work be great Jeff.
I noticed another annotation which I don't think is used anywhere else @UsesAssets (I might be wrong)

In the example Mark supplied only the Instrument.java is available so it's hard to work out if I have missed or incorrectly defined something in another file for the @UsesNativeLibraries to work. One thing that would really help to understand this properly and check I'm defining everything correctly would be to have access to the whole of Trevor B. Adams the code. Is this possible?

Thanks for the help so Mark & Jeff

Mark Friedman

unread,
Oct 1, 2014, 1:14:42 PM10/1/14
to App Inventor Open Source Development
I've sent a message to Trevor to see if he would be willing to help.

-Mark

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-so...@googlegroups.com.

Gareth Haylings

unread,
Oct 1, 2014, 2:30:32 PM10/1/14
to app-inventor-o...@googlegroups.com
Thanks Mark 



On Wednesday, 1 October 2014 18:14:42 UTC+1, Mark Friedman wrote:
I've sent a message to Trevor to see if he would be willing to help.

-Mark
On Wed, Oct 1, 2014 at 6:59 AM, Gareth Haylings <gareth....@googlemail.com> wrote:

I will see if a can figure out if it is something I'm doing of if @UsesNativeLibraries is broken , but if you do have a chance to look at this that work be great Jeff.
I noticed another annotation which I don't think is used anywhere else @UsesAssets (I might be wrong)

In the example Mark supplied only the Instrument.java is available so it's hard to work out if I have missed or incorrectly defined something in another file for the @UsesNativeLibraries to work. One thing that would really help to understand this properly and check I'm defining everything correctly would be to have access to the whole of Trevor B. Adams the code. Is this possible?

Thanks for the help so Mark & Jeff


On Wednesday, 1 October 2014 14:10:39 UTC+1, j...@mit.edu wrote:

I'll poke at it when I get a chance. The UsesNativeLibraries annotation is new and not currently used elsewhere so it may well be broken.

-Jeff

P. S. You are welcome to debug it as well.

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

Jeffrey Schiller

unread,
Oct 1, 2014, 11:52:39 PM10/1/14
to app-inventor-o...@googlegroups.com
UsesNativeLibraries is definitely broken. I'm working on fixing it. You can see my "Work in Progress" (read: still doesn't quite work) at:


I'm too tired to continue tonight... but I'll keep plugging at it.

-Jeff

Message has been deleted

Gareth Haylings

unread,
Oct 2, 2014, 4:58:36 AM10/2/14
to app-inventor-o...@googlegroups.com
I deleted my previous post as I have updated my Git in line with the mods you made Jeff other than the mods to the  appinventor/components/src/com/google/appinventor/components/scripts/ComponentProcessor.java file. Here is a link to my new code


Still fails to build but I have added both native Libraries. Looking at the code I think the name I have given the  libachievement_manager.so-v7a  for the \libs\armeabi-v7a is right. Here are the 2 native library files

libachievement_manager.so  is for  \libs\armeabi
libachievement_manager.so-v7a is for \libs\armeabi-v7a


Here is a screenshot of the buildserver folder after building which looks promising







































Will carry on looking at this  tonight  when I get home from work.




On Thursday, 2 October 2014 09:14:49 UTC+1, Gareth Haylings wrote:
Thanks for looking at this Jeff

I have updated my Git


Still fails to build but I have added both native Libraries. Looking at the code I think the name I have given the  libachievement_manager.so-v7a  for the \libs\armeabi-v7a is right. Here are the 2 native library files

libachievement_manager.so  is for  \libs\armeabi
libachievement_manager.so-v7a is for \libs\armeabi-v7a

Will carry on looking at this  tonight  when I get home from work.

Gareth Haylings

unread,
Oct 5, 2014, 3:09:20 PM10/5/14
to app-inventor-o...@googlegroups.com
Getting further now. I have debugged the code so it is completing the build and I am able to write the  \libs\armeabi\libachievement_manager.so and \libs\armeabi-v7a\libachievement_manager.so-v7a to the temp build location but the file and not being pacakged into the APK. 

Here is a link to my latest git with my amendments


I think the reason the native Libraries are now not being compiled into the APK is because the file appinventor\buildserver\src\com\google\appinventor\buildserver\Compiler.java the code package the native libraries is missing. Below show the current code in the Compiler.java to package the APK

 String[] aaptPackageCommandLine = {
     getResource
(aaptTool),
     
"package",
     
"-v",
     
"-f",
     
"-M", manifestFile.getAbsolutePath(),
     
"-S", resDir.getAbsolutePath(),
     
"-A", project.getAssetsDirectory().getAbsolutePath(),
     
"-I", getResource(ANDROID_RUNTIME),
     
"-F", tmpPackageName,
 
};

Not sure if the missing native library can be added to the above code but if not I think it can be done with something like the following but will have to work this out:

aapt.exe a "MIT AI2 Companion.apk" lib/armeabi /libachievement_manager.so
aapt
.exe a "MIT AI2 Companion.apk" lib/armeabi-v7a /libachievement_manager.so


Any thoughts on this?



   

Gareth Haylings

unread,
Oct 8, 2014, 4:16:15 AM10/8/14
to app-inventor-o...@googlegroups.com
Hi

Great news. I have now managed to add the missing native libraries. Trevor Adams shared his code with me yesterday and even though is was completely different to the current MIT AI source I discovered where the issue is in the MIT AI Source code. It is one statement highlighted in red below:

 String[] aaptPackageCommandLine = {
        getResource(aaptTool),
        "package",
        "-v",
        "-f",
        "-M", manifestFile.getAbsolutePath(),
        "-S", resDir.getAbsolutePath(),
        "-A", project.getAssetsDirectory().getAbsolutePath(),
        "-I", getResource(ANDROID_RUNTIME),
        "-F", tmpPackageName,
        libsDir.getAbsolutePath()
    };


I've been struggling with this for a few weeks. Can't believe it was just one missing line of code. I'll rebase my code make the amendment thoroughly test it the code then put in a pull request. What I will also do is put together a short document that explains how to use the @UsesNativeLibraries and @UsesAssets annotations as this isn't currently documented in the AI Source Development guide

Thanks you all for the help on this issue.
Gareth
Message has been deleted

Gareth Haylings

unread,
Oct 9, 2014, 4:03:26 AM10/9/14
to app-inventor-o...@googlegroups.com
Hi 

I re-did the pull request as the original pull request I put in late last night had a number of my other commits. 
Here is the corrected pull request  with only the fix for the native library https://github.com/mit-cml/appinventor-sources/pull/137

In the comments of the pull request is an example detailing how to add native library files to the AI code. It might be worth adding this info to the AI developers guide in the section of Annotations if the pull request is accepted.



On Wednesday, 8 October 2014 22:14:09 UTC+1, Gareth Haylings wrote:

Jos Flores

unread,
Oct 9, 2014, 9:52:04 AM10/9/14
to app-inventor-open-source-dev
Thanks, Gareth. This is great. We'll review it shortly!


cheers,
José


--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-so...@googlegroups.com.

sunxuc...@163.com

unread,
Dec 19, 2016, 5:51:16 AM12/19/16
to App Inventor Open Source Development

Hello!    

I have read your comments carefully.But I do not know the rules to create the ***.so file and the way to use the function in it .I have written a **.so file with NDK support.The extension component  file can be built successfully.But When I use it to built the app.It dose not work.Can you give me some tips on how to create the ***.so file and the way to use the fuction in it?

Thank you in advance!

The attachment is extension source code .The figures below are the .so file source code and the result








在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.


在 2014年9月30日星期二 UTC+8上午4:51:50,Mark Friedman写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.
extensions.txt

sunxuc...@163.com

unread,
Dec 19, 2016, 6:11:20 AM12/19/16
to App Inventor Open Source Development

Hello !
I have read your comment carefully.
 I want to create a native library which is written by C language and then I can use this native library to create a component extension.I have read many posts and tried many times,But I always failed.I know how to include the native library and know how to add the code in build.xml. I want to know the rules to create the Native library(*.so file) and the way to use it. Can you give me some tips or an article that I can refer to.
 
Thank you  a lot ! Chen.

在 2014年10月1日星期三 UTC+8上午12:15:15,j...@mit.edu写道:
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.

To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at http://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages