How to create JAR file from Android Studio also referencing res/ folder? Below is my graddle. As of now it contains "include('classes.jar')" inside "task exportJar". Is it possible to also add resource folder?
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy'
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
//task to delete the old jar
task deleteOldJar(type: Delete){
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy){
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
//Give whatever name you to give
rename('classes.jar', 'Executable.jar')
}
exportJar.dependsOn(deleteOldJar, build)
To properly include resources, manifest entries, assets, in addition to compiled code, you need to create an android library which exports an AAR file (not a JAR).
You do this with an Android Library. You can create one using Android Studio with File -> New -> Module and select Android Library. You'll notice that the build.gradle it creates has apply plugin: 'com.android.library' which tells gradle that it's going to build an aar instead of an app. Other app projects can then include this aar to merge all the library resources into its own.
Related
The reason I need this is because I'm trying to work around what I'm told is a bug with Xamarin not always closing streams and thus not writing to some content providers. The technique is to create a jar and then run a script to C#ifiy it and export it to a dll. That's why I need a jar.
I initially tried this by creating a Java Library but that didn't work because I need access to various android packages (for example, android.Net). So while I did manage to create a jar for a Java Library following these instructions (Abhinav Tyagi's answer) I can't seem to get it to work for an Android Library. For an Android Library it says it ran the Create Jar script but I don't see it.
To be clear, this is what I would like to turn into a jar:
Export jar file using Android Studio
Check out the library source code from the repository.
Import checkout library in your android studio.
Modify your build.gradle
If your current module is a library project then you need to properly recognize ‘android-library’ plugin as mention below.
apply plugin: 'com.android.library'
android {
compileSdkVersion 'Google Inc.:Google APIs:16'
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 13
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:support-v13:23.0.0'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myLib.jar')
into('release/') //you can change this directory where you want to copy your .jar
}
task clearJar(type: Delete) {
delete 'build/libs/myLib.jar'
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 'Google Inc.:Google APIs:16'
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 13
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:support-v13:23.0.0'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myLib.jar')
into('release/') //you can change this directory where you want to copy your .jar
}
task clearJar(type: Delete) {
delete 'build/libs/myLib.jar'
}
Run gradlew to make jar.
For generating .jar, follow below steps,
Open the terminal.
Move to your project root directory.
Call makejar function which you have added in build.gradle
Example :-> D:\Users\MyWorkspace\project > gradlew makejar
After following all above steps you will find “myLib.jar” under release folder.
I am fairly new to android studio, and I am trying to export a jar out of an Android Studio project for another Unity project.
I followed the instruction given on the link Create An Android Plugin For Unity Using Android Studio
But whenever I'm trying to export its showing "Build Successful" and "External task execution finished 'exportJar'" but I am not able to find any jar in specified folder of gradle file.
I looked into the folders "app/release" its empty and "app/build/intermediates/bundles/release/" contains only another folder named "instant-run".
So I am bit confused whether I am skipping any step or looking on the wrong path.
I m using Android Studio v2.1.2
App.build file
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.example.merchantapp"
minSdkVersion 8
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:22.2.1'
compile files('libs/PGSDK_v1.0.jar')
compile files('libs/classes.jar')
}
task deleteOldJar(type: Delete) {
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
///Rename the jar
rename('classes.jar', 'AndroidPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
android {
lintOptions {
abortOnError true
}
}
you should change this line
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
it is mandatory. and then rebuild your project.
cause we are generating library and it should required for build jar file.
reference How to export library to Jar in Android Studio?
I have this project structure:
ProjectFolder/IosFolder,AndroidFolder,CommonFolder
Now android app uses files from it's assets folder.
But we decide to make Common folder for the same files.
Could you help me to make function witch will copy files from Common folder(this folder is under my project, so in Android Studio I don't see it) to android assets folder before app will be built?
In Common folder will be some .json files and font files.
As I understand, i need to write this function in my build.gradle file
something like that:
task copyFiles(type: Copy)
copyFiles {
description = 'Copy files'
from 'Common/'
into 'Android/{projectName}/app/src/main/assets'
}
Here is my file:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "amc.amc_mobile_promo2"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
//For Flurry
multiDexEnabled = true
}
//For Flurry
/*compileOptions {
//noinspection GroovyAssignabilityCheck
sourceCompatibility JavaVersion.VERSION_1_7
//noinspection GroovyAssignabilityCheck
targetCompatibility JavaVersion.VERSION_1_7
}*/
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'joda-time:joda-time:2.8.2'
compile 'com.github.orangegangsters:swipy:1.2.0#aar'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.6.0'
/*compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services-ads:8.3.0'
compile 'com.google.android.gms:play-services-identity:8.3.0'
compile 'com.google.android.gms:play-services-gcm:8.3.0'*/
}
And could you tell me where can i see results of executed methods in Gradle Console?
What path i need to use and where in build.gradle file situate this method?
Hope you will help me.
Can you try this configuration:
gradle.projectsEvaluated {
preBuild.dependsOn(copyFiles)
}
update:
there are many commands the copy task can do for you. from the docs here are examples:
task anotherCopyTask(type: Copy) {
// Copy everything under src/main/webapp
from 'src/main/webapp'
// Copy a single file
from 'src/staging/index.html'
// Copy the output of a task
from copyTask
// Copy the output of a task using Task outputs explicitly.
from copyTaskWithPatterns.outputs
// Copy the contents of a Zip file
from zipTree('src/main/assets.zip')
// Determine the destination directory later
into { getDestDir() }
}
if you just want to copy from one source directory to another you can do this :
task copyFiles(type: Copy) {
from 'pathToMyAssets'
into 'AndroidStudioAssetsFolderPath'
}
UPDATE do this in your app's build.gradle at the very bottom:
task copyFiles(type: Copy) {
from 'Users/kostya/repo_amc_mobile_promo/Common/'
into 'Users/kostya/repo_amc_mobile_promo/Android/AMC_Mobile_Promo2/app/src/main/assets'
}
preBuild.dependsOn(copyFiles)
According to documentation here's how to copy a file via gradle:
task myCopyTask(type: Copy) {
from file("${buildDir}/reports/my-report.pdf")
into file("${buildDir}/toArchive")
}
To do it before build you must add the following line:
preBuild.dependsOn(myCopyTask)
You can use absolute or relative path that may use some of the project properties. To ensure path of project properties do not hesitate to print it using :
println file('${buildDir}')
This application is a customized View which has a 3d effect:
enter image description here
Please notice the contents which are surrounded by the red box.
*.so is a C++ SDK which contains some 3d animation effects.
I want to make this whole project into a widget SDK, so I can simply reuse it in my other project.
Could anybody give me some suggestions how to make it happened?
Here is my build.gradle, unfortunately this gradle export a jar I can not use, which I think the reason that it is lacked of *.so.
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets{
main{
jniLibs.srcDirs=['libs']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
//task to delete the old jar
task deleteOldJar(type:Delete){
delete 'release/AndroidPlugin.jar'
}
//task to export contents as jar
task exportJar(type:Copy){
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
//give whatever name you want to give
rename('classes.jar','icreatorSDK.jar')
}
exportJar.dependsOn(deleteOldJar,build)
Waiting for your advice. Thank you!
I'm using Android Studio and building Android library project, so I distribute aar file. And to be more specific I just distribute the jar inside (yes- customers still using Eclipse so I combine manually the jar with manifest, res etc well known procedure).
This is my Gradle build script (regular default...):
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
lintOptions {
abortOnError false
}
defaultConfig {
// Ain't applicationIdNot
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile '...'
}
The problem is that: Under one java package alongside regular java classes I have ttf font files. This is due to historical reasons and now the project complexity not allowing to make changes and move them to where they actually need to be.
Well - The jar missing these files. How to implement the build process such the jar inside the arr will include these files - Because after all - It's regular archive and those file are in the file tree already compressed into the jar.
Thanks,
Just specify to include the files in your sourceSets closure
// if you want to include the files in your aar
android {
...
sourceSets {
main {
resources.includes = [ '**/mySweetFont.ttf' ]
}
}
}
// to include in your jar
sourceSets {
main {
resources.includes = [ '**/mySweetFont.ttf' ]
}
}
This will place the files at the root of the artifact produced. For the jar you may need to specify the sourceSets closure twice once inside android closure and again after the android closure