Tool Native Library Generator (C/C++ to B4A)

logo.png

Native Library Generator 4.20 (2018-11-11)
Written in B4J, this program allows you to write or import C/C++ code and have all the functions compiled into a native (.so) library, which in turn is included in a ready-to-use B4A Java lib. When using already existing code, if present, the main() function will be automatically ignored. If you're familiar with C/C++ but not with JNI or NDK, this is the right tool for you! No JAVA required.

Requirements:

Open Source:
This project is open source, under the GNU General Public License. Enjoy! :)
https://gitlab.com/brunowonder/NativeLibraryGenerator-CommunityEdition

Price (pay what you want):
If you wish to buy me a beer (or even dinner!) just follow the link below.
https://www.paypal.me/ninjadynamics
0317.png
0317.png
0317.png

How does it work:
upload_2017-10-27_15-57-1-png.61061


Video Demos:

Updates:
[Version 4.21] [2018-11-12] [CRITICAL]
QuickSource mode fixed
For background music add "nlg.mp3" to the NLG folder ;)

[Version 4.20] [2018-11-11] [NEW FEATURES]
Now supporting multiple source files
Select which files should be exposed to B4A
Requires NDK r18b (please update)
Bug fixes

[Version 4.01] [2017-05-07] [NEW FEATURES]
The 'cpu-features' library can now be used
Custom Makefiles may now be used
Custom Java wrappers may now be used
Bug fixes

[Version 4.00] [2017-05-07] [NEW FEATURES]
Complete visual overhaul
Specifying additional include paths is now possible
Specifying additional prebuilt libraries is now possible
Specifying the target ABI's is now possible
Compatible ABI's are now auto-detected if possible
Handling
multiple projects is now possible

[Version 3.0] [2017-05-07] [NEW FEATURES]
Returning arrays is now possible.
B4A logging with printf() is now possible.


[Version 2.9] [2016-08-12] [NEW FEATURES]
Small UI redesign and bug fixes.
Code clean-up.

Little bit more user-friendly.
Custom makefiles.

[Version 2.8] [2016-07-12] [NEW FEATURES]
Now supporting local #include files.
Now supporting external project locations.
New video demo.
Minor bug fixes.

[Version 2.7] [2016-03-14] [CRITICAL]
Major bug fixes.

[Version 2.6] [2016-01-31] [IMPORTANT]
Important bug fixes.
MazeSolver 1.5 Included as example source code.

[Version 2.5] [2016-01-31] [NEW FEATURES]
Now supporting comment transcription from C++ to B4A.
New splash screen.
Minor bug fixes.

[Version 2.4] [2016-01-27] [CRITICAL]
Critical bug fix regarding string (char*) support.
Cool new intro/exit screens added.


[Version 2.3] [2016-01-25] [NEW FEATURES]
Now supporting Strings (char*), both passing and returning.

[Version 2.2] [2016-01-20] [IMPORTANT]
Now supporting simple C/C++ typedef struct.
Now supporting C/C++ inline functions.

Minor bug fixes.

[Version 2.1] [
2016-01-12] [CRITICAL]
Included example (NinjaCore) source code updated. Several bug fixes.

[Version 2.1] [2016-01-12] [IMPORTANT]
Now supporting C++ headers, such as <vector> or <algorithm>.

[Version 2.0] [2016-01-10] [CRITICAL]
Major issue corrected. Please download Native Library Generator again.

Included Example:

In order to showcase this software's capabilities, I decided to include my MazeSolver project.

A. Download link:

B. Instructions (QuickSource Mode):
In QuickSouce mode all you have to do is write some code and hit the Generate Library button.

1. Before anything else, please make sure you have installed the following required software:
B4A, Simple Library Compiler and Android NDK


2. Download the .jar file into a system-writable folder (NOT Program Files).

3. Run NLG and provide the necessary software paths, as seen below.
upload_2018-11-12_11-56-16.png


4. Compile the example by clicking the big blue button.
upload_2018-11-12_11-56-37.png



5. Test the library you just compiled with this B4A project:
https://www.b4x.com/android/forum/t...orithm-w-path-optimization.61998/#post-391230

6. As you can see, it works!
upload_2018-11-12_12-14-11.png

C. LibFastMath Example code (Copy/Paste to NLG):
B4X:
//LibFastMath 1.00 - Native Library Example

//FastSin() and FastCos() by Allen Chou
//http://allenchou.net/2014/02/game-math-faster-sine-cosine-with-polynomial-curves

//FastSqrt() adapted from Quake 3's Fast Inverse Square Root algorithm
//https://en.wikipedia.org/wiki/Fast_inverse_square_root

#define PI         (3.1415926535f)
#define HALF_PI    (0.5f * PI)
#define TWO_PI     (2.0f * PI)
#define TWO_PI_INV (1.0f / TWO_PI)

float FastSin(float x);
float FastCos(float x);
float FastSqrt(float x);

inline float Hill(float x)
{
  const float a0 = 1.0f;
  const float a2 = 2.0f / PI - 12.0f / (PI * PI);
  const float a3 = 16.0f / (PI * PI * PI) - 4.0f / (PI * PI);
  const float xx = x * x;
  const float xxx = xx * x;

  return a0 + a2 * xx + a3 * xxx;
}

float FastSin(float x)
{
  // wrap x within [0, TWO_PI)
  const float a = x * TWO_PI_INV;
  x -= static_cast<int>(a) * TWO_PI;
  if (x < 0.0f)
    x += TWO_PI;

  // 4 pieces of hills
  if (x < HALF_PI)
    return Hill(HALF_PI - x);
  else if (x < PI)
    return Hill(x - HALF_PI);
  else if (x < 3.0f * HALF_PI)
    return -Hill(3.0f * HALF_PI - x);
  else
    return -Hill(x - 3.0f * HALF_PI);
}

float FastCos(float x)
{
  return FastSin(x + HALF_PI);
}

float FastSqrt(float x)
{
    //This is the method used in Quake3
    const float xhalf = 0.5f*x;

    //Get bits for floating value
    union
    {
        float x;
        int i;
    } u;
    u.x = x;

    //Give initial guess y0
    u.i = 0x5f3759df - (u.i >> 1);

    //Newton step, repeating increases accuracy
    return (x*u.x*(1.5f - xhalf*u.x*u.x));
}

D. How does it work:
Initially, I created this program in order to speed-up the process of writing JNI C/C++ code. Following B4A's RAD philosophy, it allows me to write native code in Visual Studio and compile it into a fully-working B4A lib within a single click.
E. Internal Functions:
If you wish any of your native function not to be exposed in the B4A library, you may do so by either changing their name from foo() into privateFoo() or simply adding a "::ignore" the function's comments.
B4X:
//This function will not be visible in B4A because its name starts with "private".
int privateSum(int a, int b)
{
    return a + b;
}

//This function will not be visible in B4A because this comment contains the keyword "::ignore".
int multiplication(int a, int b)
{
    return a * b;
}

//This function will be exposed in B4A.
int result(int a, int b)
{
    return privateSum(a, b) + multiplication(a, b);
}
F. Current Limitations:
As happy as I am to share this work with the B4x community, let's not forget that this is just my own internal tool, a never ending work-in-progress personal project.
Hence, it does not yet support several features such as passing Arrays into the native layer.
G. Reminder:
Please test your C/C++ code before trying to build a B4A library, as some kinds of syntax errors may crash the application.
 

Attachments

  • upload_2018-11-12_11-53-30.png
    upload_2018-11-12_11-53-30.png
    88.2 KB · Views: 560
Last edited:

JTmartins

Active Member
Licensed User
Longtime User
Yes it does. Already working perfectly in B4A.

However I had a minor problem. Running NLG and compiling your mazesolver, it produced an SLC error.
First I thought it was because I had SLC under programfiles(x86), so I moved it to c:\b4a tools\slc and changed the path in NLG accordingly.
Same problem,
However going to workspace folder and running SLC.bat manually it did compile without problems.

The code you've changed also worked Ok, running SLC manually.

Many thanks. This is indeed an extremly usefull tool.
 

wonder

Expert
Licensed User
Longtime User
Glad to hear that! :)
I'll check the SLC routine.

No issues here, I'm unable to reproduce your problem.
NLG 2.71, Java 1.8.0_73, NDK r10e, SLC latest version.

EDIT: Just upgraded to NDK r11. No issues so far.
 
Last edited:

wonder

Expert
Licensed User
Longtime User

ppgirl

Member
Licensed User
Longtime User
Hi Wonder , I want add other include & library files directory to compile , How can I set it ? thanks
 

wonder

Expert
Licensed User
Longtime User
Hi Wonder , I want add other include & library files directory to compile , How can I set it ? thanks
Hello hello!

Regarding your request, I'll be releasing an update soon, which handles #include files at the most basic level. I'll release it alongside with a demonstration video. :)
 
Last edited:

bparent

Member
Licensed User
Longtime User
Alright, the bug was SOLVED. :)

If anyone experiences the issue mentioned above, please download the latest version of NLG.
http://85.89.182.79:5555/apps/native/native_library_generator_2_6.jar?
Also updated on the first post.

@JTmartins, I'm very glad I could help!! Happy coding!! ;)

I am having a similar problem as the previous posts. I am using the nlg 2.8 jar. I get an error NDK failed to compile and to run compile.bat . When I execute this program I get path printout with the correct path for the NDK, but in quotes (in fact it shows 3 time for the 3 times I ran compile.bat). It then says ndk-build is not a command. When I run manually with SLC it compiles. Any ideas?
 

wonder

Expert
Licensed User
Longtime User

wonder

Expert
Licensed User
Longtime User
Hi!

First of all, thanks for giving my NLG a chance!! :D

About the error, well, let's see...
JNI conversion: OK
NDK compilation: OK
SLC build: FAILED

I see that you're using NDK 12, correct?

Now... ICSharpCode? I've never seen that name, file or library in my life.
I'll do some googling and get back to you.
 

wonder

Expert
Licensed User
Longtime User
Capture.png


No issues with NDK 11... I'll download v12 and give it a try.

By the way r11 stands for RELEASE 11, while 12b stands for 12 BETA.
It's possible that the latest version isn't stable yet...
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Alberto, can you please download UltraSearch (freeware), search for ICSharpCode.SharpZipLib and post a screenshot?

http://www.jam-software.com/ultrasearch/

Capture.png


@Erel, should the ICSharpCode.SharpZipLib.dll version be the same in B4A, B4J and SLC?
 
Last edited:
Top