Importing Android System app into Android Studio/IntelliJ IDEA - android

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

Related

How to import OpenCV 4.5 in Android Studio

The fact that this question is still occasionally getting new upvotes makes me think I'm not the only one in this kind of situation, so I've decided to write a step by step explanation of what worked for me in hope that others might find it useful in the future. Everything is based on this video, but I think it's better to write a proper guide here in case it gets taken down.
If you're looking for help with OpenCV 3.4, this is what I was following before trying version 4.5.
This is the complete procedure that currently works for me with OpenCV 4.5.2 on Android Studio 4.1.3.
In your project click on File > New > Import Module... and select the /sdk directory inside your OpenCV download. Give it a meaningful name and wait for the procedure to finish: the directory you selected should have been copied in the root of your project where the default /app directory resides;
open the Project Structure (for example by clicking on File > Project Structure...), then go to Dependencies (on the left), click on app and on the + icon in the Declared Dependencies tab (not the one in the Modules tab);
click on Module Dependency and select the checkbox for the OpenCV SDK that you imported earlier. You should now see it in the list with the other dependencies, so click on Apply and OK to exit from the Project Structure;
open the build.gradle file of your app module, copy the values of compileSdkVersion, minSdkVersion and targetSdkVersion, then paste them in the build.gradle file of the OpenCV module replacing the default ones so they match exactly. You can also update the sourceCompatibility and targetCompatibility fields to JavaVersion.VERSION_1_8;
finally, sync your project with Gradle files.
To check if it works, add this snippet to your code, for example in MainActivity:
if (OpenCVLoader.initDebug()) {
Log.d("myTag", "OpenCV loaded")
}
The sdk directory for opencv version 4.5.3 is "opencv/sources/modules/java/android_sdk". After doing each steps on the https://stackoverflow.com/a/65571017/9486652, i got some errors and i solved it by commenting or deleting the
'arguments "-DANDROID_STL=#ANDROID_STL#"' line which is found inside build.gradle of the opencv module.
externalNativeBuild {
cmake {
// arguments "-DANDROID_STL=#ANDROID_STL#"
targets "opencv_jni_shared"
}
}
I had success to import opencv 4.5.2 in android studio. It is not so difficult. The key is to provide the correct OpenCV_DIR path for CMake to install the OpenCV.
Download SDK https://opencv.org/releases/
Import module by File > New > Import Moduleā€¦
Add OpenCV module in setting.gradle
include "opencv"
project(":opencv").projectDir = file("sdk")
In application build.gradle, add OpenCV_DIR cmake argument under
android > defaultConfig > externalNativeBuild > cmake
arguments "-DOpenCV_DIR=" + file('../sdk').absolutePath + "/native/jni",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared"
Add OpenCV module dependency in application build.gradle
implementation project(':opencv')
Link OpenCV library in your application cmake.
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
......
target_link_libraries(${PROJECT_NAME} ${ANDROID_OPENCV_COMPONENTS})

Flutter plugin gradle subproject is not resolved by external app project

I have a Flutter plugin project whose Android side is setup to include a third-party aar file via gradle module. In order to get the plugin's example app to build, I have to add include ':aar-module' to the app's settings.gradle file.
The issue arises when I try to include my plugin in the pubspec.yaml of an external Flutter app. When I try to build, I'm told that it can't find the third-party aar module. However, if I try to include the aar module in the settings.gradle of the Flutter app, it can't find it:
Project with path ':aar-module' could not be found in project ':my-plugin'.
I tried following the solution on this GitHub issue: https://github.com/flutter/flutter/issues/17150, but it requires hard-coding the module path.
So my question is: How do I include a gradle submodule when its parent is included in a Flutter app (via pubspec)?
If it helps, the settings.gradle for the plugin project is the same as the default with the inclusion of the aar module:
include ':aar-module'
rootProject.name = 'my-plugin'
For the time being, I'm just importing the aar again into the external Flutter android app. It's not ideal because things will break if the aars are out of sync.
I'll refactor the plugin to be part of my Flutter app for now; Having it as a standalone plugin would have been nice but I can't waste too much time on it.

How to force CMake to use response files in an Android NDK project?

I have an Android project that has an NDK component, configured to be built with CMake. And depending on where the project dir is located on a drive, I may get a "Command line too long." error when CMake is trying to build this project. It's because CMake assembles huge command lines listing all the .cpp files in a project.
I read that CMake has a mechanism called "response files" to work around this, but I can't find a way to enable them in an NDK project. A little advice, please?
You can specify response file in CMakeLists.txt, or in build.gradle, add
android { defaultConfig { ...
externalNativeBuild {
cmake {
arguments
"-DCMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS=1",
"-DCMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS=1" ,
"-DCMAKE_C_RESPONSE_FILE_LINK_FLAG=#",
"-DCMAKE_CXX_RESPONSE_FILE_LINK_FLAG=#",
"-DCMAKE_NINJA_FORCE_RESPONSE_FILE=1"
}
}
}}

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

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.

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.

Categories

Resources