Add a JNI build file (. so file) to the Android Studio Gradle Project

Source: Internet
Author: User

Ext.: http://blog.csdn.net/qiujuer/article/details/24209457

To adapt to the trend of using Android studio or more than half a year!

For adding. jar files and. So files from the Eclipse migration project to Android Studio is undoubtedly a very important and headache problem!

In the latest version, the default is to automatically package all of the. jar files under Libs, so the jar does not have to speak more.

Join. So (this is to see your project Gradle version)

Tip: All set Build.gradle files

(1) old version, as if refers to 0.5 ago, specifically not remember, the method is as follows:

[Plain]View Plaincopy
    1. task copynativelibs (type: copy)  {  
    2.      from (New file (Project (': MyProject ') .builddir,  ' native-libs '))  { include   ' **/*.so '  }  
    3.     into new file ( builddir,  ' native-libs ')   
    4. }  
    5.   
    6. Tasks.withtype (Compile)  { compiletask -> compiletask.dependson copynativelibs  }  
    7.   
    8. clean.dependson  ' cleancopynativelibs '    
    9.   
    10. Tasks.withtype (com.android.build.gradle.PackageApplicationTask )  { pkgtask ->  
    11.     pkgtask.jnidir  New file (builddir,  ' native-libs ')   
    12. }  


(2) The new version, the specific version number forgot, anyway if you download is the latest and then comes with the latest compiler.

I now know three ways to pack this version. So file

(2.1) Unpack the APK file automatically after the. jar file before packing

[Plain]View Plaincopy
    1. Task Nativelibstojar (type:zip, Description: ' Create a jar archive of the native Libs ') {
    2. Destinationdir file ("$buildDir/native-libs")
    3. BaseName ' Native-libs '
    4. Extension ' jar '
    5. From Filetree (dir: ' Libs ', include: ' **/*.so ')
    6. Into ' lib/'
    7. }
    8. Tasks.withtype (Compile) {
    9. Compiletask-Compiletask.dependson (Nativelibstojar)
    10. }


The following sentence is the. jar file in the package build directory (BUILD\NATIVE-LIBS)

[Plain]View Plaincopy
    1. Compile Filetree (dir: "$buildDir/native-libs", include: ' Native-libs.jar ')


(2.2) Automatically unpack to apk file after manually generating. jar file

This method needs to be done manually. So file compression, in the following steps: Compress all required. So files into a. zip file (the file directory structure in the zip is: lib/armeabi/*.so) and then change the zip file suffix to. Jar and then put it in Libs to generate the apk on OK


The default is to automatically package all. JAR file:

[Plain]View Plaincopy
    1. dependencies {
    2. Compile Filetree (dir: ' Libs ', include: ' *.jar ')
    3. }


(2.3) This is now the Way I am using (recommended), in fact, the majority of the day is just put. So files packaged into the Lib folder apk, if you carefully read the use of Gradle, naturally know that in fact Gradle official in the new version has been automatically implemented packaging. So file. Very simple level is to add the following content under the configured Android node OK:

[Plain]View Plaincopy
    1. sourcesets {
    2. Main {
    3. Jnilibs.srcdirs = [' Libs ']
    4. }
    5. }


Other places without modification, the entire project configuration file is as follows:

[Plain]View Plaincopy
  1. Apply plugin: ' Android '
  2. Android {
  3. Compilesdkversion 19
  4. Buildtoolsversion "19.0.0"
  5. Defaultconfig {
  6. Minsdkversion 16
  7. Targetsdkversion 19
  8. Versioncode 1
  9. Versionname "1.0"
  10. }
  11. Buildtypes {
  12. Release {
  13. Runproguard false
  14. Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' proguard-rules.txt '
  15. }
  16. }
  17. sourcesets {
  18. Main {
  19. Jnilibs.srcdirs = [' Libs ']
  20. }
  21. }
  22. }
  23. dependencies {
  24. Compile Filetree (dir: ' Libs ', include: [' *.jar '])
  25. }


This is not easy, no manual packaging, no complicated packaging copy commands, simple operation is OK. It's important to look more at official documents.

Add a JNI build file (. so file) to the Android Studio Gradle Project

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.