Build native library and JAR at the same time - android

I am trying to build a JAR file that I can import and use it for other android projects.
I created Android project that contains java code (src/main.java) and it calls bunch of native code I wrote (Using System.loadLibrary(...))
Under jni folder, I have bunch of C/C++ codes, for instance jni/sample/sample.cpp.
My question is that when I export this project to JAR, can I build native library at the same time?
In other words, do I need to have pre-built .so file before exporting to JAR file?
My goal is when I export or something like that, it will do:
Build C/C++ code and create .so files --> Build .java ---> JAR
at once.

If you are using Eclipse, you can create a "Launch Group" in your debug/run configurations. That way you can include your Java build and an Android Native Application build in one configuration.
If you are looking to move to a more sophisticated build system, you should look into Maven. It is considerably more complex than the plain old debug/run configurations, but it is much more powerful. It includes an apklib packaging to build libraries for Android. There are a few quirks with using the NDK in an apklib, but reasonable project design can avoid most problems.

Related

AOSP Build System Library (jar) as pure JAR. Not dexed

I have a Java Library project in my AOSP build which creates a jar file and hosts it in system/framework
Everything works as expected, however I now wish to create a JAR file from the same library to allow and application to link and compile against it. Goal is to be able to create APKs against this lib (compileOnly) so that the APK can run and use the library on the ROM.
The problem is that Jack seems to be generating only dexed jar files which cannot be used in Android Studio as libraries.
Is there a way to disable DEXing in the Android.mk ?
I know there is an option in Android.bp (cannot recall it) which gives the option for the build to generate normal jar (with .class files).
Note: I cannot use BUILD_DROIDDOC with stubs due to enviroment issues (custom ROM) assuming BUILD_DROIDDOC is only executed with lunch sdk-eng
This is for a custom ROM with Android 8.1 base. I think the custom option of not dexing in Android.dp file is not supported in this version anyway.

Don't want to manually copy android JNI(.so) and Jar libraries to App project each time

I'm building and Eclipse to make an Android library I want to distribute to developers.
TedLibJni:
It uses the Android NDK and so it compiles down to a .so file.
TedLibJar:
It also has a Java interface that binds the then extern'd calls in the JNI, so it has a Jar library associated with it.
TedDroidApp:
The concensus is that I need to manually copy both TedLibJni.so and TedLibJar.jar to lib/armeabi of this App for it to be used.
Question: Is there any way that TedDroidApp can pick up the externally located .so or .jar files? It seems crazy that I would have to manually copy and paste these files accross each time I iterate them.
Use an Android library project for the JNI code and the JAR. You can then attach the Android library project to other projects. With the new Gradle-based build system, you can package the library project up as an AAR and obtain it from an artifact repository as well.
CWAC-AndDown, my wrapper around the C hoedown library, works this way.

android native lib not installed with APK

I've made an android library project that uses some native libraries.
I've made the jni wrapper and put the native libs (.so) in the libs// folders. The native libs are compiled using cmake, for armeabi, armeabi-v7a, x86 and mips.
I export this project to a jar and put this jar into a "normal" android project. I then export this project to an apk and can see that my libs are bundles into it.
However, when i install the apk, the libs corresponding to the device are not copied into /data/data/com.my.app/lib and obviously, running the app complains about not finding the libs (UnsatisfiedLinkError).
I've search through SO and everywhere i can but found no answer that solved my case.
i'm using Eclipse, btw
Thanks for your help
UPDATE
OK, i've read the doc in the ndk and seen the examples, and unfortunately, i can't see the solution.
The ndk build the c code into shared libs and places them into the appropriated location in the project. But it doesn't generate anything that says that the libs must be installed with the apk.
My goal is to provide an android library (so a jar), that can be included within an android application. I don't see the real difference between what i'm doing (compile the c libs using cmake and package the jni and java compiled classes into a jar) and what is done with android.mk
If you see what i'm missing, feel free to tell me (even if its obvious).
thanks
UPDATE
i've made a dirty hack: in the libs folder of my application, i've put the jar file containing my classes and my native libs and a copy of the .so files for each arch. Suprise, the libs are no installed in /data/data/com.me.myapp/lib
It seems to confirm that it's a packaging problem.
I export this project to a jar and put this jar into a "normal"
android project. I then export this project to an apk and can see that
my libs are bundles into it.
The issue is that the Android packaging system doesn't handle with binary assets in JARs. For your application project to find and include the generated .so files, you need it to reference the library project as an 'Android library project':
Did you call ndk-build command?
See description below for details.
http://developer.android.com/tools/sdk/ndk/index.html
You can build the shared libraries for the sample apps by going into /samples// then calling the ndk-build command. The generated shared libraries will be located under /samples//libs/armeabi/ for (ARMv5TE machine code) and/or /samples//libs/armeabi-v7a/ for (ARMv7 machine code).

Quick and dirty android module creation without using Eclipse IDE to build jar files

I am currently developing an android application that allows me to dynamically load modules.
Therefore I can have as many modules as I like, and my main application just needs to load them.
I've managed to make this work for the most part - but I want to create an executable to "speed up" the module creation process, or even just find a way to simplify the steps involved.
To create a module I currently have to do the following:
Build the android project containing the files required for my
module [none of which are activity classes - so I don't have to
worry about the androidmanifest file at all]
Use the eclipse IDE jar creation tool to select which src files I want to be compiled into .class to put into my jar, and specify my own custom manifest file for this jar, as well as package all the images I use for the module into the jar as well.
Then using the jar file I run the dex creator command on it to generate a .dex file from the class files contained in the jar, and then use the aapt command to push the dex file back into the jar file.
At this point the jar(Now its a module) is created and I can put it on the server for downloading, download the modules in my app and load all the code I need in my app using reflection.
I have looked into building with ant. It looks fairly complicated for what I wish to achieve and I'm not quite sure where to start with it.
I obviously can't use simple javac to compile my java files contained in my module src because all that code makes references to the android sdk as well as a static library shared between my main application and my respective module.
Currently I use the Eclipse IDE to create the inital jar with all my packaged images, class files, manifest, and then I use two separate batch files that call on the android-sdk to create dex and push the dex into the jar.
Can I simplify this process in one easy step instead? Or is trying to do this - a whole project on its own?
I have looked into building with ant. It looks fairly complicated for what I wish to achieve and I'm not quite sure where to start with it.
The documentation for Ant is online, as is the documentation for building Android projects with Ant. The only difference is that you will want to add a <jar> task to your Ant build.xml file, as I have done in several projects, such as this one:
<target name="jar" depends="debug">
<jar
destfile="bin/CWAC-EndlessAdapter.jar"
basedir="bin/classes"
/>
</target>
You are also welcome to consider Maven. While I do not use Maven personally, it has many fans and community-driven Android support.
You are also welcome to write your own build script in any programming language that suits your fancy: Java, Ruby, Perl, Python, etc.
I obviously can't use simple javac to compile my java files contained in my module src because all that code makes references to the android sdk as well as a static library shared between my main application and my respective module.
Every Android IDE, and Ant, and Maven, and so on, "use simple javac to compile [an Android project's] java files". They simply add the appropriate Android SDK JAR file to the build path.

Is it possible to create jar that includes android functions?

As the topic indicates I would like to create a jar library that uses some android functions (no layouts) and that will be included in an Android project.
Is that possible and how?
From the research I've made I managed to include a simple jar file that uses pure Java (JAVA SE 1.6), but
when I tried creating a jar file I encountered the following exception when I tried to run the Andoid app: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: mylib.pleasework.amen
I tried including android.jar in my library and removing the java library, so that the jar file is build against android sdk, but it didn't work.
I tried including the jar file under a /libs folder as it is said to be the correct way to import jars in android projects from ADT v17 and after, but that didn't work either.
The jar I want to create will not use any resources (xml layouts, strings.xml) just Log.d and WifiManager.I am aware of Android Library Project but my library source is sensitive and I am afraid that it won't be safe if exposed in a Android library project. I was thinking of creating a jar and using ProGuard ( http://developer.android.com/tools/help/proguard.html ) obfuscate it.
I think I mentioned everything. Any help will be appreciated.
Thanks,
Thomas
As the topic indicates I would like to create a jar library that uses some android functions (no layouts) and that will be included in an Android project. Is that possible and how?
Use the jar command, or the <jar> Ant task. I am sure that there are ways to export a JAR from Eclipse, but I personally have never used them.
For example, in this GitHub repo I have a reusable component and a sub-project that is a sample app. My build.xml for the repo contains the following custom task:
<target name="jar" depends="debug">
<jar
destfile="bin/CWAC-WakefulIntentService.jar"
basedir="bin/classes"
/>
</target>
This generates a JAR file, that other Android applications can use by adding to their libs/ directories.
I am aware of Android Library Project but my library source is sensitive and I am afraid that it won't be safe if exposed in a Android library project.
It won't be safe exposed as a JAR, then, either. You can create an Android library project for public consumption that replaces the src/ tree's contents with a compiled JAR in libs/ in the library.
The way I did it in the end was: to create an Android Library project (check isLibrary checkbox in project properties) export it through Eclipse (right click on the project->export->jar file, careful to deselect all resources - res folder, androidmanifest.xml, *.png etc) and put it in the project you want by importing it under /libs folder. I don't know if this is the best solution but it worked for me.Used ADT r20, Eclipse 3.7.1, Android api level 7

Categories

Resources