Android Gradle - Building library module generates unexpected .aar - android

I am currently working on setting up a multi-module project, with the following structure:
Project Root
|- app/
|- src/
|- build.gradle
|- lib
|- src/
|- build.gradle
|- build.gradle
|- gradle.properties
|- settings.gradle
While the library module builds as expected, I get the following build error for the app module:
A problem was found with the configuration of task ':app:prepareProjectRootLib10Library'.
> File 'C:\projects\Project Root\lib\build\outputs\aar\lib-1.0.aar' specified for property 'bundle' does not exist.
As far as my build script goes, I do not think I am doing anything wrong with my build scripts. At the same time, when I look for the generate .aar for my library project, I instead find a lib-1.0-SNAPSHOT.aar file instead, which explains the problem. While manually renaming that file to match what the build script expects fixes the issue, I need a solution that does require manual work. Is there a way to modify the build script such that the application module can get the library project files it needs?
If it helps, below is app/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 16
targetSdkVersion 20
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
androidTest {
java.srcDirs = ['src/androidTest/java']
resources.srcDirs = ['src/androidTest/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile project(':lib')
compile 'com.android.support:support-v4:20.0.0'
}
And this is lib/build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 16
targetSdkVersion 20
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
androidTest {
java.srcDirs = ['src/androidTest/java']
resources.srcDirs = ['src/androidTest/resources']
res.srcDirs = ['src/res']
assets.srcDirs = ['src/assets']
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0'
}

Related

Gradle unable to compile NDK files

I got the source of a project (An android application) from github and I am trying to launch it, the project is called "RtkGps+" it's an Anroid application to that use a C++ libary called Rtklib to play with GPS and Raw measurements.
My Android environment configuration is bellow,
Gradle version : 3.1.3
NDK revision : 19.0.5232133
I'm unable to compile the project, I got the errors bellow :
As shown in the the capture there are many errors, that I want to enumate them,
So the first one is this error where the NDK_PROJECT_PATH=null
SIMPLE: Error while executing process C:\Users\Yazid\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\Yazid\Documents\Dev\Android\RtkGps-master\RtkGps-master\jni\Android.mk NDK_APPLICATION_MK=C:\Users\Yazid\Documents\Dev\Android\RtkGps-master\RtkGps-master\jni\Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=0 APP_PLATFORM=android-21 NDK_OUT=C:/Users/Yazid/Documents/Dev/Android/RtkGps-master/RtkGps-master/build/intermediates/ndkBuild/release/obj NDK_LIBS_OUT=C:\Users\Yazid\Documents\Dev\Android\RtkGps-master\RtkGps-master\build\intermediates\ndkBuild\release\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
The second one is saying that the Android.mk file is not supported
SIMPLE: Android NDK: WARNING: Unsupported source file extensions in C:\Users\Yazid\Documents\Dev\Android\RtkGps-master\RtkGps-master\jni\Android.mk for module rtklib
I tried adding gradle code from this answer : https://stackoverflow.com/a/50707270/6306138
The Gradle file of the project.
buildscript {
repositories {
mavenCentral()
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
jcenter()
google()
}
dependencies {
implementation group: 'cz.msebera.android' , name: 'httpclient' , version: '4.4.+'
implementation 'com.android.support:support-v13:27.1.1'
implementation 'com.google.gms:google-services:3.2.1'
implementation 'com.google.android.gms:play-services-gcm:11.0.4'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.karumi:dexter:5.0.0'
implementation 'ch.acra:acra:4.5.0'
implementation 'commons-net:commons-net:3.6'
implementation 'org.slf4j:slf4j-api:1.7.25'
implementation 'com.infstory:proguard-annotations:1.0.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
android {
sourceSets.main.res.filter.exclude '**/proj4/jniwrap/org/**'
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
compileSdkVersion 26
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "gpsplus.rtkgps"
minSdkVersion 21
targetSdkVersion 26
multiDexEnabled true
ndk {
abiFilters = []
abiFilters.add('armeabi-v7a')
abiFilters.add('x86')
abiFilters.add('x86_64')
abiFilters.add('arm64-v8a')
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDir 'libs'
}
// instrumentTest.setRoot('tests')
}
externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}
packagingOptions{
doNotStrip '*/mips/*.so'
doNotStrip '*/mips64/*.so'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
debug {
debuggable true
jniDebuggable false
renderscriptDebuggable false
}
}
productFlavors {
}
}
Neither of the "errors" mentioned are errors. NDK_PROJECT_PATH=null is normal behavior for Studio, and the second is just a warning.
Without seeing your Android.mk or the full error message (the line immediately after the one you included tells you what went wrong) I can't tell you how to fix the warning, but like the warning says, one of your source files has an unrecognized extension so ndk-build didn't know how to build it.

Include generated Jar and aar as a dependency in Android project

I have a AndroidStudioSupport Project which has modules.
1. module 1 takes care of Android UI part
2. module 2 is a library project which generates .aar file
3. module 3 is a java project which generates jar file
Now I want to build the project in a way that
1. First build jar project
2. Then copy generated jar into libs of library project as a dependency
3. Then build aar file
4. Then copy generated aar into libs of UI project as a dependency
5. Finally generate .apk file
Please suggest me what changes do I have to do in my gradle files
jar project gradle file
apply plugin: 'java'
sourceSets {
main.java.srcDirs = ['src/main/java']
main.resources.srcDirs = ['src/main/java']
test.java.srcDirs = ['tests/java']
test.resources.srcDirs = ['tests/resources']
}
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it)}
}
}
library project gradle file
apply plugin: 'com.android.library'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':java_project')
}
task copyToLib(type: Copy) {
from configurations.compile
into 'libs'
}
build.dependsOn(copyToLib)
android {
publishNonDefault true
compileSdkVersion 21
buildToolsVersion '23.0.2'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('test')
}
buildTypes {
debug {
jniDebuggable true
}
}
}
This current implementation doesn't include jar from my java project

How to use buildship for android?

I have a dummy project, which I succeed to compile through buildship plugin in eclipse IDE.
This is my local.properties file:
sdk.dir=C:/Asta/altro/adt-bundle/sdk
This is settings.gradle file
rootProject.name = 'testgradle'
This is my build.gradle file
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.testgradle"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
lintOptions {
abortOnError false
}
}
sourceCompatibility = 1.6
targetCompatibility = 1.6
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.10'
}
Despite I got the apk fully compiled, eclipse is not integrated: still seeing missing libraries and giving more than a 100 errors! All the libs are perfectly managed by gradle in \build\intermediates\ and assembled into apk, but the eclipse IDE is not "live". I would like to use gradle to download and explode the libraries and then to inform eclipse and let it to make the apk with its own builder.
Buildship can be used only to run Android tasks (assembleDebug).
During the build process it will load dependencies and tell if there're some errors.
To load dependencies into Java classpath, to see errors in Eclipse and resolve imports you can either manually add .jar files to your Java Build Path or use this Gradle plugin: https://github.com/greensopinion/gradle-android-eclipse.
It generates classpath for Eclipse project and you just import it into the IDE.
To run Gradle tasks you can create a Run configuration (named "Gradle Project") and put there your task and Working directory.

Built apk contains unneeded resources

Having an issue where the apk being generated by our Gradle build contains a bunch of unnecessary files inside a test/resources directory
Our application build gradle contains several local module dependencies which are built as android libraries.
I know there is packagingOptions field in Gradle but you can't exclude directories as far as I can tell. Excluding each file isn't an option. Anyway, I would like to know the root cause for this.
Application gradle build:
apply from: "${rootDir}/android_application.gradle"
dependencies {
compile project(':local-lib1')
//... etc
compile project(':local-libN')
testCompile libraries['junit']
}
android {
defaultConfig {
versionCode 1
versionName "1.0"
multiDexEnabled true
}
}
And android_application.gradle:
apply plugin: 'com.android.application'
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
compileSdkVersion android_sdk_version
buildToolsVersion android_build_tools_version
defaultConfig {
minSdkVersion android_min_sdk_version
targetSdkVersion android_target_sdk_version
}
buildTypes {
}
}
All modules are structured like so:
module
|
--AndroidManifest.xml
--build.gradle
--assets/
--res/
--src
|
-- main/
| |
| --java/
| --resources/
|
-- test/
|
--java/
--resources/
Why are test/resources being included in apk? How can I exclude them?
Edit (to show lib gradle):
Example lib gradle:
apply from: "${rootDir}/android_library.gradle"
dependencies {
compile 'com.android.support:multidex:1.0.0'
// ...etc external deps
// test deps
testCompile libraries['logback-classic']
testCompile libraries['junit']
testCompile libraries['mockito-core']
// etc...
}
android {
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
}
}
android_library gradle:
apply plugin: 'android-library'
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
compileSdkVersion android_sdk_version
buildToolsVersion android_build_tools_version
defaultConfig {
minSdkVersion android_min_sdk_version
targetSdkVersion android_target_sdk_version
}
buildTypes {
release {
}
}
}
I am building with gradlew.bat -x test build
The test/resources included in the apk contains all the files in the corresponding test/resources for each lib. The size of directory (uncompressed) is 10mb so adds unreasonable amount to size of overall apk.
Try the resource shrinking. As per Google Documentation: "The Gradle build system for Android supports
resource shrinking": the automatic removal of resources that are
unused, at build time, in the packaged app. In addition to removing
resources in your project that are not actually needed at runtime,
this also removes resources from libraries you are depending on if they are not actually needed by your application
android {
...
buildTypes {
test {
minifyEnabled true
shrinkResources true
...
}
}
}
Think I've solved the issue.
I had in app build.gradle and each library build.gradle:
android {
sourceSets {
main {
resources.srcDirs = ['src']
}
}
}
Changed to
android {
sourceSets {
main {
resources.srcDirs = ['src/main']
}
}
}
I guess gradle was taking every resources dir under src/ and merging them into one - http://tools.android.com/tech-docs/new-build-system/resource-merging

play_licensing throws AIDL error with downloader_library for APK Expansion with Gradle

I tried to use the APK Expansion libs zip_file and downloader_library (which requires play_licensing). zip_file worked fine since it has no dependencies, but play_licensing throws an error when I try a Gradle sync.
Android Studio spits out this error log:
Information:Gradle tasks [:app:generateGoogleDebugSources, :app:libs:downloader_library:generateDebugSources, :app:libs:play_licensing:generateDebugSources, :app:libs:zip_file:generateDebugSources]
Information:/Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/aidl/ILicenseResultListener.aidl:21 interface ILicenseResultListener should be declared in a file called com/android/vending/licensing/ILicenseResultListener.aidl.
Information:2 errors
Information:0 warnings
Information:See complete output in console
/Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/aidl/ILicensingService.aidl
Error:(19) couldn't find import for class com.android.vending.licensing.ILicenseResultListener
Error:(19) Execution failed for task ':app:libs:play_licensing:compileReleaseAidl'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Users/faiz/Projects/Android/android-sdk-macosx/build-tools/19.0.3/aidl -p/Users/faiz/Projects/Android/android-sdk-macosx/platforms/android-19/framework.aidl -o/Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/build/source/aidl/release -I/Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/aidl -I/Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/src/release/aidl -d/var/folders/y6/g0d5m3f94rxdvq5myp53d9k80000gn/T/aidl662748145109587430.d /Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/aidl/ILicensingService.aidl
Error Code:
1
Output:
/Users/faiz/Projects/Android/TogetherTime/app/libs/play_licensing/aidl/ILicensingService.aidl:19: couldn't find import for class com.android.vending.licensing.ILicenseResultListener
I haven't modified any of the libraries, but since they weren't built for Gradle, I copied over a build.gradle from another project and modified them accordingly.
downloader_library/build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:+'
compile project(':app:libs:play_licensing')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
defaultConfig {
minSdkVersion 4
targetSdkVersion 19
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
play_licensing/build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
aidl.srcDirs = ['aidl']
}
}
defaultConfig {
minSdkVersion 3
targetSdkVersion 19
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
I'm currently using Android Studio 0.5.1 and gradle 0.9.0 with build tools 19.0.3
I had the same problem and found solution for it. Just create a new package with the name com.android.vending.licensing under the aidl folder and move your aidl files there:
P.S. you also have to add empty <application /> tags to AndroidManifest.xml of play_licensing and downloader_library modules. And compile project(':PlayLicensing') to the dependencies tag of the build.gradle of the downloader_library

Categories

Resources