how to merge two manifests in same module by gradle? - 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?

Related

How can I specify location of AndroidManifest.xml?

I'm porting a module from Eclipse to Android Studio/Gradle, and need to specify the locations of my sources, resources, and manifest:
sourceSets {
main {
manifest {
srcFile './AndroidManifest.xml'
}
java {
srcDirs = ["src"]
}
resources {
srcDirs = ["resource"]
}
}
}
Android Studio/Gradle seems perfectly happy with my java and resources entries, but balks at my manifest entry:
No signature of method: build_b4wnchd9ct4a5qt388vbbtbpz.sourceSets() is applicable for argument types: (build_b4wnchd9ct4a5qt388vbbtbpz$_run_closure2) values: [build_b4wnchd9ct4a5qt388vbbtbpz$_run_closure2#35290d54]
All of my googling and searching SO suggests that this should have worked.
Arctic Fox, 2020.3.1. Not sure which version of Gradle came with it.
Ahh, figured it out. Leaving here in case someone else has the same question.
Add an android.sourceSets.manifest.srcFile entry to your module's build.gradle file:
android {
...
sourceSets {
main {
manifest {
srcFile './AndroidManifest.xml'
}
}
}
}
or simply:
android {
...
sourceSets.main.manifest.srcFile './AndroidManifest.xml'
}
My biggest mistake was not putting the sourceSets directive inside the android directive.

Android Studio does not show the res directory

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

Gradle: how to specify two sourcesets of library and add dependency in project

I try to specify two different sourcesets of a gradle library and add a sourceset specific dependency in a project.
The idea is to do something like this:
The library build.gradle file:
apply plugin: 'com.android.library'
android {
...
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java {
srcDir 'src'
}
res {
srcDir 'res'
}
}
exclude_fr_it{
manifest.srcFile 'AndroidManifest.xml'
java {
srcDir 'src'
}
res {
srcDir 'res'
exclude 'res/values/values-fr'
exclude 'res/values/values-it'
}
}
}
...
}
...
In the Project A, I want to include the main sourceset:
dependencies {
compile project(':explore_layout')
}
In the Project B, I want to include the build that use sourceset exclude_fr_it.
dependencies {
compile project(':explore_layout') library 'exclude_fr_it'
}
I tried to understand the documentation. But I don't see how this can be done...
Question 1
Doc
Question 2
This is not a direct answer to the sourceSets question. But you may not need to use the sourceSets feature at all.
If you simply want your app to include a subset of the resources in your custom android library, you can accomplish that with a configuration block in your app's build.gradle. (In fact, the library does not need those special configuration blocks for the 'main' and 'exclude_fr_it' sourceSets at all.)
This is possible because of the 'resConfigs' property.
In particular, suppose your library has resource configurations for three languages: 'en', 'fr', and 'it'.
Then, in project A, which includes all of the languages from the library, you simply use this in build.gradle:
dependencies {
compile project(':explore_layout')
}
But, in project B, which you want to exclude the custom library's 'fr' and 'it' resources, you use the 'resConfigs' property within a configuration closure to include only a subset of resources like this:
dependencies {
compile project(':explore_layout') {
android {
defaultConfig {
resConfigs 'en'
}
}
}
}
I have achieved this using distribution to mavenLocal of a specific sources specification. I have a library project that has some java.awt code that I want to omit so I can include the project as dependency to an android project (which doesnt support java.awt)
In your library you will have something like this:
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'maven-publish'
sourceSets {
main {
java {
srcDirs = [ 'src' ]
}
}
// android source set excludes portion of source that
// references java.awt
android {
java {
srcDirs = [ 'src' ]
excludes = ['**/skunkworks/**', '**/awt/**']
}
}
}
// this task compiles sources defined by custom sourceSet 'android'
task androidJar(type: Jar) {
from sourceSets.find {'android'}.output
}
publishing {
publications {
myLibrary(MavenPublication) {
groupId = 'foo.bar'
artifactId = 'mylib-android'
version = '1.0'
artifact androidJar
}
}
}
Then use the tasks->publishing->publishToMavenLocal target to compile the custom jar to your ~/.m2/repository.
Now in the android project I reference like this:
repositories {
mavenLocal()
}
dependencies {
implementation 'foo.bar:mylib-android:1.0'
}

Android Project Structure is incorrect; only one build.gradle

I have an incorrect project structure. I need a top-level build-gradle, and a module on the same level that contains its own build.gradle.
See picture of how it is organized now. What you see is almost two different levels merged into on.e The build.gradle here is the only one in the project. The one you see has the note that it is the top-level version.
What is the correct way to straighten this out? Do I need to reimport or can I reorganize it?
Other info, I have Gradle 2.10 installed.
EDIT: MORE INFO
Usually I have my top-level Gradle file that contains something like this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
But in the setup above, without having that second Gradle file, where do I put the other info ... for example:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven {
url "https://jitpack.io"
}
}
android {
defaultConfig {
// edited
}
dependencies {
// edited
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apply plugin: 'com.google.gms.google-services'
When I run the program, I get this error:
Error:A problem was found with the configuration of task ':checkDebugManifest'.
> File 'C:\--\src\main\AndroidManifest.xml' specified for property 'manifest' does not exist.
Is this related?
This way is still assuming a flat hierarchy without the extra module asked by OP, but since it's based on my own Eclipse to AS migration I know it worked... for me.
To recognize eclipse defaults without moving the files you need this:
android {
defaultConfig {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
test.java.srcDirs = ['src/test/java', 'build/generated/source/debug']
}
This will most likely allow you to use both eclipse and Android Studio with the same folders in place.
The second way is about not changing gradle but moving folders so gradle finds things where it expects to.
move AndroidManifest.xml, it must go into src/main
move res into src/main/res
move src/com into src/main/java/com (can you confirm where is your com folder currently?
You can either move files or direct gradle to where they are, it's your choice - but don't do both. The only step I don't remember is the build/generated/source/debug for test, I can't remember if I used that because I use groovy or if it was another eclipse maven/AS gradle mismatch.
It's because Gradle looks for AndroidManifest in a default place --> App/src/main/AndroidManifest.xml
You can define where Gradle can search for your AndroidManifest.
How to tell Gradle to use a different AndroidManifest from the command line?
select "android" from the drop down menu instead of "project"

Adding Android Library to Existing Project on Android Studio 0.3.6

I am trying to add a library, https://github.com/edmodo/cropper, to my Android project. I am following the methods described here: How do I add a library project to Android Studio?
but the Android Studio has changed since then and I can no longer "Import a Module". I can only add a new one. Here are the steps I have tried:
Copy the library into a folder named libraries.
Open Module Settings, and I am presented with this screen
I click the + sign to add a new module and am then presented with this screen
I choose to fill in the content root
The rest of the fields autofill to this
The next step...
I change the package
I hit next and now my project looks like this
Any idea on the correct way to add an external library in Android Studio 0.3.6+?
I don't use the wizard. Usually I edit the gradle files.
Create a structure like this:
- Blunka
build.gradle
- cropper
build.gradle
src
res
settings.gradle
In settings.gradle:
include ':Blunka', ':cropper'
In cropper/build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion XX
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
In Blunka/build.gradle add:
dependencies {
// Libraries
compile project(':cropper')
}

Categories

Resources