Android studio source code share by local aar - android

I'd like to share my source code among different projects through aar. Current version of Android studio is able to generate aar files automatically for library module. But I have a few issues:
How to output aar files to specific folder?
Currently the output is in build/outputs/aar folder. Can I move the files automatically once the compilation has been finished.
The dependencies in library module is not inherited by app module. For example:
My library module (build.gradle):
apply plugin: 'com.android.library'
......
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.code.gson:gson:2.2.4'
}
My app module:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
flatDir {
dirs 'myaarfolder'
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.company.appid"
minSdkVersion 8
targetSdkVersion 21
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 'com.android.support:appcompat-v7:21.0.3'
compile(name:'mylib-release', ext:'aar')
}
I got an error something like:
Error:(3, 35) Error: Package com.google.gson.annotations not exist
I have to add compile 'com.google.code.gson:gson:2.2.4' in the dependency of my app module (build.gradle)
Any idea? Thanks

AAR files don't have their dependencies baked into them, so if you're including a bare AAR like this, you'll need to manually add dependencies in the parent module. To do what you want you'd need to package the AAR into a Maven artifact and specify its dependencies in the POM. A quick search of Google reveals this link, which looks like it's for an older version of the Android Gradle plugin, but it might get you going, or you can do your own searching for a better resource:
http://www.vandalsoftware.com/post/52468430435/publishing-an-android-library-aar-to-a-maven
If you want to manipulate of your output files, you can take inspiration from this question, which renames outputs:
Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl
Essentially it involves a block of build script that looks like this:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, /* calculate new path here */)
}
}

Related

Packages from library module not found in main module

I got a project which I imported from Eclipse to Android Studio. In Eclipse everything worked well.
It contains a main module (a project in Eclipse) which uses packages from a library module (library project in Eclipse). Since the migration did not went well, I have created a library module manually and just copied all the source code to the newly created module.
The problem is that the main module doesn't seem to find the packages from the library module and when I rebuild the project I get errors like "package bla bla does not exist".
Here is the main module gradle.build:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.pointer.mamagoose"
minSdkVersion 9
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':linphoneclean')
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.google.android.gms:play-services:9.4.0'
compile files('libs/firebase-client-android-2.5.0.jar')
compile files('libs/apache-httpcomponents-httpclient.jar')
compile files('libs/apache-httpcomponents-httpcore.jar')
compile files('libs/android-support-v7-recyclerview.jar')
}
linphoneclean is the library module.
The entire project's settings.gradle:
include ':linphoneclean'
include ':tigris'
This is the build.gradle of the library module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.6.2'
compile files('libs/commons-lang3-3.4.jar')
compile files('libs/linphone.jar')
compile files('libs/firebase-client-android-2.5.0.jar')
}
The structure of the library module includes for example folder:
src/main/com/pointer/linphone (and inside there are all the java files with a deceleration of package com.pointer.linphone, Yet I still get an error saying >"package com.pointer.linphone does not exist).
What am I doing wrong?
After fighting with the same issue for hours here is what worked for me.
I've created a fresh project with blank activity, added a library module with a dummy class, defined the dependency. Verified that it works by importing the dummy class in the app. Then I copied all my relevant code from the real project.
My thinking is that it was probably issue with IDE's iml files, since starting from scratch and copying stuff over worked.
See properly source file contains both java folder and res folder,add java files in java packages and res in res folder.Add Activity name in Manifest file see here ,add necessary libraries files in Gradle file.Clean and rebuild the project in the Android studio.
https://developer.android.com/guide/topics/manifest/manifest-element.html
Maybe you can check if the library's AndroidManifest has defined the package correctly, like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.pointer.linphone">

Create pre build event to copy files to assets folder in Android application

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}')

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')

Android Studio: How to see sources for android support v4 and v7?

In Android Studio support library appcompat (for ActionBar) is defined as Gradle dependency.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
That resolves to get v4 as well.
How to see source when clicking to into classes?
e.g. android.support.v4.widget.DrawerLayout
Currently Android Studio says
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
For ADT it was How to add source + javadoc for android-support-v7?
Following from the above research done by Paul Verest...
IDE: Android studio 1.3.2
It is a 2-step process: Consider this sample build.gradle
1) Add the following to your build.gradle (Module:app) - search the 2 //Add comments below.
apply plugin: 'com.android.application'
apply plugin: 'idea' //Add
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.mycompany.android.myapp"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.0'
}
//Add
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
2) Rebuild project.
After this point if you want to see source in Android Studio, it will pull up the source *.java instead of decompiled *.class
Thanks to Setu for hint. As I already had all sources before, I just added in app/build.gradle
apply plugin: 'idea'
idea {
module{
sourceDirs += file("E:\\Android\\sdk\\extras\\android\\support\\v4\\src\\")
sourceDirs += file("E:\\Android\\sources\\platform_frameworks_support\\v7\\appcompat\\src")
}
}
below dependencies section and press "Sync project with Gradle Files"

Gradle in Android Studio

I'm trying to set up Double Espresso, but that's probably not relevant here. What I'm trying to do is to set up a project in Android Studio using Gradle.
I'm very new to Gradle and build tools in general, though I've successfully used Maven before. Despite an hour of searching I can't find an answer to a very simple question.
In Jake Wharton's instructions it says
No more fumbling with local jars or dependency conflicts. Pull it in with one line:
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r3'
Pull in where? Where do I put/execute that command to import the project? In the command prompt? Do I put it in one of the scripts?
Thanks for any help.
You have a build.gradle file inside your app folder. In that file you can configure your project, "dependencies" and other options. It's very similar to maven. You have another build.gradle file in your root folder from your project. This conf file is more general and call the other build.gradle file.
e.g I have in one of my projects
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.acostela.example"
minSdkVersion 17
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:20.+'
compile "com.android.support:gridlayout-v7:18.0.+"
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'commons-net:commons-net:3.3'
compile 'net.sf.opencsv:opencsv:2.3'
}
Dependencies here are similar to maven and the use in that tool of "/".
Gradle take libs from repositories in the same way of maven. In fact you can use the maven repository. You have a tab with the gradle sentence to download libraries.
http://mvnrepository.com/artifact/com.squareup.assertj/assertj-android/1.0.0

Categories

Resources