How to set an app as also a Library in Android Studio? - android-studio

I've an app X that is also library for other apps. Its default state is as a Library. When I want to compile X as an app, I change that in Eclipse using Project > Properties > Android, and (un)clicking in Is Library.
How can I do this in Android Studio without editing the X build.gradle file line
apply plugin: 'com.android.library'
since this is not convenient and gives tons of errors for the other apps?
Thanks!
L.

Afaik this is the only way. The solution to giving tons of errors by other apps is to export the project as an .aar library, put it into libs folder of other projects and import it via build.gradle.

Related

How to get Android Studio to recognize my plain Gradle Java project?

I have a simple multi-module Gradle project with Java code. When I import it in IntelliJ Community, it asks me to select the build tool from among Maven and Gradle, and will properly recognize the Gradle modules and show me a Gradle tool window.
When I do the same in Android Studio, I don't get the build tool question, and it won't recognize the projects or show me the Gradle tool window.
How do I get this to work in Android Studio?
Simply Try This,
Go to File -> Project Structure -> Sdk Location
This You will get a option menu like this (the given image)
there you can update your gradle settings.
The answer, as far as I've been able to find out, is: Don't even try. Use IntelliJ instead. Installing it is most certainly quicker than trying to get Android Studio to do this.
How do you open the project in Intellij? Do you open the folder? Or do you open the build.gradle?
Just like a maven project (where you should open the pom.xml, not the folder) you should open the project using the build.gradle, not the folder.
Intellij will then do what you expect and import your gradle project
The least required to make it recognize the project is to add AGP, the Android Gradle Plugin:
buildscript {
repositories {
google()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
}
}
Then one can define Android modules with:
apply plugin: "com.android.application"
apply plugin: "com.android.library"
apply plugin: "com.android.dynamic-feature"
Then it will recognize the android {} configuration block (without this Gradle DSL configuration block it would not know what or how to build for Android - as it isn't a sane Android project). Just see the documentation linked, this has little to do with the default Java tooling.
Gradle 7.2 is currently required to build. Even if one cannot mix the plugins per module, one can have different plugins per module - or use different IDE, depending on the tooling of the module. How compatible this really is, is being determined by the dependencies used in the Java module. The fact that one can only use either tooling per single module dictates the layout to be applied.
I suspect that you will have to manually move some code from the successful build files in intellij to the android build files.

Incorporate 3rd Party Native Libs into Android Studio project

I am trying to setup an NDK library module structured very similar to the gen-libs module in the Android-NDK sample project. It identifies what I want in an Android NDK library module to allow me to incorporate 3rd party Native Libs:
src/main/cpp
CMakeLists.txt
Android manifest
build.gradle
I am NOT looking to add C or C++ source files to my app module.
Instead, I am looking to create an Android-NDK library module within Android Studio, so that I can incorporate 3rd Party Native Libs.
However, I am Unable to easily reproduce the structure of the gen-libs module in Android-NDK sample hello-libs.
More specifically, Android Studio does not provide any option to create a "New => Module => Android Library => Add NDK/C++ support"
I suppose I can recreate this NDK module manually, but before I do that, I would like to ask the community if, for a brand new project, is there any easy way to auto-create the gen-libs module structure from hello-libs beside manual method?
Feel free to use your copy/paste skills to produce the project manually. The wizard won't do it for you, at least for now.
You can also create an app project and switch it to library by changing one line in build.gradle.
You can also create a library module, right click on this module, and choose 'Link C++ Project with Gradle' from the popup menu (or from File menu).

Building tess-two into a project using Android Studio's gradle build

There are answers about getting the tess-two project integrated into an Android project within Android Studio, but many are out-dated and none used the current capabilities defined here:
Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI)
What specific steps would be required to make tess-two functionality a part of the resulting APK, using functionality within Android Studio, rather than external file placement, manipulation and command line tools? So taking the building, creation of *.so files into the IDE. Specifically using just Android Studio's integrated build system (Gradle) as described here.
1. Start a new project
2. Import Tess-Two into the project
2b. Add any needed plug-ins
3. Add code in the main activity to get native functionality
4. Configure Android Studio build so that native functionality is available
This is where the specifics are required
4. Configure Android Studio build for native functionality
4a. Link C++ Project with Gradle
First, check the tess-two project for build files. You may select CMakeLists.txt or Android.mk files. Both are currently supported.
In this case, I used ndk-build, which seemed like a good bet for integrating the native code.
For more information, see Android Studio documentation
4b. Manage Long Commands
In Windows, errors may be encountered if the command length grows too large. To prevent problems, use LOCAL_SHORT_COMMANDS AND APP_SHORT_COMMANDS in the Android.mk file.
The "e=87" error is what you are avoiding by doing this:
For more on that topic, see stackoverflow question about error 87.
4c. Add Module Dependency for tess-two
In File > Project Structure > Dependencies use the + to add the tess-two dependency:
4d. Build the Project and check .apk file for .so files
The build, which takes a long time, should complete now. Validate that the .apk file contains the .so files, created during the build. With the tess-two libraries in an static initializer, run the project on your Android device:

Add reference to CMake project in Android Studio

In my particular case, I implemented a cross platform project in C++11 that uses CMake. I initially designed this project to work on PC (Windows, Unix), but now I realized that I can use it on mobile platforms too, without re-implementing the business logic on every platform. The project uses standard libraries: Boost, Poco, OpenSSL, Protobuf.
After a few searches I came to conclusion that this is not even an usual mode to put together native and managed code in Android.
Is it possible to add reference in a Gradle project in Android Studio to a native project that uses CMake?
Can NDK adopt the project in a simple manner if I compile everything in command line (assuming I don't use Android Studio)?
If it is possible, is it recommended at all?
Is it possible to add reference in a Gradle project in Android Studio to a native project that uses CMake?
Since Google announced Android Studio 2.2 that comes with cmake plugin, so it's possible to reference CMake project to android project.
Can NDK adopt the project in a simple manner if I compile everything in command line (assuming I don't use Android Studio)?
In my experience, i compile NDK in separate way and then link the static/shared library (.so file) to be used in android project. I used NDK while developing PDF Reader using mupdf here
Hope this helps.
it would be possible with android studio 2.2, mainly the android plugin for gradle makes it possible. you could look at some examples in:
https://github.com/googlesamples/android-ndk/tree/master-cmake
android studio directly reads-in your cmake scripts -- there is nothing like desktop systems that generate project files for their native IDEs ( Visual Studio or XCode ); so you might have to modify your existing cmake files to eventually make it to work for android studio.
as long as you build to the correct APK, command line or IDE does not matter too much. If you intend to ship source code, having your project ready with an IDE would be attractive.
Please see CMaker_Boost, build the Boost with the CMake at a configure time. Now it is tested on the Linux and Android, gcc and clang. This can be included to the gradle project in the Android Studio. I hope this helps.

Sharing own module across projects Android Studio

I'm struggling badly with moving from Eclipse to Android Studio.
Basically, I get that an Android Studio project is more like a workspace and a module more like a project...
However, in the Android Studio start page you can only create projects, so how do you share a module (i.e. a project in eclipse terms) across projects?
Basically, I have a number of apps that use a shared library I've created, in Eclipse all I do is flag it as a library and in each project simply link to it.
I have absolutely no idea how to do this in Android Studio. The examples for creating modules seem to take you as far as creating a module for no real purpose other than to use it within one app.
I first imported my library as a project in android studio, but that proved pointless, thinking that was how to do it because I want it kept separate in my version control system.
I then created a temporary module inside my app, but then it stores it within the project and in my other apps I cannot find a way to import the modules, so I really don't see what the point of a module is when it's embedded in a project and can't be separated or referenced anywhere else.
Thanks for any help.
Consider your library project name common-lib
Open build.gradle of the projects to which you want to add library add add the following
dependencies {
compile project(':common-lib')
}
and sync gradle

Resources