Android Compile call C code

Source: Internet
Author: User

Blog Address: www.zalezone.cn



Foreword Source of Demand

These days to help others do a simple Android client, there is no function, the main is to call C code to the mobile phone WiFi network settings, so it also leads to the technical difficulties, how to call C program to achieve our desired effect.

Solution Solutions

For this, I came up with two solutions:

    1. The first scenario is to use JNI for local invocation. As for what is JNI, JNI is actually the abbreviation for Java Native Interface , the Java local interface, which provides several APIs that enable communication between Java and other languages (mainly C and C + +).

    2. The second method is to compile the C code you want to execute into an executable file , and then package the executable file with the program as an APK to invoke the executable when it is needed.

Final selection

Finally, I chose the second option, the reason is that the second method is generally simpler and more operable when I have an executable file. And the first one, because the Android NDK,NDK is a collection of tools that help developers quickly develop C or C + + dynamic libraries, and can automatically package so and Java applications together as APK, very handy.

Technology implementation executable file

First need to get an executable file, of course, you want to the executable file is not as simple as imagined, not in Linux directly to the gcc can be, here need to cross-compile C code Get executable files that can be run on the Android machine, how to cross-compile C files, here will not repeat, you can search the Internet. In addition, the NDK is a good tool.

Resource Storage

The Resource storage page here is a small pit, usually when we write Java programs, if you want to open a file, the direct input path, such as if the file you want to use in the project directory, directly enter the file name can be called, but here the operating environment is embedded devices, not the PC, This involves a question of how resources are stored.

Let's talk about the similarities and differences between the Asset folder and the Res/raw folder in Android:

    • Same point
      • The files in both directories are stored intact in the APK package after they are packaged and will not be compiled into binary.
    • Different points
      • The files in the Res/raw are mapped to R.java, which are accessed directly using the resource ID, and the files under the Assets folder are not mapped to R.java.
      • Res/raw can not have a directory structure , and the assets directory can be re-established folder.
Resource acquisition

Here by the way, res/raw The file resource Read method, get the input stream for write operation

1
InputStream is =getresources (). Openrawresource (R.id.filename);

The next thing I use is to read the assets method, and also to write by getting the input stream

123
null; am = Getassets (); InputStream is = Am.open ("filename");

Note: It is said that the assert can only put a single file not more than 1M files, but is not really specific has not been verified, if you encounter problems should consider this point of attention.

Although the read is successful, but to use the Shell script execution, should be on the phone's storage should have this file, just read the words in the phone is not found, so we need a file to save the operation. Here I wrote a function to save the file, in which the methods to get the data in assets are combined.

 123456789101112131415 
  Public  void  copydatatosd  (String outfilename) throws  ioexception{inputstream myinputstream;outputstream myoutputstream = new  FileOutputStream (outfilename); myinputstream = this . Getassets (). Open (  "a.out" ); byte  [] buffer = new  byte  [1024 ];< span class= "keyword" >int  length = myinputstream.read (buffer); while  (Length > 0 ) {myoutputstream.write (buffer, 0 , length); length = myinputstream.read (buffer);}
 Myoutputstream.flush (); Myinputstream.close (); Myoutputstream.close ();} 

And then I define the incoming outfilename that is defined by the file path plus the filename

12
Private Static "Data/data/com.example.g3wifi/a.out"; Private Static File Exe_file;
Shell command execution

Here is "everything is ready, only the East wind", we need to execute the resulting executable file, because Android is Linux -based, so some basic commands are still supported, in Android to execute the shell command, the following format is as follows:

12345678
 Public  void ExeC (String cmd) throws Ioexception{runtime Runtime =runtime.getruntime (); Process process = runtime.exec (cmd);    //process Process = runtime.exec (new string[]{"su", "reboot"});//You can execute two commands    //This can get the result after executing the shell command    New BufferedReader (new InputStreamReader (Process.geterrorstream ()));





Android Compile call C code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.