I am trying to build an Android library project with native code. Per the "Known limitations" section under http://tools.android.com/tech-docs/new-build-system/gradle-experimental hybrid library projects are supported. But I don't seem to see the native so files being generated under the libs folder of the library aar file.
Here is how my project setup looks like
Followed steps in http://tools.android.com/tech-docs/new-build-system/gradle-experimental for ndk support
Added a library modules that statically loads the native so file and exposes capabilities via some methods
Added another module that uses the native module. Set the module dependencies to include the library project.
When I run this app I get an UnsatisfiedLinkError, which I expected as I see no native so files being generated in the aar file.
This is how my library build.gradle file looks like
apply plugin: 'com.android.model.library'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
}
android.ndk {
moduleName = "native"
cppFlags = ['-std=c++11']
stl = "gnustl_shared"
}
}
This is how my app module's build.gradle looks like
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
}
}
dependencies {
compile project(':mylibrary')
}
This is a common problem in gradle experimental 0.2.0 and 0.2.1. Had the same problem, upgraded to 0.3.0-alpha4 and it worked (just replace 0.2.0 with 0.3.0-alpha4 in your project's build.gradle). To upgrade you will need Gradle 2.6 (right click on a module/Open Module Settings/ select the Project submenu/ set Gradle version field to 2.6). Note that I am using Android Studio 1.4 RC3.
Related
How can I add a module of a local project as dependency to another project?
I tried the following:
Add to settings.gradle:
include ':app', ':module'
project(':module').projectDir = new File('/path/to/module')
Add to project build.gradle:
buildscript {
dependencies {
ext { compileSdkVersion = 31; minSdkVersion = 19; targetSdkVersion = 31 }
}
}
Add to app build.gradle:
dependencies {
implementation project (":module")
}
But this fails to build with error:
Build file '/path/to/module/build.gradle' line: 3
A problem occurred evaluating project ':module'.
Plugin with id 'com.some.lib' not found.
I can build the module project without any issues with ./gradlew publishToMavenLocal, but how can I build it automatically as part of the parent project?
The goal is to develop the module and test the changes immediately in an example app.
The way I do it now is publish the module to local maven and let local maven override the dependency in the example app, but I wonder if there is a more direct way that automatically builds the module and where I can even debug-step through the module while testing it in the example app.
Remove the space after colon : on line#2 in settings.gradle
It should be project(':module') but you have written project(': module')
I am trying to add a KMM shared module to my existing Android Studio project. I would like to use the compileSdkVersion and targetSdkVersion from project level build.gradle in the shared module, but I can't seem to use it.
Here's the code snippet from shared module build.gradle.kts:
android {
compileSdkVersion(30) // I want to use a project-wide variable here instead of '30'
defaultConfig {
minSdkVersion(23) // And here
targetSdkVersion(30) // And here
}
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
}
How can I do this?
You can create a Dependencies/Versions file in your buildSrc to specify versions, that can be reused by multiple modules.
For an example see https://github.com/joreilly/PeopleInSpace/blob/master/buildSrc/src/main/java/Dependencies.kt
I'm using Android NDK to build a C/C++ code base into a shared library. The NDK built the code successfully and generated the .so files. Then I want to put the prebuilt shared library (.so file) to my Android Studio project. The Android Studio version I'm using is 1.0.1. When I placed all prebuilt four .so files under the jniLibs folder in src/main/jniLibs as suggested by this article, and then I built the whole project in Android Studio, the build failed because it reported some "C++ STL header not found" error.
I thought it is just as easy as putting .so files under jniLibs folder to integrate NDK with Android Studio but it's not. Obviously the Android Studio gradle build system is recompiling my C/C++ source code even though I already built the shared library using command line NDK. Since NDK can successfully built the C/C++ code, Android Studio shouldn't recompile the code but should use the pre-existing .so files. Is this a known issue in NDK integration with Android Studio? How do I let Android Studio use prebuilt .so files instead of rebuilding C/C++ code from its own?
Here is the $PROJECT/app/build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
Additionally, here is the top-level build.gradle file in project root directory $PROJECT/build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
How to solve this issue to let Android Studio use the prebuilt shared library directly instead of rebuilding the C/C++ source code again? I believe this may be either due to a known issue in Android Studio with NDK or there is a trick in the gradle script used by Android Studio to avoid recompiling native code.
Android Studio does not officially support NDK yet and there might be some glitches in adding precompiled shared .so files. You can force Android Studio to disable building your shared lib files by adding this on build.gradle file
jni.srcDirs = [] //disable automatic ndk-build call
You can also include your Android.mk and Application.mk files. Here is a detailed video on how to build an NDK application on Android Studio https://www.youtube.com/watch?v=0fEtrekNcOo
I'm using Android Studio 0.2.2
I've imported an existing eclipse project, which has the classic eclipse ADT project
structure and added a gradle build script into it.
Let's name it ProjectA
Then I created a module in it, which has the new Android Studio structure,
And added it as library in File>Project Structure dialog.
Let's name it LibA
The problem is this - ProjectA is somewhy lacking the classpath of LibA and therefore the IDE's java parser fails to see classes from LibA, therefore I'm getting a lot of errors on
the IDE's side.
Gradle of course, compiles everything ok because the build process is no longer related to the IDE as it were in Eclipse.
Anyone got any pointers in the matter?
Thanks!
If you are trying to add a library, go to
File -> Project Structure -> Libraries -> click + then add the jar.
Once it has been added, it will ask you for the module which is dependent on jar. Select ProjectA from the list. That should link the library to Module.
After setup dependencies in build.gradle:
try "Tools -> Android -> Sync Project with Gradle Files"
Set up you configs like this:
/settings.gradle
include ':LibA', ':progect'
/project/build.gradle
apply plugin: 'android'
dependencies {
compile project(':LibA')
}
android {
compileSdkVersion 18
buildToolsVersion "18"
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}
How can I convert an existing Android project into an Android library project in Android Studio? In Eclipse, that is possible.
Actually, I want to convert an old Android project into an Android library project so that I can use existing code of that Android project to build a new Android project with minor changes in Android Studio.
In your module's build.gradle file (not the root project, if you use modules!), simply replace:
apply plugin: 'com.android.application'
// or, if you're on an old version
apply plugin: 'android' // note: this one is deprecated
...with:
apply plugin: 'com.android.library'
// or, if you're on an old version
apply plugin: 'android-library' // note: this one is deprecated
Note that recently, 'android' has changed to 'com.android.application', while 'android-library' has been changed to 'com.android.library'. Avoid using the old names in new projects.
After updating your build.gradle file, you should Sync Project with Gradle files (which is in the toolbar), as not doing it might result in errors and things not working correctly.
Android Studio will then update some files to indicate that the module is now a library; as this will be added into your .iml file:
<option name="LIBRARY_PROJECT" value="true" />
As you might already know, you will not be able to run your (now) library project -- you will need to include it into an app project.
If you're using Android Studio 1.0 and you are getting “Library projects cannot set applicationId”, make sure you do not have applicationId in your Gradle build file.
Looking at this document
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
I think all you have to do is add this to your build.gradle file,
Creating a Library Project
apply plugin: 'android-library'
From the link
Creating a Library Project
A Library project is very similar to a regular Android project with a few differences.
Since building libraries is different than building applications, a different plugin is used. Internally both plugins share most of the same code and they are both provided by the same com.android.tools.build.gradle jar.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 15
}
Changing to Library
Goto android project, where you need to change as Library module.
In build.gradle(:app) file,
change this line to
plugins {
id 'com.android.application'
}
to
plugins {
id 'com.android.library'
}
Delete the line for the applicationId in same build.gradle(:app) file
defaultConfig {
applicationId "com.project.example" //remove this line
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Now Sync Project with Gradle Files.
open project in file explorer,open project.properties and try changing android.library=true in project.properties
This is a late response, but I've been trying to do the same thing. None of the above seem to have done the job for me, but I found this that I think works:
Right click on the project name -> Mark Directory As (at bottom) -> Sources Root
I don't know the difference between Resources Root and Sources Root, and a bit of googleing to turn up the answer, but hopefully that's right. I just know a Library isn't supposed to build an apk, and after setting this option, it's not able to so I'm assuming it works.
If anybody else knows more than me, please say so!
If you make it with command line, like chanakya propose, you have to update it with:
android update lib-project \
--target <target_ID> \
--path path/to/your/project
see: http://developer.android.com/tools/projects/projects-cmdline.html#ReferencingLibraryProject
That work for eclipse, but not for android-studio since that update the build.xml.