I have been working on creating multiple product flavors. I have a requirement to include only specific set of file into the build from the source directory. I have used the include filters as given below
lean {
java{
srcDirs = ['src/main/java']
setIncludes(new HashSet(['com/zos/zid/CustomBuild.java']))
}
res {
srcDirs = ['src/main/res']
setIncludes(new HashSet(['drawable/**']))
}
manifest.srcFile 'src/main/AndroidManifest.xml'
}
The include filter works as expected for the Java sources, however it does not work for the Android resource files.
Related
I have an Android project checked out in Android Studio (4.0.1). The build.gradle contains the following sourceSets block (snippet shown below). It is meant to prevent certain values resource files from being included in the packaged aar (I only want to include values-en-rUS and values-es-rUS in the aar).
The aar gets packaged correctly and contains only values-en-rUS and values-es-rUS, as expected.
sourceSets {
main {
def resSrc = fileTree(dir: 'src/main/res').matching { exclude { details ->
(!details.file.canonicalPath.matches('.*values-(en|es)-rUS.*')
&& details.file.canonicalPath.matches('.*values-.*'))
} }
...
res.srcDirs = [ resSrc ]
...
}
}
However, Android Studio does not show me the res directory (the entire directory!) anymore in the "Project" sidebar when "Android" is selected, and the Java code files can no longer resolve the ids or layouts (e.g. findViewById(R.id.bottom_sheet_layout) -- they show as red squiggly lines).
How can I make it so that Android Studio continues to show the res directory in this case?
Thanks.
Try this:
sourceSets {
main {
def resSrc = fileTree(dir: 'src/main/res').matching { exclude { details ->
(!details.file.canonicalPath.matches('.*values-(en|es)-rUS.*')
&& details.file.canonicalPath.matches('.*values-.*'))
} }
res.srcDirs = [resSrc, 'src/main/res']
}
}
Note: If two or more resource directories contain the same resource file, an error occurs during resource merging.
For more information: https://developer.android.com/studio/write/add-resources
android gradle resources exclude file not working. e.g.
android {
sourceSets {
androidTest {
resources {
srcDir "../../src/foo/resources"
exclude "../../src/foo/resources/META-INF/foo.xml"
}
}
}
}
The foo.xml is not excluded.
version: com.android.tools.build:gradle:3.4.1
Your tree shows that foo.xml is excluded in androidTest. You might want to change it to the main android node android.
I have two manifests in one module, one is used as a library for the whole app, another is used as the module's manifest, then this module can run alone. Below is my gradle config:
sourceSets {
main {
if (configs.isIntegrate) {
//module as app
manifest.srcFile 'src/main/module/AndroidManifest.xml'
} else {
//module as library
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
so, when I add a new activity, I have to register twice.is this a way I can register
once in manifest below the main dir, then let the manifest that below the main/module dir merge it?
I am following the tutorial from this tesseract tutorial and had everything go smoothly up until my actual running of the Java code. When I try
new TessBaseApi();
It throws the following error
Error Code: 2
Output:
In file included from tesstwo/src/main/jni/com_googlecode_leptonica_android/box.cpp:17:0:
tesstwo/src/main/jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
#include <allheaders.h>
^
compilation terminated.
make: ***
I have looked into /jni/com_googlecode_leptonica_android/src/src and find the allheaders.h file there. I have a feeling my paths are wrong, but I've tried almost everything and no avail. What's the issue?
I also ran into this problem with Android Studio. After googling some more i found this issue.
https://code.google.com/p/android/issues/detail?id=74132
Apparently the NDK plugin generates it's own Android.mk file and ignore any existing one, so the recommended way is to run ndk-build to generate the native .so files.
When I used ndk-build in the tess-two directory it compiles just fine and the .so files is created.
How you can include native libraries in gradle and android studio is described in this post: Add pre-built .so files in project using Android Gradle plugin 0.7.3
This works for me: https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle
But do not delete libs directory!
Set compileSdkVersion, buildToolsVersion, minSdkVersion and targetSdkVersio to the same values as in project buil.gradle
I also change classpath 'com.android.tools.build:gradle:0.9.+' to classpath 'com.android.tools.build:gradle:1.0.+'
At some point Android Studio suggests to set
jni.srcDirs = []
leading to following sourceSets in the gradle.build of my tess-two library project
sourceSets.main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
jni.srcDirs = []
jniLibs.srcDirs = ['src/main/libs']
}
With correct src path entered here this actualy works
I'm not sure if it works for you but in my case, here's what I've done:
1. In common.h: change #include <allheaders.h> into #include <src/src/allheaders.h>.
2. In the library project build.gradle: add this
sourceSets{
main {
manifest.srcFile 'AndroidManifest.xml'
jni.srcDirs = []
jniLibs.srcDirs = ['src/main/jniLibs']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
Ppl, After struggling a day.. finally got the solution
In build.gradle of tess-two module add the below code:
sourceSets.main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
jni.srcDirs = []
jniLibs.srcDirs = ['src/main/jniLibs']
}
Main thing is please check manually weather all those file paths specified in above code exists in tess-two module!!
Check in which path "liblept.so" and other ".so" files exist in tess-two library. For me it was inside /tesstwo/src/main/jniLibs/armeabi-v7a . Hence i have made jniLibs.srcDirs = ['src/main/jniLibs'] in above code. Hope it helps !!
I'm trying to migrate my applications to use gradle, but I'm facing some problems including library projects.
My project tree is this:
My projects root
- MyLib1
-- res
-- src
-- libs
- MyLib2
-- res
-- src
-- libs
- MyLib3
-- res
-- src
-- libs
- MyAppBase
-- res
-- src
-- libs
- MyApp - full version
-- res
-- src
-- libs
- MyAppFree - free version
-- res
-- src
-- libs
With Eclipse I had the following dependencies
MyAppBase depends on:
-MyLib1
-MyLib2
-MyLib3
MyApp depends on:
-MyAppBase
-MyLib1
-MyLib2
-MyLib3
MyAppFree depends on:
-MyAppBase
-MyLib1
-MyLib2
-MyLib3
This organization worked well within Eclipse, but now with Android-Studio and gradle I'm having problems.
I've got the following build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
//compile project('../MyLib1') <- error
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 18
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 4
targetSdkVersion 14
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
How can I include the projects MyLib1, MyLib2, MyLib3 as a dependency so It will be compiled along with my project???
At present, all dependencies need to live under the project's root directory, so you'd need to set up your project to be rooted at the directory above MyLib1, MyLib2, MyApp, etc. This limitation will be lifted in the future; you can track its progress at https://code.google.com/p/android/issues/detail?id=56367. Your libraries would be library modules under that root, and your app(s) would be Android application modules. Each module has its own separate build.gradle file and can compile to a JAR (plain Java library), AAR (Android library, which includes code + resources), or APK (Android app).
I'm not sure if MyAppFree and MyApp are separate Eclipse projects; if they are, under Android Studio and Gradle I'd encourage you to combine them into one module that has free and paid flavors. Build flavors are designed explicitly to aid this sort of use case. See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors for more info.
UPDATE
In the comments below it looks like you have a very large number of libraries. In that you probably don't want to build them all from source all the time, or manage a project that has dozens of modules. In that scenario, keeping modules that don't change very often as separate projects makes more sense. Those projects can compile to JAR or AAR, which brings us back to your original question of how to make those work in Android Studio.
You could copy JAR files into a libs directory under your project root and link them in. I believe there are problems trying to do the same with AAR libraries; see https://code.google.com/p/android/issues/detail?id=63908 to track the progress of that. If you don't want to maintain multiple copies of the libraries, you could either try symlinking the directories (I think that will work), or you could set up a local Maven repository and have the side projects publish their artifacts to that. I don't have links with detailed instructions on that at my fingertips; you could start with http://www.gradle.org/docs/current/userguide/publishing_maven.html.
There will be a fair learning curve to getting a local Maven repo set up, but once it's done, you'll probably find that it solves your problem pretty cleanly, and if you're in a shop with multiple developers and would like an organization-wide Maven repo, perhaps with artifacts that are published to it from a build server, you can set that up.
Maybe go to the menu "Project Structure" (Ctrl+Shift+Alt+S) and add the desired dependencies to the correct modules.
In Android Studio Your Project Structure should be looking like this
YourProjectName
-libraries
--myLib1
--myLib2
--myLib3
-MyAppBase
--build.gradle( It will have dependency of all three libraries like mentioned in last)
--src
--res
-MyApp
--src
---main
----java
----res
----Manifest
---full
----java
----res
----Manifest
---free
----java
----res
----Manifest
--build.gradle ( It will have dependency of only MyAppBase because your MyAppBase is already having dependency of all of your three libraries so no need to include them again)
Now you can have your build flavors in this last mentioned build.gradle file like
buildTypes {
debug {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
release{
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.prodSigning // write your signingConfig
}
}
productFlavors{
full {
//required configuration for full version
}
free {
//required configuration for full version
}
}
Note your directory name and product falvours name in build.gradle file must be same so while compilation (from Built Variant tab available in Left panel or command prompt) it will automatically take the code/res/manifest from respective folder.
Above configuration will give four type of following build variants
debugFull,debugFree,releaseFull,releaseFree
You Can add dependency in build.gradle of MyAppBase like
dependencies {
compile project(':libraries:myLib1')
compile project(':libraries:myLib2')
compile project(':libraries:myLib3')
}
And in the MyApp module's build.gradle file like this
dependencies {
compile project(':MyAppBase')
}