Android Studio: Setup external project as module - android

I need to use an external project(ResuableProject) as module in multiple projects(one of which is ProjectOne). What I've done here so far is:
Added following in settings.gradle of ProjectOne
include ':ProjectOne', ':ResuableProject'
project(':ResuableProject').projectDir = new File(settingsDir, '../ResuableProject/module')
Added following in build.gradle
dependencies {
compile project(':ResuableProject')
}
After that gradle sync without any error and in Project Explorer the module is included but when I use any class from ResuableProject it gives me an error that class cannot be found until I click on class name and it give me an option to add dependency on ResuableProject. When I select the option the class is accessible and it seems all working fine.
But when I Build the project and try to run it gives me this:
Error:Execution failed for task ':ProjectOne:compileDevDebugJava'.
> Compilation failed; see the compiler error output for details.
Error:(10, 30) error: package <packagename of ResuableProject> does not exist
Am I missing something or do I have to include source directory of ResuableProject in ProjectOne, if yes the how?
Also is there a better way to use one common project across multiple apps? (not jar or maven cause I need to include source with every project and common project will be hosted in a separate git repo)
Previously I was using this approach with Eclipse (one common Library project) and including it in multiple apps.
UPDATE: Adding file system structure
workspace
----ResuableProject (Android Common stuff)
--------.git
--------module
----ProjectOne (Android App One)
--------.git
--------ProjectOne (module)
------------build.gradle
--------settings.gradle
----ProjectOne (Android App Two)
--------.git
--------ProjectOne (module)

It turned out that you can add ResuableProject but cannot use its source if build.gradle has value apply plugin: 'android' i.e. the type of the project is application.
I changed that to apply plugin: 'android-library' i.e. it must be a library and now everything is working fine.

Related

Android Library issue

I'm trying to create an Android library for two days already.
Realy need your help.
I have an existing App which I want to do as a Library.
According to Android developers documentation:
f you have an existing app module with all the code you want to reuse,
you can turn it into a library module as follows:
Open the module-level build.gradle file. Delete the line for the
applicationId. Only an Android app module can define this. At the top
of the file, you should see the following: apply plugin:
'com.android.application' Change it to the following: apply plugin:
'com.android.library'
I have done this step.
The next step is saying:
When you want to build the AAR file, select the library module in the
Project window and then click Build > Build APK.
I don't really understand how to build the AAR file.
Also in my library, I have others dependencies which I need to be in my Library.
I tried a lot of suggestions in StackOverflow but didn't find the answer.
Unfortunately, I didn't find a good example of creating an Android Library with dependencies.
I don't really understand how to build the AAR file
Just compile the library or use ./gradlew build.
The output will be stored under the library's subdirectory under build/outputs/aar.
I have others dependencies which I need to be in my Library.
The aar file doesn't contain the transitive dependencies.
I suggest you publishing your library into a public or private maven repository.

aar dependency error in my android project using android-maven-gradle-plugin

I have a problem when using the aar module dependency aar module as the below issue explains.
https://github.com/dcendents/android-maven-gradle-plugin/issues/65
I have a project such like a client-server structure.
It has 3 modules:
app (my server to provide some service) -- apk module
sdk (my sdk to provide some API to communicate with my app) -- aar module
common (something which should include both in my app module and sdk module ,such as utils,beans,etc. --aar module
I do not want to write it in my both app and sdk modules, debugging and modifying should work twice.
Any idea how I can resolve this?
I'm not sure exactly what you did wrong there, but I've created/modify those gradle scripts and they're working great: https://github.com/sensorberg-dev/gradle-scripts
I can see you're trying to "install" on your local maven. To do that, you have to add this line at the end of the build.gradle from the common module this line:
apply from: 'https://raw.githubusercontent.com/sensorberg-dev/gradle-scripts/master/publish-to-local-maven-repo.gradle'
and then add the metadata to the gradle.properties files, like this:
project gradle.properties:
POM_GROUP_ID=com.company.omg
POM_VERSION=0.0.1
POM_PACKAGING=aar
and common module gradle.properties:
POM_ARTIFACT_ID=common
then it's just call: ./gradlew clean installArchives

Error:(44, 13) Failed to resolve: org.achartengine:achartengine:1.2.0 [duplicate]

I have to create an application that makes extensive use of charts.
Reading the web I chose achartengine that seems to have everything I need.
I downloaded the jar file, I plugged in the libs folder, I selected "add to library" and I lunch the gradlew clean.
Result in the sources where I do the import of org.achartengine.xxxx I always returned the error that fails to resolve symbols .
Do you have suggestions?
Thank you
Andrea
I am able to use this library in my Android Studio project, this topic explains how to add AChartEngine repo to your project.
What I did:
Added following to project-wide build.gradle (one from the project root):
allprojects {
repositories {
...
maven {
url "https://repository-achartengine.forge.cloudbees.com/snapshot/"
}
}
}
For every module that uses the library, add this to its build.gradle (you may put this to the top-level build.gradle if it should be included in all modules):
dependencies {
...
compile group: 'org.achartengine', name: 'achartengine', version: '1.2.0'
}
Now I can use the library in the project, I see the classes in code assist popups and build runs as succeeds.
It seems like the new version (1.2.0) is not available for download anymore in the http://www.achartengine.org/ site. and that's why the gradle/maven method doesn't work (or the snapshot file was removed).
I succeeded using and adding it to my project by downloading the jar file from here:
https://github.com/ddanny/achartengine/files/460139/achartengine-1.2.0.zip

Gradle Android project dependency not working

I'm trying to migrate from maven to Gradle for an Android project and I am facing the following issue:
I have three projects i.e
root
- projlib
build.gradle
- projA
build.gradle
- projB
build.gradle
build.gradle
settings.gradle
Basically what I want to achieve is that projB depends on projA and projlib. projlib is a lib folder that compiles and generates a lib(jar) file. projA is an Android Application and projB is another Android Application that needs to reference code in projA. Right now what I have added in the projB build.gradle file is
dependencies {
compile project(':projlib')
compile project(':projA')
}
So say if there's a class
FooProjLib in projlib and
FooProjA in projA
Then In projB I can do
FooProjLib foo = new FooProjLib
which works fine
but when I do
FooProjA foo = new FooProjA
Gradle gives me package projA does not exist, what I have observed is that both dependency is resolved but only the lib can be reference and not the apk.
Does anyone have an idea how to solve this?
You can't do exactly what you want. projA can't build an application (i.e. an APK) and also have other things depend on it. projB can only depend on projA if projA is an Android library, meaning in its build file you have this declaration:
apply plugin: 'android-library'
instead of
apply plugin: 'android'
Of course, this means that projA won't build an APK, but will build an AAR instead.
If projA needs to also be an APK, you'll have to restructure things such that the common code that's in projA that projB also needs is moved out to a shared library. If both projects are similar, perhaps you could have just one module for them and use project flavors to differentiate them; it's hard to say whether this is a good approach without a lot more information.

Android Gradle build with sub projects

I am currently in the process of converting one of our projects to Gradle from maven. The folder structure is as follows:
gitRoot
settings.gradle
build.gradle
ProjectA
build.gradle
src/main/java
Libraries
SomeLib (git submodule)
ProjectBRoot (git submodule)
settings.gradle
build.gradle
ProjectB
build.gradle
src/main/java
Libraries
FacebookSDK/facebook
build.gradle
src
So already it looks complicated. But the idea is that ProjectB is a library project and it should be able to be built and packaged separately, which is why it has its own settings.gradle and as far as i can tell it seems to be working ok, i have it building and its finding facebook just fine.
The ProjectB/build.gradle contains this line
compile project(':libraries:facebook-android-sdk:facebook')
The ProjectBRoot/settings.gradle contains this line
include ':ProjectB', ':libraries:facebook-android-sdk:facebook'
The gitRoot/settings.gradle contains this line
include ':ProjectA', ':Libraries:ProjectBRoot:ProjectB'
The ProjectA/build.gradle contains this line
compile project(':Libraries:ProjectBRoot:ProjectB')
When I run the build i get this error
The TaskContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead.
FAILURE: Build failed with an exception.
* Where:
Build file '/gitRoot/Libraries/ProjectBRoot/ProjectB/build.gradle' line: 17
* What went wrong:
A problem occurred evaluating project ':Libraries:ProjectBRoot:ProjectB'.
> Project with path ':libraries:facebook-android-sdk:facebook' could not be found in project ':Libraries:ProjectBRoot:ProjectB'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.652 secs
So my guess as to whats wrong is that facebook is not in a direct subfolder from ProjectB...but that doesn't matter when building within ProjectBRoot. This is probably due to the face that I am referencing ProjectB directly and not through the ProjectBRoot/gradle.build but I tried that and it also did not work. Can someone please help me out, I have looked through the documentation and it doesn't talk about multiple projects that have their own settings.gradle files and I think thats the thing that is messing me up.
Update:
So I followed Xav's answer and I am now able to build with the command line however i can't import/build with android studio. I know the problem is still with the facebook project. The error i get is that it could not configure ProjectB.
Gradle: A problem occurred configuring project ':ProjectA'.
> Failed to notify project evaluation listener.
> A problem occurred configuring project ':Libraries:ProjectBRoot:ProjectB'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.
The error is caused by the line
compile project(':facebook-sdk')
inside the ProjectB/build.gradle
settings.gradle must define all the modules. It won't load other settings.gradle found in the tree to load more module.
You'll have to either
define a module for the facebook SDK in the top level settings.gradle. Yes it's redundant with the other settings.gradle.
publish Project B somehow (as well as its dependencies so in this case the facebook SDK library), somewhere (a corporate artifact repository for instance) and access it from Project A.
While #1 is better, it makes the ProjectB -> Facebook dependency tricky as the path will be different depending on the settings.gradle used. One way to fix this is to split the module name/path from its actual location on disk. this is done inside the settings.gradle file.
In your top level settings.gradle file, do
include 'facebook-sdk'
project(':facebook-sdk').projectDir = new File('Libraries/ProjectBRoot/Libraries/FacebookSDK/facebook')
In the setting.gradle file inside your Project B, do the same with the different relative path:
include 'facebook-sdk'
project(':facebook-sdk').projectDir = new File('Libraries/FacebookSDK/facebook')
This makes both project setup define the same 'facebook-sdk' module located at the same absolute location on disk.
All projects depending on this module should just declare the dependency as
compile project(':facebook-sdk')
This isn't the answer that you are looking for, but...
I spent about 4 days trying to migrate my complex project structure (much like yours actually), into gradle. In the end, I ditched the entire sub-project concept (since in my case, it just didn't work), and I opted to use the local maven repository approach, as described in this article: http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds
So, in my Library projects that had sub-projects, I basically created complete library projects for each of those, and then I used the link above to publish those to the maven local repository. Then in the parent project, I removed any references to sub projects, and in the dependencies, I just referenced the published libraries. And then in my main project, I removed all references to sub-projects, and I referenced published maven local versions of my libraries.
In the end, it all works very well, but it did take some time to convert everything over. I had about 6 library projects with sub projects, that that sub projects, etc, and now everything works fine.
The thing that I dislike about systems like Gradle and Maven is that they really expect you to change "how" you structure your code/projects. So, if you are migrating to Gradle vs starting with Gradle, then the process can be quite frustrating. Once you figure it out though, the next time is much easier ;)
I would just have the app with the libraries all at the same level. You can still build and package each library based on the build.gradle file. I might be missing something, but the structure isn't as important as what's in the build.gradle files. You could still have projectB depend on Facebook, even if they are at the same folder level.
Had the same problem, in my case, I just forgot to add the project in settings.gradle. After that, it worked
I have finally managed to build the project (without gradle errors). Answer from Xavier was very helpful.
I spend almost 3 days trying to setup my project, I know I am very close to be finsihed but I have UNEXPECTED TOP-LEVEL EXCEPTION at very last step of gradle build process (dexDebug).
My project setup is very similar to Stoyan's however I have multiple android-library projects that are referencing android-support libraries. I suggest that if you have problems with compiling your top root project (error saying support android is already added) than you need to move the jar into separate android library project (decompose/separate into stand alone instance).
Example
--| ProjectARoot
--| ProjectA (where main/java etc are)
--| build.gradle
--| settings.gradle
--| libraries
--| ProjectBRoot
--| settings.gradle
--| ProjectB
--| libraries
--| android-supports (android lib project)
--| libs
--| android-support-v4.jar
--| android-support-v13.jar
--| build.gradle
--| libA
--| build.gradle (referencing android-supports)'
Example build scrip for libA referencing android supports projects
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile project(':android-support')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
}
}
// top root settings.gradle
include 'ProjectB', 'android-support', ':ProjectA' (notice Project B is first than android-support and lastly Project A)
project(':android-support').projectDir = new File('libraries/ProjectBRoot/libraries/android-support')
project(':ProjectB').projectDir = new File('libraries/ProjectBRoot/ProjectB')
Currently when I run gradle build I get this error
:ProjectA:dexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
1 error; aborting
:ProjectA:dexDebug FAILED

Categories

Resources