Equivalent of 'compileOnly' in Android.mk - android

I have a pre-built java library as a compile time dependency for Android library project(AAR). So while building it in Android Studio, 'compileOnly' is used so that the same gets linked in runtime when deployed with an APK.
However, I should also write an equivalent Android.mk for the library project. I am not able to find any reference to include a prebuilt java library in Android.mk. Can someone help me on this part.
I tried using LOCAL_PREBUILT_JAVA_LIBRARIES attribute, but the system threw an error:
error: dont use LOCAL_PREBUILT_JAVA_LIBRARIES anymore LOCAL_PATH=xxx.
This kind of dependency linking may look strange. Let me give some insight into it also. Basically I am building an application that has one small part being developed by a third party. The setter interface APIs for the third party are shipped in by me in the form of a .jar file so that they use the same as a compile time dependency and build an AAR out of it.
Now the third party project is to be included in my project build(AOSP). This brings a dependency that their module should be compiled for the AAR to get generated and my project uses that AAR to generate the APK.
For the AAR to get compiled and built, I need to link the prebuilt java interface library that is shipped in by me(mentioned in the first step).
Need an equivalent of 'compileOnly' used in build.gradle.

Related

Android Studio is bundling stubbed library into the APK

I am using Android Studio to build a native C++ project. There is a dependency library (let's call it lib.so) which is required to be linked with the JNI library to resolve few of its symbols. However, I do not want this library to be bundled into the APK as this is a stubbed library and to be used only for the linking purpose. A proper lib.so with proper symbol definitions is already present on the Android device where I want to run the built APK.
I am importing the stubbed library lib.so to be linked with JNI library as:
add_library(lib SHARED IMPORTED)
set_target_properties(lib.so PROPERTIES IMPORTED_LOCATION "location_of_lib.so")
add_dependencies(native-jni lib})
target_link_libraries(native-jni lib)
This way the APK compilation is successful but the library is getting bundled into the APK.
I am using AS 4.0.1, NDK r19c, CMake 3.17. The same project used to work as expected, i.e. not bundle the library lib.so with the APK but somehow it started bundling it.
Any leads to debug this issue would be appreciated.
Try to use find_library for that. Put your prebuilt lib.so (bad name at least liba.so) to some path and add it to CMAKE_FIND_ROOT_PATH:
list(APPEND CMAKE_FIND_ROOT_PATH ${PREBUILT_LIBS_DIR}/${ANDROID_PLATFORM}/${ANDROID_ABI}/a)
example how it could look:
~/my-proj/prebuilt-libs/android-28/x86/a/lib/liba.so
Then find library and link it:
find_library(lib-a a)
target_link_libraries(native-jni lib-a)

Android build c++ with dependency

I am working on an Android application with NDK. My c++ code has some dependencies from c++ open source libraries, so I can download the source code of the dependencies and package them in my app. I have created my CMakeList files, but I don't know how to include the external library.
I would like to build the dependency to be able to use it in my own project, so the flow would be as follows:
cd external/library/path
./configure
make
build my app and link the built dependency
Is this possible? If so, how can this be done? Sorry if this is a dumb question, I'm new on C++ and Cmake.
The way I solved this was by cross compiling the libraries and adding them as static libraries. You can cross compile using the clang binaries included in the ndk directory, using the one that targets your app min sdk.

Difference between module, libraries, jar, library projects, gradle projects, aar and jar

I work for a company which has a android mobile application developed by an offshore team. I have access to GitHub repositories.
I am piecing together the android app block by block and it is giving me hell.
How do I distinguish between module, libraries, jar, library projects, gradle projects, aar and jar. Can someone please give me a practical definition. NOT out of a freaking google search. I have Google too.
PS yes I am a noob and not proud of it.
I'm just giving a brief description about each of these. I hope I'm clear.
Module : A Module is an component of your application that can build / test independently and re use it in another application if required.
Libraries : AAR files, JAR files etc.
JAR : Java library
AAR : Just like JAR, only difference is that it also contains android specific files like resources etc.
Gradle Project : Gradle is just a build system which is used by Android Studio to build the android project. Its very much powerful as compared to the build system which was used in Eclipse earlier.
Library Project : An Android library project is similar to an Android app project in that it also includes a project manifest file in the project’s root directory. Also, this directory contains src, res, and other directories that you also find when building an app project.
However, there is a significant difference. You cannot compile a library project into an APK file because a library project doesn’t describe an app. Instead, it provides reusable code and resources that contribute to an app when the app’s project and source code refer to the library project. At build time, this code and these resources are merged into the app’s APK file.
To explain more on this, let me give you an example :
Say you want to use a networking library volley for making API calls, now since this is an open source library from Google you can clone it making customisations as per your requirement.
You can make volley library as your Library Project, build it independently, unit test, etc.
Now say you started building an application where you need to make HTTP calls so you need to add volley library to your project. You have two choices for that :
Compile your library project volley, generate the aar file and add it your app Project.
Add Volley as module in your project. (If you choose this option you can make changes to volley library in same studio project since it will act as a component in your project)
Please let me know if something is not clear.
1.) Module
A module is a collection of source files and build settings that allow you to divide your project into discrete units of functionality. Your project can have one or many modules and one module may use another module as a dependency. Each module can be independently built, tested, and debugged.
There are 4 type of module in Android Studio.
Phone & Table Module
Android Wear Module
Android TV Module
Glass Module
2.) Support Library
The Android Support Library offers a number of features that are not built into the framework. These libraries offer backward-compatible versions of new features, provide useful UI elements that are not included in the framework, and provide a range of utilities that apps can draw on.
Support libraries provide a range of different features:
Backward-compatible versions of framework components.
UI elements to implement the recommended Android layout patterns.
Support for different form factors.
Miscellaneous utility functions.
3.) Jar file
JarFile is used to read jar entries and their associated data from jar files.
for more detail visit this : https://developer.android.com/reference/java/util/jar/JarFile.html
4.) Android Library Project
The Android team introduced a new binary distribution format called Android ARchive(AAR). The .aar bundle is the binary distribution of an Android Library Project.
An AAR is similar to a JAR file, but it can contain resources as well as compiled byte-code. This allows that an AAR file is included in the build process of an Android application similar to a JAR file
5.) Gradle and Gradle Project
Gradle is an automated build toolkit that allows the way in which projects are built to be configured and managed through a set of build configuration files. This includes defining how a project is to be built, what dependencies need to be fulfilled for the project to build successfully and what the end result (or results) of the build process should be. The strength of Gradle lies in the flexibility that it provides to the developer. The Gradle system is a self-contained, command-line based environment that can be integrated into other environments through the use of plug-ins. In the case of Android Studio, Gradle integration is provided through the appropriately named Android Studio Plug-in.
for more detail visit this : http://www.techotopia.com/index.php/An_Overview_of_Gradle_in_Android_Studio

How to use an AAR file with Android Studio 1.2

EDIT:
When I use the response of How to manually include external aar package using new Gradle Android Build System I have an error.
BASE:
I never use AAR file, with the release of Android Studio we can use it.
But I don't find any explanation to use it.
Do you know how to use it?
It's necessary to make some things like ask for the permissions or create the dependencies?
Thanks
AAR format is a jar file containing a compiled Android Library project. Here's some info on what is inside an AAR file
What this means for app developers is that instead of (the old way):
Downloading the source code of a library project
Building it in eclipse
Setting your application project to depend on the local library project
You can instead add the dependency specification to your project/app/build.gradle's dependencies block: compile 'fr.baloomba:viewpagerindicator:2.4.2'
Then gradle (on the next build) searches for the aar file in a central binary repository, downloads the library and allows you to use it's functionality in your project. ( ex: http://search.maven.org/remotecontent?filepath=fr/baloomba/viewpagerindicator/2.4.2/viewpagerindicator-2.4.2.aar )
You can also search the central repository's web interface for libraries to depend on. Once you've clicked on a library to add to the project, click "Gradle/Grails" under "Dependency Information" to copy the line to add to build.gradle.
In my opinion this is a huge improvement.
You don't need to add any permissions to the app, either way, adding a Library project dependency is a compile time thing, not runtime or user-facing.

Java module referencing inside Android studio

I'm trying to compile Java module in Android Studio.
I'm also referencing android.jar in the module. So, yes, module is supposed to be Java, but uses android.jar as compilation reference.
The version SDK for this android.jar is irrelevant (I think, but it is 19 something).
(You might ask why if I need android.jar don't I use Android module instead of Java?, well it is intermediary step to restructure our Android project).
The problem I'm facing:
error: cannot find symbol variable SDK_INT
apply plugin: 'java'
dependencies {
compile project(':ATTLogger')
compile project(':BandwidthController')
compile project(':iwpstack')
compile files('libs/android.jar')
}
You see – IT doesn't want to recognize some… code clearly present in the android.jar.
And yes it sees import android.os.Build; and doesn't complain about it.
Here is my bukd.gradle:
Please help if you happened to solve this nuisance I've wasted half a day on.
Thanks!
If it is a Java only code (without any calls to Android API) then you can make it a JAR without adding a dependency to android.jar. Otherwise, if you have calls to Android API, you should make it AAR module.
From http://developer.android.com/sdk/installing/studio-build.html#projectModules :
Java library modules contain reusable code. The build system generates a JAR package for Java library modules.
Android library modules contain reusable Android-specific code and resources. The build system generates an AAR (Android ARchive) package for library modules.
Android application modules contain application code and may depend on library modules, although many Android apps consists of only one application module. The build system generates an APK package for application modules.
So to fix your issue, you should skip this "intermediary step" and make it an Android module right away.

Categories

Resources