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
- task copynativelibs (type: copy) {
- from (New file (Project (': MyProject ') .builddir, ' native-libs ')) { include ' **/*.so ' }
- into new file ( builddir, ' native-libs ')
- }
-   
- Tasks.withtype (Compile) { compiletask -> compiletask.dependson copynativelibs }
-   
- clean.dependson ' cleancopynativelibs '
-   
- Tasks.withtype (com.android.build.gradle.PackageApplicationTask ) { pkgtask ->
- pkgtask.jnidir New file (builddir, ' native-libs ')
- }
(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
- Task Nativelibstojar (type:zip, Description: ' Create a jar archive of the native Libs ') {
- Destinationdir file ("$buildDir/native-libs")
- BaseName ' Native-libs '
- Extension ' jar '
- From Filetree (dir: ' Libs ', include: ' **/*.so ')
- Into ' lib/'
- }
- Tasks.withtype (Compile) {
- Compiletask-Compiletask.dependson (Nativelibstojar)
- }
The following sentence is the. jar file in the package build directory (BUILD\NATIVE-LIBS)
[Plain]View Plaincopy
- 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
- dependencies {
- Compile Filetree (dir: ' Libs ', include: ' *.jar ')
- }
(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
- sourcesets {
- Main {
- Jnilibs.srcdirs = [' Libs ']
- }
- }
Other places without modification, the entire project configuration file is as follows:
[Plain]View Plaincopy
- Apply plugin: ' Android '
- Android {
- Compilesdkversion 19
- Buildtoolsversion "19.0.0"
- Defaultconfig {
- Minsdkversion 16
- Targetsdkversion 19
- Versioncode 1
- Versionname "1.0"
- }
- Buildtypes {
- Release {
- Runproguard false
- Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' proguard-rules.txt '
- }
- }
- sourcesets {
- Main {
- Jnilibs.srcdirs = [' Libs ']
- }
- }
- }
- dependencies {
- Compile Filetree (dir: ' Libs ', include: [' *.jar '])
- }
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