I have been trying to configure Android Studio for NDk by following this article and this article. The following are the contents of my gradle-wrapper.properties
#Sat Aug 08 09:36:53 IST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
Following is the contents of build.gradle(project)
// 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-experimental:0.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
and finally the build.gradle(module)
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 22
buildToolsVersion = "23.0.0 rc3"
defaultConfig.with {
applicationId = "com.opaxlabs.nativetest"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 22
versionCode = 1
versionName = "1.0"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
}
}
}
android.ndk{
moduleName = "native"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
}
when I try to sync gradle files I get the following error:
Error:No such property: android for class: com.android.build.gradle.managed.AndroidConfig
the following paths are defined in local.properties
ndk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk/ndk-bundle
sdk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk
so it looks like I have missed something only I can't locate it.
Any help will be greatly appreciated.
In your build.gradle(module), the android.buildTypes block needs to outside of the android block. So it should look like this:
model {
android {
compileSdkVersion = 22
buildToolsVersion = "23.0.0 rc3"
defaultConfig.with {
applicationId = "com.opaxlabs.nativetest"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 22
versionCode = 1
versionName = "1.0"
}
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
}
}
android.ndk{
moduleName = "native"
}
}
Have you declare your NDK path in Local.property file ??
Looks like the environment path and the local.properties files are pointing to different locations:
PATH: C:\Program Files (x86)\Android\android-ndk-r9d
local.properties: C:\Program Files (x86)\Android\android-studio\android-ndk-r9d
Make sure which is right. You can keep the PATH and drop the local.properties declerations, and then try this command through the console: ndk-build -? to see if it was found in PATH
NDK Build option
ndk {
moduleName "SeePlusPlus" // Name of C++ module (i.e. libSeePlusPlus)
cFlags "-std=c++11 -fexceptions" // Add provisions to allow C++11 functionality
stl "gnustl_shared" // Which STL library to use: gnustl or stlport
}
Related
I am trying to develop a module from my C project with Android Studio. I am having the problem of
Plugin with id 'com.android.model.native' not found.
What did I do wrong?
Thank you!
Below is the App level build.gradle
apply plugin: "com.android.model.native"
model {
android {
compileSdkVersion 27
defaultConfig {
applicationId = 'com.example.androidthings.nativepio'
minSdkVersion 27
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
ndk {
//platformVersion = "21"
moduleName "libusb"
ldLibs.addAll(["log"])
CFlags.add("-Wall")
CFlags.add("-I" + file("src/main/jni/android").absolutePath)
CFlags.add("-O3")
}
sources {
main {
jni {
exportedHeaders {
srcDir "src/main/jni/libusb"
}
dependencies {
project ":dependency_module"
}
}
}
}
}
}
Here is the project level build gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This plugin was part of an experimental and old version of the current build system.
You should rewrite your gradle file to work with the current build system. Either let Android Studio generate the necessary additions by linking to your CMake file, or copy from a known-good sample like the HelloJNI sample:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
ndkVersion '21.2.6472646'
defaultConfig {
applicationId 'com.example.hellojni'
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildFeatures {
viewBinding true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
flavorDimensions 'cpuArch'
productFlavors {
arm8 {
dimension 'cpuArch'
ndk {
abiFilters 'arm64-v8a', 'armeabi-v7a'
}
}
x86_64 {
dimension 'cpuArch'
ndk {
abiFilters 'x86_64', 'x86'
}
}
universal {
dimension 'cpuArch'
// include all default ABIs. with NDK-r16, it is:
// armeabi-v7a, arm64-v8a, x86, x86_64
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
my module build.gradle
apply plugin: 'com.android.application'
model {
android {
// 编译SDK的版本
compileSdkVersion = 23
// build tools的版本
buildToolsVersion = "23.0.3"
//useLibrary = 'org.apache.http.legacy'
defaultConfig.with {
// 应用的包名
applicationId = "com.example.administrator.design"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
tasks.withType(JavaCompile) {
//指定编译JDK版本
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
}
android.ndk {
moduleName = "test"
ldLibs += "log"
abiFilters += "armeabi"
abiFilters += "armeabi-v7a"
abiFilters += "x86"
}
android.productFlavors {
create("arm7") {
ndk.abiFilters.add("armeabi-v7a")
}
create("arm8") {
ndk.abiFilters.add("arm64-v8a")
}
}
android.buildTypes {
release {
// 是否进行混淆
minifyEnabled = false
// 混淆文件的位置
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFiles += file('proguard-android.txt')
proguardFiles += file('proguard-rules.pro')
//resValue "string", "facebook_app_id", '"597651723652854"'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//testCompile 'junit:junit:4.12'
compile 'org.xutils:xutils:3.3.20'
...
}
my project build.gradle
buildscript {
repositories {
jcenter()
//mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
gradle wrapper
#Mon Mar 28 16:31:26 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
if I use "com.android.tools.build:gradle:0.20" in project build.gradle, and make some according changes it just works fine, But I want the new feature instant, run I have googled almost one afternoon.
Looks like you used an experimental gradle plugin version. The stable version 2.0.0 and the experimental version have some differences regarding the notation in the build.gradle file.
For example the android block needs to be outside the model block. And some attribute specifications are slightly different. Have a look at the Experimental Gradle Plugin User Guide, especially the red highlighted parts in the build.gradle example file.
Also I'm not really sure, but the non-experimental version still doesn't have full NDK support. Although it looks like the experimental version 0.6 does support the NDK as well as Instant Run, so you might use the version 0.6 instead of 2.0.
I'm trying to use Experimental Gradle Plugin for my NDK projects, but it do not compile ndk. Everything looks fine - Android Studio understands my C code, can jump from Java to native and so on, but when i run "Build", step of compiling ndk is skipped - i see no tasks like "compileNdk".
When I list all available tasks by
gradlew tasks
there is no taks like "compileNdk" also. "Libs" folder are empty too, so when I try to call native method, app crash with "UnsatisfiedLinkError".
So what am i missing? How to tell gradle to compile NDK?
My config is:
root build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.3.0-alpha5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
module build.gradle:
apply plugin: 'com.android.model.application'
model
{
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with {
applicationId = "com.kaspersky.experimentalcpp"
minSdkVersion.apiLevel = 14
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
android.buildTypes {
debug {
ndk.with {
debuggable = true
}
}
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
}
}
android.sources {
main {
jni {
source {
srcDirs 'src/main/jni'
}
}
}
}
android.ndk {
moduleName = "test_experimental"
stl = "stlport_static"
cppFlags += "-Isrc/main/jni".toString()
ldLibs += ["log"]
ldLibs += ["android"]
}
android.productFlavors {
create("arm") {
ndk.abiFilters += "armeabi"
}
create("arm7") {
ndk.abiFilters += "armeabi-v7a"
}
create("fat")
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
}
I'm using Android Studio 1.4.1 on Windows 7. ndk.dir points to "ndk-bundle" folder which was downloaded by AS.
Any help appreciated!
So after some research I figured out that my libs actually compiles, they just puts not in the "libs" folder, but in "build/intermediates/binaries/...". I dump result .so and figured that my JNI methods was not in there. The reason was that my JNI methods was declared in .h file. After renaming .h to .cpp all works fine (except debugging). Maybe it will help somebody else!
Recently i decided to take the new NDK plugin in Android Studio for a ride
The changes needed are explained here
My build.gradle was ported successfully. Today i decided that i need a copy task in order to copy files into my 'asset' folder.
Searching online says that i must use a 'preBuild.dependsOn taskName' line, which i am sure works Ok for normal Gradle, but fails in the new experimental one (With the introduction of the 'model' behaviour)
Now my build.gradle fails.
Error:(25, 0) Could not find property 'preBuild' on root project 'Android'.
I am sure that the task is defined correctly, since the error comes from the preBuild... line
Here is my build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.model.application'
task copyWebViewAssets(type: Copy){
from '../Common/WebView'
into 'src/main/assets'
include('**/*')
}
preBuild.dependsOn copyWebViewAssets
model {
compileOptions.with {
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
}
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with {
applicationId = "com.company.product"
minSdkVersion.apiLevel = 9
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
android.ndk {
moduleName = "native"
}
android.buildTypes {
release {
minifyEnabled = false
}
debug {
ndk.with {
debuggable = true
}
}
}
android.productFlavors {
// To include all cpu architectures, leaves abiFilters empty
create("all")
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
}
I would like to address again that this is related to the new experimental Gradle for NDK. I am currently on Android Studio 1.4, using Gradle 2.5.
Thank you for your support
Use compileTask instead of prebuild
it works for me.
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(changeNamespace)
}
I have experience with Android development on Eclipse, but I'm quite new to this gradle stuff.
I created a project on Android Studio and tried adding native library support. I followed the documents on android.com
Android NDK Preview
Experimental Plugin User Guide
I bumped into the following problem and I'm not even familiar with the terminology! It says: Unable to load class 'com.android.build.gradle.managed.ProductFlavor_Impl'
Suggestions did not solve the problem. How can I solve it?
Here are the contents of the build.gradle and app/build.gradle:
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
}
}
allprojects {
repositories {
jcenter()
}
}
app/build.gradle
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.0"
defaultConfig.with {
applicationId = "com.example.wofm"
minSdkVersion = 15
targetSdkVersion = 23
versionCode = 1
versionName = "1.0"
}
}
android.ndk {
moduleName = "wofmUtil"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles = getDefaultProguardFile('proguard-android.txt')
proguardFiles += 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
}
You should use minSdkVersion.apiLevel and targetSdkVersion.apiLevel instead of minSdkVersion and targetSdkVersion. And I also had a problem with getDefaultProguardFile() so I had to remove it.