Android AAR native library with header only prefab - android

I've been trying to create a Android AAR native library with header only prefab without success. We distribute other native libraries we build in Android Studio as prefabs in AARs and include them in other Android native applications. We'd like to do the same with a header-only library. We use Android Studio and cmake for the native part.
The docs clearly states:
Each dependency can expose at most one Prefab package, which comprises one or more modules. A Prefab module is a single library, which could be either a shared, static, or header only library.
One obvious workaround would be to build a dummy library and include it in the AAR so that prefabPublishing would work (it breaks when there no library file).
Another would perhaps be to add to the AAR (zip) a manually created prefab directory but I read somewhere that this could be a problem with signing.
Yet another would perhaps be to create the perfab directory structure manually, which would be ok, and included in verbatim in the AAR (if that would work?).
We use Android Studio 4.1.1 and gradle plugin 4.1 and create our AARs as described in the docs referenced below.
buildFeatures {
prefabPublishing true
}
prefab {
mylibrary {
headers "src/main/cpp/include"
}
}
https://developer.android.com/studio/build/native-dependencies?buildsystem=cmake
https://google.github.io/prefab/
Thanks,
/Johan

Since Android Gradle Plugin 7.1 you can define headerOnly true.

Related

Publish to MavenCentral with private submodule

Our Android project is a monorepo containing couple of app modules, and even more library modules. One of the libraries should be also accessible to other developers so we have decided to deploy it on MavenCentral. Let's call it the library L. This library however also contains dependency within the monorepo to another module (let's call it a module m). In order to test whether the library L is correctly accessed by the other developers, we have created a demo Android project. But when trying to include the dependency of the publish library L, generally all works great except that the module m could not be resolved in the demo Android project.
Understandingly, the module m could not be resolved due to it being available neither on MavenCentral nor as an AAR file. We cannot publish the module m on MavenCentral since it should not be available for others to get, but including it as an AAR file, doesn't work either. Terminal throws an error:
Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error).
So the question comes - how can a project - that includes a submodule that can't be exposed, be published to repository like mavenCentral?

Equivalent of 'compileOnly' in Android.mk

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.

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.

Android Studio Java Library Module vs. Android Library Module

My desired end result is to have a Project that can output different productflavors of Mobile, Tv, and Wear apps, that share the same code-base. So I would like the codebase to be a dependency for all three app-types, where each app module would only contain activities and interaction with the codebase.
My first idea was to add a "Android Library Module" to my newly created project, but I noticed that this is really just a new app module with its own resources and everything. I would like the codebase to function more like a "Java Library", but it needs access to packages like "android.graphics.Color".
So in short, is the correct way of achieving this result to use a java library that has a reference to an android sdk or am i just going about this the wrong way?
Continuation of this question at:Does an Android Library need a manifest ,app_name,Icon?
There's no in-between. If you want access to Android APIs, then the library needs to be an Android library so that the build system can properly link it in to dependent projects. It's true that Android Libraries have resources and other things you may not need, but you can ignore those bits and treat it essentially as a plain Java library if you wish. Even if you're not using resources, you may find useful the ability to specify AndroidManifest.xml attributes to be merged into the dependent app.
The Android Library project doesn't build a fully-fledged APK as its output; it generates an AAR, which is conceptually similar to a JAR archive, but has resources and meta-information useful to Android projects.
Supplemental answer defining terms
The Android Studio documentation defines a module as follows:
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.
So basically a module is a subproject in your bigger project.
Types of modules
Android app module - The app module is usually what you are working with in a normal project. When you compile it you get an APK file that will run on a device. Here are the different types of app modules that exist:
Phone & Tablet Module
Android Wear Module
Android TV Module
Glass Module
Library module - The purpose of a library is to share code. For example, you could have a project with a couple different app modules. The common code that they both use could be located in the library.
Android Library - In addition to Java code, this allows you to also include Android resource files and a manifest. If you are making an Android project and are wondering what kind of library to use, then choose the Android Library. When compiled it creates an AAR (Android Archive) file.
Java Library - This only allows you to include Java code files, no Android resource files. This is useful for cross-platform code sharing. When compiled it creates a JAR (Java Archive) file.
Google Cloud module - This type of module is the Google Cloud backend for communication with your client side app.
One additional point that I've not seen well documented: An android library module can have a dependency on another android library module or java library module, but a java library module cannot have a dependency on an android library module.

Categories

Resources