Can't add custom Library Project with Android Studio - gradle

I've created an Android Project with Android Studio, and usually everything just works fine.
However, whenever I try to add a new custom Android Library Module or just a project with an Android Library Module I get the following error:
Failed to import Gradle project: Could not fetch model of type 'IdeaProject' using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
A problem occurred configuring project ':MyLibrary'.
A problem occurred configuring project ':MyLibrary'.
Failed to notify project evaluation listener.
Main Manifest missing from C:\Users\cku\AndroidStudioProjects\MyApplicationProject\MyLibrary\src\main\AndroidManifest.xml
Consult IDE log for more details (Help | Show Log): Failed to import Gradle project: Could not fetch model of type 'IdeaProject' using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
A problem occurred configuring project ':MyLibrary'.
A problem occurred configuring project ':MyLibrary'.
Failed to notify project evaluation listener.
Main Manifest missing from C:\Users\cku\AndroidStudioProjects\MyApplicationProject\MyLibrary\src\main\AndroidManifest.xml
Consult IDE log for more details (Help | Show Log)
I've followed this answer and this answer with no luck.
Any help is appreciated, thx!

I believe the build.gradle file of the project you are trying to import do not specify the right paths because it can't find AndroidManifest.xml:
Main Manifest missing from C:\Users\cku\AndroidStudioProjects\MyApplicationProject\MyLibrary\src\main\AndroidManifest.xml
It is trying to locate AndroidManifest.xml inside the default folder structure which should be:
-Project
- src
-instrumentText
-main
-java
-res
AndroidManifest.xml
As you are importing a project I believe it has other folder structure, which you should inform in build.gradle like this:
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
And make the necessary changes to reflect the folder structure of the project you want to import.

Just recently (Android Studio 0.2.6) I've tried this again in another project, with no success at all. A co-worker has experienced a similar issue. Now the wizard for creating new modules simply appears blank. It seems to be a genuine bug of Android Studio (which in all fairness is still in early development).
The way to go is to create a new project with only a library in it and then copy its single module over to the existing project. Tinker with the gradle scripts until it builds on the command line. Then go ahead, close Android Studio and delete the .idea directory of your project and all .iml files (there should be one at the root of the project and one at the root of each module). Finally, re-open Android Studio and re-import the project using the gradle.build file in the project root.
Edit: Seems to have gotten more reliable with Android Studio 0.2.8

Related

Duplicate generated class gradle output (build/...) vs intellij output (out/...)

I'm using intellij (2019.1.1) for a java gradle (5.4.1) project and use lombok (1.18.6) for autogenerating code. Intellij puts generated sources under out/production/classes/generated/... and gradle puts them under build/generated/sources/...
This is fine, and I like that intellij keeps it's own build artifacts separate from gradles, however, intellij seems to look in both directories when running projects and it complains about the duplicate generated classes.
What is the best practice for using intellij with gradle and autogenerated sources? Do you:
tell intellij to output to the same directory as gradle (this
could lead to odd behaviour if a process outside of intellij updates
a file under build/)
tell intellij to perform all tasks with
gradle (i hear this is slower than intellij's make)
tell intellij
to simply ignore the 'build' directory (how do you even do this? and
why does intellij even care about 'build/' when it knows it outputs
to 'out/')
UPDATE: to clarify the situation, the issue is NOT with lombok autogenerated code, it is with hibernate-jpamodelgen. The problem remains the same (duplicate generated sources) but I want to clarify the it is the sources generated by jpamodelgen and not lombok.
UPDATE 2: I have tried the following configuration in an attempt to tell intellij where the generated sources live and also to tell intellij to ignore the build directory. Sadly, this did not work (still get duplicate class error on the generated source files).
apply plugin: 'idea'
idea {
module {
sourceDirs += file('out/production/classes/generated')
generatedSourceDirs += file('out/production/classes/generated')
excludeDirs += file('build')
}
}
UPDATE 3:
Tried the advice from M.Riccuiti and deleted build/, out/, .idea/, .gradle/ and reimported the gradle project but intellij is still seeing the generated sources in the build/ directory.
Here is an approach that finally worked for me. The trick is to notice that when gradle generates the classes, it puts them in:
build\generated\sources\annotationProcessor\java\main\com...
but intellij has the production sources directory set to "generated" in this case, the sources go to:
build\generated\sources\annotationProcessor\java\main\generated\com...
if you compile with gradle first and then use idea, you get both of them, which causes the problem!
To solve this, replace "generated" and "generated_test" in the intellij annotation processors "production sources directory " and "test sources directory " configuration with just a "/" this makes both gradle and intellij generate the sources in the SAME directory, overwriting each other as needed. Also make sure that the "store generated sources relative to" is set to "module content root" and REBUILD the application to clear out any other sources.
The solution I proposed in previous comment was working fine in IDEA 2018.3.x but after upgrading to IDEA 2019.1 I again got this duplicate class exception...
Below is a working solution to make this work with Gradle 5.x (tested with 5.4) and IDEA 2019.1 , to implement your solution #3 which I think is the best option (do not mix gradle & idea generated output directories, and do not delegate IDEA action do Gradle )
The key point is to use excludeDirs property from idea.module extension to make IDEA ignore generated sources managed by Gradle under build/generated/sources/...
ext {
// path to Gradle generated main sources directory
gradleGeneratedMainSourcesDir = "$buildDir/generated/sources/annotationProcessor/java/main/"
// path to Gradle generated test sources directory
gradleGeneratedTestSourcesDir = "$buildDir/generated/sources/annotationProcessor/java/test/"
// path to IDEA generated sources directory
ideaGeneratedSourcesDir = "$projectDir/out/production/classes/generated"
}
idea {
module {
// exclude main & test sources generated by Gradle from project source directories
excludeDirs += file(gradleGeneratedMainSourcesDir)
excludeDirs += file(gradleGeneratedTestSourcesDir)
// include generated sources directory managed by IDEA
sourceDirs += file(ideaGeneratedSourcesDir)
generatedSourceDirs += file(ideaGeneratedSourcesDir)
}
}
See complete sample project based on this configuration here : https://github.com/mricciuti/sample-springboot-gradle-idea
You can enter to IntelliJ Settings (Preferences):
Preferences | Build, Execution, Deployment | Build Tools | Gradle | Runner
Then you tick the checkbox Delegate IDE build/run action to Gradle
Finally, you clean and build again. The issues will be resolved.

Can't install Kontakt Android SDK using Gradle on Android Studio

I'm trying to install the Android SDK from Kontakt into a project in Android studio. I'm following the (seemingly basic) instructions on the Kontakt site here:
http://docs.kontakt.io/android-sdk/quickstart/#installing-the-sdk---android-studio
In brief, these steps say add maven { url 'http://repo.kontakt.io/repository/internal/' } in the repositories in your top-level build file, and add to add compile 'com.kontakt.sdk.android:library:1.0.5' in the dependencies in the main module build file.
However, when I try to sync my project with the updated files, it fails with an error message:
Error:Failed to find: com.kontakt.sdk.android:library:1.0.5
I tried with a totally fresh project and the same issue.
I'm guess this all has something to do with the fact that the instructions are written for Android's 0.12 version of the Gradle tools, but I'm using v1.1.0. I'm not across the detail of Gradle, so so any help would be appreciated.
My guess is that path to Kontakt.io public repository should be added to allprojects section instead of buildscript.
allprojects {
repositories {
jcenter()
maven { url 'http://repo.kontakt.io/repository/internal/' }
}
}
I am not 100% sure so don't shoot me:P
I think adding this line:
-->compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.kontakt.sdk.android:library:1.0.6'
Directly above the gradle project I wanted to compile fixed it.
Go to the Kontakt.io Android Proximity SDK https://github.com/kontaktio/kontakt-android-sdk on GitHub.
Click the Download ZIP button
Un-zip the file to a temporary location.
Add the jar file in your Android Studio Project.
File -> New Module -> Import .JAR/ .AAR Package -> Click Next -> Browse the SDK path (2.1.1) -> Finish (Take time to add in the project)
Then, Add module in dependencies
Right Click on Project Name -> Open Module Settings -> Under the list (Modules) -> Click on Dependencies -> Click on '+' -> Module Dependency -> Add Kontakt.io-android-sdk
Now Build your project It worked for me to kontakt io SDK. If anybody find the way to do this as Gradle build, please let me know.

Gradle/Android: Specify 'root' as a dependency of subproject

First of all, I've got an Android Studio project setup that works fine from within Studio. I can build it and run it on a device and in the emulator without problem and can also build a release APK.
I'm now trying to get to the point where I can have some command-line tools that will handle my build for me. All the reading I've done suggests that Android Studio uses gradle, but from what I've seen that's clearly not the case for my project -- as I had to create my build.gradle, settings.gradle files before gradlew would even attempt to build anything -- and yet Android Studio could still compile and deploy just fine.
Here's my project structure (as it sits physically on disk):
Root Project (Android library project, contains all the src, logic, UI, activities, etc.)
\--- WrapperProject (Android app, depends on the root project)
\--- SubProject1 (Android library, used by root)
\--- SubProject2 (Android library, used by root)
This is my root project's settings.gradle file
include ':SubProject1', ':SubProject2'
Likewise, here's my root project's build.gradle file. Note that this project is marked as an android-library and does not reference the WrapperProject in any way (should it?)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile project(':SubProject1')
compile project(':SubProject2')
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
I'm able to build my root project successfully using gradlew build -- but my ultimate goal is to build the APK from the WrapperProject that has a dependency on the root project. Can you suggest what my WrapperProject's build.gradle file should contain?
I'm here because of searching for similar answers, although my problem is primarily the opposite. I'm finding that the integration between command-line builds (gradlew) and Android Studio 0.2.2 is full of secrets.
One thing I see that stands out in your config file: this is pretty beta/alpha stuff. You have not revised your project to the latest Android Studio 0.2.0 notes. In particular, the 'com.android.tools.build:gradle:0.4' should be revised. Reference: http://tools.android.com/recent
There is an open source application that has the same basic need as yours. It is a Grocery Shopping app that is provided as a sample for CouchDBLIte https://github.com/couchbaselabs/GrocerySync-Android
it has to reference the existing projects. It seems to use a more explicit syntax for the dependencies than you did. example: "compile 'com.couchbase.cblite:CBLiteEktorp:0.7.4'" - including the full class paths. But they define two basic approaches, direct integration and artifact integration.
Sorry I can't provide a more precise answer to your syntax issue - but I'm sharing what I know so far.

Android Studio adding external JAR to the project

I'm trying all I can to add an external JAR file to my project but I just can't do it. Here's what I'm doing atm:
Copying JAR to libs folder
Hitting "Add as library" and selecting jar package option
Modifying build.gradle to include the new jar, I'm doing it simply like this:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Yet I'm still getting some weird error on Pre Dex phase:
Android Pre Dex: [StartAppUnifiedSDK-3.0.2.jar] UNEXPECTED TOP-LEVEL EXCEPTION:
Android Pre Dex: [StartAppUnifiedSDK-3.0.2.jar] java.util.zip.ZipException: error in opening zip file
Any ideas on what might be wrong?
the error message format shows that you are not using Gradle to build. You either loaded the eclipse project file (.classpath/.project) or had some existing iml files.
This means changes to build.gradle will have no effect since Studio uses the IntelliJ internal builders instead.
You need to import your build.gradle file instead to create a new Studio project.

Android Studio update today with regression

Started Android Studio today, and it popped up an update notice. Since I'm eager to explore new features and bug fixes, I clicked yes. Now the Project Structure dialog where you setup dependencies is missing, and has been replaced by a message:
We will provide a UI to configure project settings later. Until then, please manually edit your build.gradle file(s.)
So, I go out to try to figure out how to add ActionBarSherlock to my project as a project dependency, and have hit a brick wall. I have no idea how to import ActionBarSherlock project as a Library Project, and configure my project to use it. There isn't much documentation on this at Google, IntelliJ, and the Gradle docs assume a LOT of pre-existing knowledge of build systems.
There are plenty of posts on SO how to do this (although referenced with already missing Project structure dialog)
How do I add a library project to Android Studio?
Android-Studio ActionBar sherlock error with gradle
Installing ActionbarSherlock with Android Studio?
Problems importing project into Android Studio regarding ActionBarSherlock
In short:
Create a folder (i.e. /libraries) in your root.
Extract actionbarsherlock there
Create a new file build.gradle in actionbarsherlock root with the following contents
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
In your project's build.gradle
dependencies {
//NOTE that "libraries" is actually a folder name you created in step 1
compile project(":libraries:actionbarsherlock")
//Any other dependencies here
//Make sure there is no android-support-v4.jar in this build file
//as it is already contained in actionbarsherlock project
}
In settings.gradle
//NOTE that "libraries" is actually a folder name you created in step 1
include ':libraries:actionbarsherlock', ':<Your project name>'
EDIT
Last step: Close your project and open it back again for Studio to pick up the Intelli-sense data

Resources