Building stand-alone static library for Android using Gradle/Android Studio - android

I've transitioned my NDK based app from command-line ant builds to Android Studio.
Android Studio is layered on top of Gradle.
Gradle invokes CMake to build the C++ code.
What I am looking for is a way to build just a set of static .a files for the Android targets armv7, arm64, x86, etc.
Android Studio projects are geared towards complete apps.
Is there a way to have Gradle (or AStudio) build static libs (.a) only, without building an app or shared library?
I'm using all the latest Android stuff under linux:
Android Studio 3.0 canary4.
NDK 15.0.4075724

You can build a static library in Android Studio and Gradle in this 2 ways:
Adding in your module's Android.mk include $(BUILD_STATIC_LIBRARY), using either ndk-build or gradle-experimental plugin.
Adding in your CMakeLists.txt
add_library(mylib STATIC
source_file1.cpp
source_file2.
... )
using Android Studio 2.3+ and adding in your module's build.gradle
android{
defaultConfig{
externalNativeBuild{
cmake{
\\ add cmake parameters here if you have some
}
}
}
}
and then press Synchronize Gradle Files.
But always remember that you wouldn't be able to package (add) the builded static library in your app.apk, and hence you wouldn't be able to load the cpp code from the java code. You can only load shared libaries in your android app.

Related

Releasing Android Library without NDK dependency

I am building a .aar Library that is partially written in C++ and uses OpenCV.
When i am assembling the Library i get a .aar with everything included and i can import it into a different project. When building the project i get the error, that i still need the correct ndk in the project which imports the .aar. This is not good if i want to give the library to others.
Shouldn't the JNI part of the library already be compiled so i don't need the NDK if i already have the .aar?
How can i remove the dependency from the .aar?
Edit:
The Error is No version of NDK matched the requested version 20.0.5594570. Versions available locally: 17.3.6528147, 21.0.6113669
I include the .aar by putting it in the libs/ folder in the module and adding '*.aar' to the build.gradle fileTree implementation.
The reason for it was the automatic stripping of the native part of my library.
Explaination here: https://stackoverflow.com/a/62320616/4284799
Solution 1: Change gradle version in the projects build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
}
Solution 2: install a ndk and set it in the local.properties
ndk.dir=/path/to/android-sdk/ndk-bundle

Android is it possible compile more that one cmake project with externalNativeBuild?

So we have an native cmake project that we would like to compile with android studio/gradle by utilizing the externalNativeBuild. The cmake of the project uses find_package to "find" other cmake packages that are then included to the target. These are packages that I need to compile (for android) and install or course. I would like to do that in gradle just like with the main project but it seems that one cannot define more that one cmake project to be build in gradle.
Am I correct here? How can we achieve such a build?

Importing Android System app into Android Studio/IntelliJ IDEA

Let's say I want to develop some system apps and contribute to AOSP.
Lets take the Music app, for example:
https://android-review.googlesource.com/#/admin/projects/platform/packages/apps/Music
I clone the Repo from Gerrit, and there is no sight of any Gradle files to be used with IntelliJ IDEA/Android Studio, just a Android.mk file.
How do I compile and test the app?
How do I import the app into some IDE?
How do I debug the app?
You could create your own gradle project and declare Android.mk in it as mentioned in "How to use custom Android.mk with new gradle build system" by notsopopularguy
Add this to your build.gradle file. This will cause the ndk-build to run as part of project build using the specified .mk file.
android{
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
}
This is also what is officially documented in "Add C and C++ Code to Your Project", section "Link Gradle to your native library "
This

Is there any way to specify gradle version when cordova creates android project?

I am using latest Cordova 5.4.0 and when I perform 'cordova platform add android' cordova creates gradle.build for gradle 2.1.1. I need to implement some JNI C++ code for android plugin, but this version doesn't support NDK well and I can't add NDK related section to the build file to introduce NDK support and set module name. Adding such section causes build errors (Gradle sync failed: Gradle DSL method not found: 'ndk()')
According to this manual http://ph0b.com/new-android-studio-ndk-support/ I am trying to add following section
android.ndk {
moduleName = "mymodule"
}
So, is there any way to force cordova to generate gradle.build for fresh gradle version like 2.8 or 2.9 ? Just changing gradle version in the build file from 2.2.1 to 2.8 doesnt work because there is new 'model' root namespace and probably other changes required.
Probably it is impossible at the moment. The best way to integrate cpp code to cordova android project is create separate JNI project, build libs for all possible platforms using ndk-build and integrate libs to your plugin in plugin.xml this way
Btw, it makes impossible debugging C++ code from Cordova project using Android Studio.
I also ran into this problem, this question helped but had to change a few things.
Here are the steps that fixed it for me.
Create gradle.properties (if it doesn't already exist) in the root folder of your app (where AndroidManifest.xml is)
Add the following line to it:
android.useDeprecatedNdk=true
In your build.gradle file, add "ndk" block under the android { defaultConfig { ... } }
android {
...
defaultConfig {
...
ndk {
moduleName "native"
}
}
}
You may have to change your gradleVersion if Android Studio prompts you to.
Rebuild your project and it should compile.

How to include http library in Android Project using M preview in Eclipse ant build

With Android M Google has deprecated support for http library. To support existing applications use this library they have documented that using GRADLE build we can add support to http library by using following parameter.
android{
useLibrary 'org.apache.http.legacy'
}
How can we achieve the same in Eclipse environment using ant build ?
Just copy
your Android SDK folder/sdk/platforms/android-23/optional/org.apache.http.legacy.jar
file to your project's Libs folder

Categories

Resources