How to share SDK without src folder [duplicate] - android

I'd like to create an aar file for my library in Android Studio, i would've gone with a jar option but my library has resources.
Any idea how to create an aar file from a library?

If your library is set up as an Android library (i.e. it uses the apply plugin: 'com.android.library' statement in its build.gradle file), it will output an .aar when it's built. It will show up in the build/outputs/aar/ directory in your module's directory.
You can choose the "Android Library" type in File > New Module to create a new Android Library.
If you are still not seeing your aar file, select Build > Rebuild Project.

Retrieve exported .aar file from local builds
If you have a module defined as an android library project you'll get .aar files for all build flavors (debug and release by default) in the build/outputs/aar/ directory of that project.
your-library-project
|- build
|- outputs
|- aar
|- appframework-debug.aar
- appframework-release.aar
If these files don't exist start a build with
gradlew assemble
for macOS users
./gradlew assemble
Library project details
A library project has a build.gradle file containing apply plugin: com.android.library. For reference of this library packaged as an .aar file you'll have to define some properties like package and version.
Example build.gradle file for library (this example includes obfuscation in release):
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "0.1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Reference .aar file in your project
In your app project you can drop this .aar file in the libs folder and update the build.gradle file to reference this library using the below example:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 20
versionCode 4
versionName "0.4.0"
applicationId "yourdomain.yourpackage"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
}
dependencies {
compile 'be.hcpl.android.appframework:appframework:0.1.0#aar'
}
Alternative options for referencing local dependency files in gradle can be found at: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project
Sharing dependencies using maven
If you need to share these .aar files within your organization check out maven. A nice write up on this topic can be found at: https://web.archive.org/web/20141002122437/http://blog.glassdiary.com/post/67134169807/how-to-share-android-archive-library-aar-across
About the .aar file format
An aar file is just a .zip with an alternative extension and specific content. For details check this link about the aar format.

just like user hcpl said but if you want to not worry about the version of the library you can do this:
dependencies {
compile(name:'mylibrary', ext:'aar')
}
as its kind of annoying to have to update the version everytime. Also it makes the not worrying about the name space easier this way.

To create AAR
while creating follow below steps.
File->New->New Module->Android Library and create.
To generate AAR
Go to gradle at top right pane in android studio follow below steps.
Gradle->Drop down library name -> tasks-> build-> assemble or assemble release
AAR will be generated in build/outputs/aar/
But if we want AAR to get generated in specific folder in project directory with name you want, modify your app level build.gradle like below
defaultConfig {
minSdkVersion 26
targetSdkVersion 28
versionCode System.getenv("BUILD_NUMBER") as Integer ?: 1
versionName "0.0.${versionCode}"
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "/../../../../release/" + ("your_recommended_name.aar")
}
}
}
Now it will create folder with name "release" in project directory which will be having AAR.
Updated Answer
In Latest releases specific path is not supported.Please add below code in library's build.gradle and rebuild project.After Rebuilding "aar",change project structure from Android to Project->navigate to your library->build->outputs->aar
android {
defaultConfig {
minSdkVersion ..
targetSdkVersion ..
versionCode ...
versionName "1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "${archivesBaseName}_${variant.name}_${defaultConfig.versionName}.aar"
}
}}
To import "aar" into project,check below link.
How to manually include external aar package using Gradle for Android

After following the first and second steps mentioned in the hcpl's answer in the same thread, we added , '*.aar'], dir: 'libs' in the our-android-app-project-based-on-gradle/app/build.gradle file as shown below:
...
dependencies {
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
...
Our gradle version is com.android.tools.build:gradle:3.2.1

btw #aar doesn't have transitive dependency. you need a parameter to turn it on:
Transitive dependencies not resolved for aar library using gradle

Finally got the solution here - https://stackoverflow.com/a/49663101/9640177
implementation files('libs/aar-file.aar')
Edit
I had one more complication - I had set minifyEnabled true for the library module.

[JAR vs AAR]
.jar is generated by apply plugin: 'java-library'
.aar is generated by apply plugin: 'com.android.library'
File -> New -> New Module... -> Android Library

If you have correctly set up for publishing, then you can just run this command to generate aar files.
./gradlew publishReleasePublicationToMavenLocal
This will generate a aar file inside <module-dir>/build/output/aar directory.
Now you can use this library in other local projects also. Add this in the project gradle in which you want to use this aar module
implementation fileTree(dir: '<location-to-library>/build/outputs/aar/', include: ['*.aar', '*.jar'], exclude: [])

Build ---> Build bundle/apk
.aar file will be generated in build/outputs/aar folder.

Related

How to compile with gradle different dependency module's dependencies based on build variants?

I have an Android app which has a module dependency. This module itself has a jar library as a dependency that comes in two variants, each relative to a build variant of the main app. When I switch build variant in the main app, I managed to automatically select the module's build variant which picks the correct jar, but this is not reflected in the code, where the specific classes from the jar are not found in the build variant specific code.
Here's the related code from the build.gradle files of the main app and the dependency module:
main app - build.gradle
buildTypes {
type1 {initWith(debug)}
type2 {initWith(debug)}
}
productFlavors {
live{}
test{}
}
dependencies {
type1Compile project(path: ':module', configuration: 'one')
type2Compile project(path: ':module', configuration: 'two')
}
module - build.gradle
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
useLibrary 'org.apache.http.legacy'
publishNonDefault true
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled = false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
one {initWith(release)}
two {initWith(release)}
}
}
dependencies {
oneCompile files('libs/versionOne/mylib.jar')
twoCompile files('libs/versionTwo/mylib.jar')
}
So, when I build testType1 variant, Android Studio automatically selects one variant, and with a clean build it all goes fine. But if I switch to testType2, although module's two variant is selected, the editor will highlight missing classes and methods.
How can I make gradle pick the right jar when I switch between the app's build variants?
Some considerations:
The module needs the library, since it uses a few classes that are common between the 2 versions.
I know this may look like bad project design, but it's an app that has been built by different people in different times, and I now have to develop it "as is".
After some tinkering, I found some clues. First of all, my two jars where the same name, in different folders. After renaming the jars to have also different names, gradle manages to pick the correct one after the build variant switch, provided that an additional "Sync project with gradle files" is done. So now it's not immediate, but eventually it's working.

Android Studio Create AAR using another AAR file and jar inside

I am creating AAR file using another aar and jar file dependency. I have successfully created the .aar release file.
Then, I have imported my new AAR file into the sample Project. the project is running fine. when going to access that aar and jar classes mean, It's showing NoClassDefFound error.
Note: First I want to know, as of now AAR inside another AAR is possible or not. can anyone help me to come out from this new things issue?
I referred to this link also Create an AAR with multiple AARs/JARs. It's saying transitive= true. But, not understand, where to use that flag. In third party using Application or while creating AAR inside AAR project dependencies.
Thanks Advance.
My Mini Project Gradle File:
Mini Project, which is only converting into .AAR file. then, this file only going to use another NewProject
Mini Project Build.gradle file
/*
This would be enabled for development purpose and to debug and check the same
*/
apply plugin: 'com.android.application'
/*
This would be enabled for exporting as aar file
apply plugin: 'com.android.library'
*/
//apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
dexOptions {
javaMaxHeapSize "4g"
preDexLibraries = false
}
defaultConfig {
/*
This application id would be used for development purpose and to debug and check the same.
This would be commented when exporting as library
*/
applicationId 'com.pss.pssplayer'
minSdkVersion 18
targetSdkVersion 23
multiDexEnabled true
versionCode 1
versionName "1.0"
}
aaptOptions {
cruncherEnabled = false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile files('libs/ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar')
compile files('libs/secure.jar')
compile(project(':getSubProject')) {
transitive = true
}// this getSubProject also contains one classes jar
}
When you refer to your aar and jar in your dependencies, then you can compile your new aar with references to the first.
But they are not embedded in your aar. Instead, the application using the "big aar" also need a dependency over the two first.
For that, you can deploy all your aar in a repository like maven in telling maven that the "big aar" depends on the two first. (-> pom.xml)
In you application build.gradle file, you can add the dependency only on the "big aar" with transitive=true to tell the system to look at the dependencies of the dependency.
Edit :
You can at least merge jars. look at these answers :
gradle - how do I build a jar with a lib dir with other jars in it?
Can Gradle jar multiple projects into one jar?
Merging aar is more difficult because you have to merge all resources, manifests and so on...
Use android-fat-aar gradle plugin, it is super usefull!

Android Studio buildflavors

Getting started with the new android studio it seems very flexible but that usually brings a steep learning curve. Hoping to get some help with that here.
I've built an app and deployed it with this build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "me.test.simpleProject
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
storeFile file("../my.keystore.jks")
storePassword System.getenv("and_ks_pw")
keyAlias System.getenv("and_ky_alias")
keyPassword System.getenv("and_k_pw")
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
debuggable true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile project(":Library:lib_my_lib_a")
compile project(":Library:lib_my_lib_b")
compile project(":Library:lib_my_lib_c")
}
This works pretty well and once I learn the syntax it'll be much easier to use than the previous system.
Now my question.
I would like to basically add a few product flavors.
I currently use google analytics to track clicks, usage stats, simple things like that.
In the past [before android studio] typically distribute my apps in a free/ paid version.
I would like to create say build flavors for:
google play free
google play paid
amazon free
amazon paid
The code for these apps should be mostly identical but they cannot be since, for example adding links to google play in your amazon apps gets you in a bit of trouble over there. So I will need to have unique classes across the build flavors.
I was reading some docs and watching google talks about setting up the build flavors but I am a bit confused.
Can someone help me define the folder structure for this project?
current application structure
test/
.gradle/
.idea/
app/
app/build
app/libs <-- empty
app/src
app/.gitignore
app/app.iml
app/build.gradle
app/proguard-rules.pro
build/
gradle/
Library/lib_a
Library/lib_b
Library/lib_c
.gitignore
build.gradle
gradle.properties
gradlew
gradlew.bat
local.properties
settings.gradle
test.iml
Can I place the new folders anywhere and how do I link them up with grade to properly build the variants?
The first thing you should do, is add these flavors to your gradle script, inside the android section:
productFlavors {
googleFree {
applicationId = "com.your.app.gfree"
}
googlePaid {
applicationId = "com.your.app.gpaid"
}
amazonFree {
applicationId = "com.your.app.afree"
}
amazonPaid {
applicationId = "com.your.app.apaid"
}
}
Note that you can define different package names for your app if needed, as well as some additional properties. Full details: Gradle Plugin - Build Variants
At this point you will have three different concepts in your app now:
Build types: Debug and Release
Product flavors: googleFree, googlePaid, amazonFree and amazonPaid
Build variants: Combination of build types and flavors
googleFreeDebug
googleFreeRelease
googlePaidDebug
googlePaidRelease
amazonFreeDebug
amazonFreeRelease
amazonPaidDebug
amazonPaidRelease
Flavor Specific resources:
For defining resources, you should have:
app
src
main
java
res
googlePaid
java
res
googleFree
java
res
amazonPaid
java
res
amazonFree
java
res
With that structure in place, you can place all your shared stuff under main, while the flavor specific stuff would live on each flavor's folder. Then, when building a specific variant of your app, android and gradle will resolve the right resources for you. If you are interested in more details, see Resource Merging.
Once the flavors have been added to your build.gradle file, if you sync your project, Android Studio will be able to pick your build variants and you may select these from the IDE:
To use flavors in your app you should have a structure like this:
app
src
flavor1
java
res
flavor2
java
res
main
java
res
You have to define the flavors in the build.gradle:
productFlavors {
flavor1 {
//
}
flavor2 {
//
}
}
Then you can have different dependencies using:
dependencies {
releaseCompile
debugCompile
flavor1Compile
flavor1DegubCompile
}
For example:
dependencies {
freeCompile 'com.google.android.gms:play-services:7.5.0'
}

How can I create aar file in android studio 1.3.1 while I've made the project without activity [duplicate]

I'd like to create an aar file for my library in Android Studio, i would've gone with a jar option but my library has resources.
Any idea how to create an aar file from a library?
If your library is set up as an Android library (i.e. it uses the apply plugin: 'com.android.library' statement in its build.gradle file), it will output an .aar when it's built. It will show up in the build/outputs/aar/ directory in your module's directory.
You can choose the "Android Library" type in File > New Module to create a new Android Library.
If you are still not seeing your aar file, select Build > Rebuild Project.
Retrieve exported .aar file from local builds
If you have a module defined as an android library project you'll get .aar files for all build flavors (debug and release by default) in the build/outputs/aar/ directory of that project.
your-library-project
|- build
|- outputs
|- aar
|- appframework-debug.aar
- appframework-release.aar
If these files don't exist start a build with
gradlew assemble
for macOS users
./gradlew assemble
Library project details
A library project has a build.gradle file containing apply plugin: com.android.library. For reference of this library packaged as an .aar file you'll have to define some properties like package and version.
Example build.gradle file for library (this example includes obfuscation in release):
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "0.1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Reference .aar file in your project
In your app project you can drop this .aar file in the libs folder and update the build.gradle file to reference this library using the below example:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 20
versionCode 4
versionName "0.4.0"
applicationId "yourdomain.yourpackage"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
}
dependencies {
compile 'be.hcpl.android.appframework:appframework:0.1.0#aar'
}
Alternative options for referencing local dependency files in gradle can be found at: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project
Sharing dependencies using maven
If you need to share these .aar files within your organization check out maven. A nice write up on this topic can be found at: https://web.archive.org/web/20141002122437/http://blog.glassdiary.com/post/67134169807/how-to-share-android-archive-library-aar-across
About the .aar file format
An aar file is just a .zip with an alternative extension and specific content. For details check this link about the aar format.
just like user hcpl said but if you want to not worry about the version of the library you can do this:
dependencies {
compile(name:'mylibrary', ext:'aar')
}
as its kind of annoying to have to update the version everytime. Also it makes the not worrying about the name space easier this way.
To create AAR
while creating follow below steps.
File->New->New Module->Android Library and create.
To generate AAR
Go to gradle at top right pane in android studio follow below steps.
Gradle->Drop down library name -> tasks-> build-> assemble or assemble release
AAR will be generated in build/outputs/aar/
But if we want AAR to get generated in specific folder in project directory with name you want, modify your app level build.gradle like below
defaultConfig {
minSdkVersion 26
targetSdkVersion 28
versionCode System.getenv("BUILD_NUMBER") as Integer ?: 1
versionName "0.0.${versionCode}"
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "/../../../../release/" + ("your_recommended_name.aar")
}
}
}
Now it will create folder with name "release" in project directory which will be having AAR.
Updated Answer
In Latest releases specific path is not supported.Please add below code in library's build.gradle and rebuild project.After Rebuilding "aar",change project structure from Android to Project->navigate to your library->build->outputs->aar
android {
defaultConfig {
minSdkVersion ..
targetSdkVersion ..
versionCode ...
versionName "1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "${archivesBaseName}_${variant.name}_${defaultConfig.versionName}.aar"
}
}}
To import "aar" into project,check below link.
How to manually include external aar package using Gradle for Android
After following the first and second steps mentioned in the hcpl's answer in the same thread, we added , '*.aar'], dir: 'libs' in the our-android-app-project-based-on-gradle/app/build.gradle file as shown below:
...
dependencies {
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
...
Our gradle version is com.android.tools.build:gradle:3.2.1
btw #aar doesn't have transitive dependency. you need a parameter to turn it on:
Transitive dependencies not resolved for aar library using gradle
Finally got the solution here - https://stackoverflow.com/a/49663101/9640177
implementation files('libs/aar-file.aar')
Edit
I had one more complication - I had set minifyEnabled true for the library module.
[JAR vs AAR]
.jar is generated by apply plugin: 'java-library'
.aar is generated by apply plugin: 'com.android.library'
File -> New -> New Module... -> Android Library
If you have correctly set up for publishing, then you can just run this command to generate aar files.
./gradlew publishReleasePublicationToMavenLocal
This will generate a aar file inside <module-dir>/build/output/aar directory.
Now you can use this library in other local projects also. Add this in the project gradle in which you want to use this aar module
implementation fileTree(dir: '<location-to-library>/build/outputs/aar/', include: ['*.aar', '*.jar'], exclude: [])
Build ---> Build bundle/apk
.aar file will be generated in build/outputs/aar folder.

Add AAR file as dependency to Intellj project

I'm using Intellij IDEA 14.1.4 to make an Android project after moving from Android Studio a while back. I made some libraries (through Android Studio) and would like to use them in Intellij.
However, it seems that Intellij only accepts .jar files through the libs folder, and Android Studio only creates .aar files.
In Android Studio, one has the option (when creating a new module) to import an existing .jar/.aar package to be put into a new module. This option doesn't seem to be in intellij. This user seems to think that Intellij supports that, but those instructions allow me to create a brand new Gradle module. What I want to do is use an existing .aar file.
What should I go about doing? Should I move back to Studio for this project, or is there a way for me to use these .aar files?
P.S. Intellij can't process raw aars, period. I tried adding this to my gradle files, but got an error:
compile fileTree(dir: 'libs', include: ['*.aar'])
Edit
Here's my build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
flatDir() {
dirs 'libs'
}
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ':mylib#aar'
compile 'com.android.support:appcompat-v7:22.2.0'
}
So I saw this option in Intellj called "Associate file..." and associated the aar with an archive. Upon recompile I got this error
Warning:Project app: Only Jar-type local dependencies are supported. Cannot handle: mylib.aar
I guess it's only jars for Intellij. Back to Android Studio, I suppose :)
Well first of all you can create .jar files with Android Studio as well, see this but .aars are awesome and enable you to do more then what you could with a jar
I have not used Intellij IDEA but I assume you have a gradle.build file in your project? If the name of your aar file is
my_lib_1.0.0.aar
and its under the libs directory then try the following in your gradle.build file:
compile(name:'my_lib_1.0.0', ext:'aar')

Categories

Resources