Local CMake configuration from Android Studio - android-studio

When using CMake in a regular project, some variables (e.g library paths) can be configured via -D option or by using ccmake or cmake-gui The values are saved in the cache, and this provides for a local configuration that is specific to every user/developer of the code.
In Android Studio, CMake can be configured from Gradle, but I haven't been able to find an equivalent to the options above. The local.properties file can be read from gradle, but it's overwritten by AS.
Is there any way of setting CMake variables to local values from Android Studio?
Example: On a regular CMake project, I can add a line to my CMakeLists.txt like:
set(EIGEN_DIR /usr/local/include/eigen3 CACHE PATH "Eigen path")
And then two different developers may set that value to their particular systems (ie. /usr/local/include/eigen3 or /opt/local/include/eigen3) without affecting the project source code. However, in Android Studio, the only way seems to be from build.gradle, which is part of the project, and will get committed to the repositories.

I found this can be achieved by putting the variables with the desired value in the local.properties file, and then read them with the code from this answer:
Properties props = new Properties()
props.load(new FileInputStream(project.rootProject.file('local.properties')))
String conf_value = props['conf.value']
and then
externalNativeBuild{
cmake {
arguments "-DMY_CONF_VALUE="+conf_value
...
}
}

Related

Can't find Android Size Analyzer in Android Studio

According to https://developer.android.com/topic/performance/reduce-apk-size, I should be able to install Android Size Analyzer as a plugin, but nothing shows up.
Android Studio -> File -> Settings -> Plugin -> Marketplace -> search for "Android Size Analyzer"
UPDATE: 17th May 2022
After a lot of trials, errors & frustration, I was able to make it work. Here are the steps: (for MacOS)
Download the the plugin's zip folder from the Github https://github.com/android/size-analyzer/releases
Extract the zip and add the size-analyzer executable and the analyzer.jar to Users/<your_user>/Library/Android/platform-tools
(You can add the above files to any place but I bundled it with platform-tools because this location was already present in my $PATH variable. Wherever you place those two files, please make sure that this location is present in your $PATH variable else it won't work. Don't know how to set path variables? Please refer this https://techpp.com/2021/09/08/set-path-variable-in-macos-guide)
Now in your Android Studio Terminal, run the following command
size-analyzer check-project <your-path-to-project-directory>
(After above step, If the terminal complains like size-analyzer command not found, it means your $PATH variable is not set properly)
After analyzing the entire project, it shows output like this
The result analysis provides lots of suggestions and space saving options. Thank you Google for such amazing plugin : )
=================
OLD : 16th May 2022
The AS plugin Android Size Analyzer is deprecated! It's no more indexed on Plugin Market Place(You cannot install it directly from AS anymore).
I even tried installing the plugin from their GitHub Repo, but all in vain. When adding the plugin to studio from disk, the following error popped up.
Maybe you can try downgrading the studio and then install it.
Looks like, we'll be on our own for a while : (

CMake autogenerated files missing includes CMAKE_C_INCLUDE_PATH and CMAKE_CXX_INCLUDE_PATH

I have a complex build I'm trying to sort out on a new build server. Using CMake for creating the makefiles. Old version of CMake on a different server was 2.8.5. New version is 2.8.12.2. For some reason, the autogenerated CMakeDirectoryInformation.cmake files are missing the CMAKE_C_INCLUDE_PATH and CMAKE_CXX_INCLUDE_PATH, which specify the absolute location of the include headers available to that particular directory. Without that information, the source code in that directory that references headers via a local path (such as foo.h versus ../../../asdf/foo.h) won't compile. Any ideas why these are missing? As far as I can tell this is supposed to be autogenerated by CMake.
I found a useful command that got me further into my build:
SET(CMAKE_INCLUDE_DIRECTORIES_BEFORE ON)
But that didn't actually solve my problem. The real issue was that in one of my CMakeLists.txt files I had:
include_directories(
$[XXXX_INCLUDE_DIR}
)
The '[' bracket was causing the build to fail because that include directory was not being used. However, on the old build server, it had no issue with this square bracket.

How can I access sdk/ndk paths in my gradle build scripts?

In Android Studio 1.3.2, I have set sdk.dir and ndk.dir set in local.properties, but how can I access them within my gradle build scripts?
What I want to do is call ndk-build manually without having to hard code a path to the ndk-build, which now is in a known location at sdk/ndk-bundle/ndk-build. Are these path variables exposed, or, how can I expose them?
You can get the ndk.dir by android.ndkDirectory. Look at this answer to see how you can call ndk-build from gradle: https://stackoverflow.com/a/28566337/3836816

Project-independent directories settings in VC++

I have a Visual C++ 2012 Express project on Git that uses an external libcurl (not included in the repository).
I added the library by adding the corresponding paths on my machine (D:\libcurl\XXX) to Include Directories and Library Directories in VC++ Directories of Project Properties. So these settings are saved in the .vcxproj file.
If someone whose libcurl path is different from mine wants to build the project, he would have to:
Change VC++ Directories settings in Project Properties.
(then the project file will become modified, which is not what I want.)
Move his libcurl files to match mine (D:\libcurl).
(library path is forced to be D:\libcurl, not so flexible)
My question is: Is there a way to avoid this?
(In old versions of Visual C++, I can set the paths in the global VC++ Directories of the Tools->Options dialog instead. But the feature is deprecated in recent versions.)
I believe u can achieve this by using environment variables.
For example, you can add an environment variable called: LIBCURL
You can then set the LIBCURL to any path u want: D:\libcurl or X:\lib2.3b ...
no matter what ur library path is, u can always use the same VC++ Directories: $(LIBCURL)\XXX

How to run NDK samples?

Sorry to ask such a noob question, but the NDK documentation is wrong (r7b):
If you are developing in Eclipse with ADT, use the New Project Wizard
to create a new Android project for each sample, using the "Import
from Existing Source" option and importing the source from
<ndk>/apps/<app_name>/project/. Then, set up an AVD, if necessary, and
build/run the application in the emulator.
There is no "apps" folder, and the samples do not contain a "project" folder. So ... what is the correct way to run a sample?
Also, can I configure Eclipse to build the C++ portion of the code automatically?
Click File | New | Project...
Choose Android project, Next
Project Name: This is the project name shown in Package Explorer. It is just a string stored in the .project file that Eclipse creates. No file or folder is created with this name, and the output binaries do not seem to contain this name.
Click "Create project from existing source"
Location: choose the project's root folder, e.g. .../android-ndk-r7b/samples/bitmap-plasma
Click Next and choose your target API. Eclipse does not seem to allow this to be changed later, at least not from the GUI. I don't know why Eclipse does not simply get this information from <uses-sdk> in AndroidManifest.xml or from the "target" line in default.properties. I also don't know why the two may be different (e.g. in the bitmap-plasma sample, target=android-9 but <uses-sdk android:minSdkVersion="8"/>.)
Click next and consider changing the "Package Name" field which defaults to "your.package.namespace". However, the project will run fine if you do not change this field. Leave "Create Activity" and "Create a Test Project" unchecked.
Click Finish. Eclipse will create numerous extra files (e.g. .classpath, .project, project.properties) and folders (bin, gen, assets) alongside the existing code. That's in addition to the output folders created by ndk-build (obj and libs).
Eclipse won't build the native code by itself, but it will automatically deploy the native code (e.g. libplasma.so) if it is aware of it. After you build the native code on the command line, e.g.:
C:\...\android-ndk-r7b\samples\bitmap-plasma>..\..\ndk-build
Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi-v7a/gdbserver
Gdbsetup : libs/armeabi-v7a/gdb.setup
"Compile thumb : plasma <= plasma.c
SharedLibrary : libplasma.so
Install : libplasma.so => libs/armeabi/libplasma.so
"Compile thumb : plasma <= plasma.c
SharedLibrary : libplasma.so
Install : libplasma.so => libs/armeabi-v7a/libplasma.so
Right-click your project and choose "Refresh", otherwise Eclipse might fail to upload the native code when it starts the emulator.
Finally, to run the sample, right-click the project and choose Run As | Android application. See here about choosing which emulator is used.
See here about configuring Eclipse to build the native code automatically.
I am curious why NDK produces two *.so files with very different sizes for each ABI, e.g. it creates libs/armeabi-v7a/libplasma.so (15 KB) but also obj/local/armeabi-v7a/libplasma.so (63 KB). Anyone know the difference?
Instead of going the long winded way ( seasoned programmer love it - but for beginners). There is a way to build the native library in Eclipse and NDK.
1> First you need to make sure NDK path is correct in Windows -> Preference -> (tabs) Android -> NDK - If not set - point it to ndk directory.
2> Assuming you have you imported project as described above (New | Project | Create from existing code) - You right click the project and go to " Android Tools -> Add Native Support ..". If the path is set it will build the example. And then you can deploy it to the device or AVM by pressing the play button.
SOURCE
I'm working with Eclipse ADT and Android ndk 5b. My correct path to the project is android-ndk-5b/samples/project
When you import from existing source you have to choose a correct target, I use Android 2.3.3.
After, I've updated my project from command line, look this link, http://developer.android.com/sdk/ndk/overview.html
From the samples path write: android update project -p . -s and $Your_Path_Android_ndk/ndk-build
Finally, only run as android app in Eclipse and it's done
It seems that with Eclipse Juno, you can now
Click File | New | Project...
Under Android category select Android Project from Existing Code and click Next.
In Root Directory you can then browse and confirm your sample project location.
From here, Eclipse can automatically detect the jni and find the projects.
If this didn't work, go to terminal or command prompt and navigate to your project root directory (where you see jni folder). Then input
android update project -p . -t <your target level>
Here should be, e.g., android-9
After this, try again the initial steps.

Resources