what is the magic of nativeLibsToJar - android-studio

The following code snippet seems to be the answer how to include native libraries with Android Studio:
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)
}
It seems to simply pack the *.so into *.jar. But I really don't understand it: Why is it necessary to wrap it in a
*.jar? When changing something in my native libraries, I can see the changes taking effect in my Application, also the Gradle building process always outputs "...:app:nativeLibsToJar UP-TO-DATE...". So I assume this task is not re-run. But when this task wraps the *.so in *.jar than how is it possible to re-wrap them without rerunning this task??
I am thankful for every explanation :)

That's really funny - I found this as solution so many times:
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/'
}
But it can be removed because it does nothing useful (maybe it did in older build-versions).
The real trick is done through sourceSets.main.jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of default dir 'jniLibs

Related

Download and commit gradle dependencies and plugins in Android Studio

This is an excerpt from a build.gradle file for one of my modules. I'm using android-studio-1.5.1 and gradle 2.10.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
}
I also have classpath 'com.google.gms:google-services:3.0.0' in the project level build.gradle file.
I'm trying to gather all the associated jars into a directory which I can commit to my git repo. Something like:
task copyRuntimeLibs(type: Copy) {
into "${projectDir}/libs"
from configurations.compile
}
(This does not work)
Also, I'm not trying to download the sources or javadocs.
I need to be able to commit all dependencies so that the project can be shared on an intranet without internet access.
I've written a plugin which will download all jars and poms. See the code here to download all jars and all poms from a Gradle Configuration
Note: There's a failing test here which shows that the parent pom's are not being downloaded. I've raised an issue here on Gradle's github.
I will likely improve the plugin to invoke the Maven Model Builder APIs to get the parent poms.
FYI - I've already integrated the ModelBuilder APIs successfully with Gradle (see here and here) so shouldn't be too difficult.

Gradle - build jar which includes both .class and .java files?

I'm using android studio + gradle. I have a module that is a plain standalone java app. Is there a way I can package that module into a .jar file that includes both its compiled .class files, and its source .java files? By default it looks like android studio is only including .class files for me.
I was originally doing this using Eclipse (export as a jar), but can't figure out how to do the equivalent with android studio.
---- EDIT -----
This is my current build.gradle file, but it still outputs a jar that only includes the .class files:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
task sourcesJar(type: Jar) {
from sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
Thanks
It is most common to publish a separate sources JAR from compiled classes.
To do that in Gradle, add this to your Gradle build:
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}

Android Studio: including AAR library from a library project

In my Android Studio project I have two subprojects/modules: an Android application (App1) and an Android library project (LibraryProject1). App1 depends on LibraryProject1. So far so good.
However, LibraryProject1, in turn, needs to import an AAR library to work properly.
So my Configuration is as follows:
App1 includes LibraryProject1
LibraryProject1 includes dependency.aar
Now, to include dependecy.aar I use the method detailed here:
How to manually include external aar package using new Gradle Android Build System
So basically in my build.gradle for LibraryProject1 I have:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile (name:'dependency', ext:'aar') //my AAR dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
Obviously, I put my dependency.aar file in the libs directory of LibraryProject1
However, this doesn't work. It seems that the repository added by LibraryProject1 is completely ignored and the local "libs" folder is not included as a repository, causing compilation to fail.
If I add the repository from the App1's build.gradle it works, but I don't want to do that, it's LibraryProject1 that needs the AAR file, not App1.
How can I do this??
Well, I found a way, but since it's very "hacky" I'll leave the question open, in case anyone comes up with a better, "proper" solution.
Basically the problem is that the flatDir repository is ignored at compilation time if included from LibraryProject1's build.gradle script, so what I do is I use App1's build.gradle to "inject" the flatDir repository in LibraryProject1. Something like this:
//App1 build.gradle
dependencies {
//get libraryproject1 Project object
Project p = project(':libraryproject1')
//inject repository
repositories{
flatDir {
dirs p.projectDir.absolutePath + '/libs'
}
}
//include libraryproject1
compile p
}
This effectively allows LibraryProject1 to include the external AAR library without having App1 include it. It's hacky but it works. Note that you still have to put:
//LibraryProject1 build.gradle
repositories {
flatDir {
dirs './libs'
}
}
inside LibraryProject1's build.gradle otherwise, even if the project itself would compile fine, the IDE wouldn't recognize the types included in the AAR library. Note that the ./ in the path also seems to be important, without it the IDE still doesn't recognized the types.
I faced to the same issue, and I figure out it by putting all libraries on that depends LibraryProject1 in LibraryProject1/libs as a .jar.
I think that aar library cannot be linked to another aar library.
Hope that help you,
Best regards

Google licence Verification Library not getting referenced, android-studio 0.8.2

Being new to android and android studio(0.8.2), I am trying hard to implement Google LVL for my project.
I have followed the link:- http://developer.android.com/google/play/licensing/setting-up.html and lots more help from web.
I want to add LVL as library project and I followed the steps mentioned in this link:--
How do I add a library project to Android Studio?
1) Created a new module for LVL.
2) copied the code from the downloaded com directory to the newly created modules com directory.
3)I have added the proper dependency using dependency tab on main project.
Following imports are not compiling.
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
If I verify my configuration files, all looks proper.
Following is my android.xml
`
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
</application>
`
build file of this LVL module is:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
build file of the main app shows the added dependency:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':apkExpansion')
}
settings.gradle has, which also looks proper:
include ':app', ':licensing', ':apkExpansion'
project(':licensing').projectDir = new File('library/licensing')
project(':apkExpansion').projectDir = new File('library/apkExpansion')
My project as the following setup:
MyProject/
| settings.gradle
+ app/
| build.gradle
+ librarary/
+ licensing
| build.gradle
+ apkExpansion/
| build.gradle
After looking your files closely, I see the probable cause is the package name in Android.xml of LVL library module.
When you create a new module for any existing library, the package name should match the package of library or else if you wish, you need to edit all files package deceleration.
You need to compile your library module separately to see if everything goes well.
Also, other than com folder you need to copy the res folder to your library module to avoid any further issues.
Hope this helps.

Barebones Gradle build from lib dir

I'm struggling to configure a super simple (my 1st ever) Gradle buildscript, that will:
Compile all the Groovy code under my src/main/groovy dir; compilation needs to include all the local (not from a repo) JARs stored in my lib/ directory
Place that compiled code under /bin (or wherever, I really don't care)
JAR up the compiled code into myapp.jar
Somehow include the wrapper task so that the gradlew gets generated as is appropriate
My project dir structure:
myapp/
src/main/groovy/
<Groovy sources>
bin/
lib/
<lots of JARs>
build.gradle
gradle.properties
So far this is what I've tried:
apply groovy
task compile {
println "Compiling from src/main/groovy and lib/ to bin/"
javac ???
}
task jar {
println "JARring up the code"
jar ???
}
Any help or nudges in the right direction would be enormously helpful for me.
What You need to do is just to apply plugin: 'groovy' and add the following section:
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
}

Resources