首页
社区
课程
招聘
[原创]Ubuntu android studio ndk编译配置问题
发表于: 2015-5-1 16:23 7859

[原创]Ubuntu android studio ndk编译配置问题

2015-5-1 16:23
7859
最近刚使用android studio开发android的APP,由于我工作中的项目用到了native开发,所以需要在android studio中构建APP的ndk编译环境。首先,我使用的是android studio 1.1.0 版本以及ndk-r10d,这个版本必须使用ndk-r10d,至于为什么,可以到android官网查看一下资料。

ndk的安装过程百度应该可以找到,这里我们就不讲了。关键是在学习android studio过程中的一些gradle的心得以及一些问题。
下面是我的app模块下的gradle配置:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.big.pv.bigsuperuser"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        ndk {
            moduleName "onehook"//模块名称
            abiFilters "armeabi", "armeabi-v7a", "x86"
            cFlags "-std=gnu++11 -fpermissive -DDEBUG -O0"
//            ldLibs "-llog -landroid_runtime -lutils -lcutils -lart -ldvm"
            ldLibs "log","android_runtime","utils","cutils","art","dvm"
            stl "gnustl_static"
        }
//        sourceSets.main {
//            jni.srcDirs = []
//            jniLibs.srcDir 'src/main/libs'
//        }
    }
    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:22.1.1'
}

其实这个编译工具是java的,我们很容易的从ADK工具中找到他们的Declaration部分,就是对着code右键选择go to -> Declaration或者快捷键xxxx即可。
我们来一层层的分析吧。
第一行 apply plugin: 'com.android.application' 可以看出我们要构建的项目是一个android 的app
首先我们开始看第一个标签android{}他所在的包的位置是package com.android.build.gradle;
我们打开他的Declaration部分是如下的:

  // IntelliJ API Decompiler stub source generated from a class file
  // Implementation of methods is not available

package com.android.build.gradle;

public class AppExtension extends com.android.build.gradle.BaseExtension implements groovy.lang.GroovyObject {
    private final org.gradle.api.internal.DefaultDomainObjectSet<com.android.build.gradle.api.ApplicationVariant> applicationVariantList;

    public AppExtension(@com.android.annotations.NonNull org.gradle.api.internal.project.ProjectInternal project, @com.android.annotations.NonNull org.gradle.internal.reflect.Instantiator instantiator, @com.android.annotations.NonNull com.android.builder.core.AndroidBuilder androidBuilder, @com.android.annotations.NonNull com.android.build.gradle.internal.SdkHandler sdkHandler, @com.android.annotations.NonNull org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.BuildType> buildTypes, @com.android.annotations.NonNull org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.GroupableProductFlavor> productFlavors, @com.android.annotations.NonNull org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.SigningConfig> signingConfigs, @com.android.annotations.NonNull com.android.build.gradle.internal.ExtraModelInfo extraModelInfo, boolean isLibrary) { /* compiled code */ }

    public org.gradle.api.internal.DefaultDomainObjectSet<com.android.build.gradle.api.ApplicationVariant> getApplicationVariants() { /* compiled code */ }

    public void addVariant(com.android.build.gradle.api.BaseVariant variant) { /* compiled code */ }
}

看他继承自 extends com.android.build.gradle.BaseExtension,我们通过android{compileSdkVersion 22}里面的项compileSdkVersion再goto一下Declaration,可以发现android{这里的项都是来自BaseExtension继承的属性或函数},这样我们就可以顺藤摸瓜,找到所有的apply plugin: 'com.android.application'构建的声明解释,从找到我们想要的编译方法。

==================================================
下面是我遇到的一个关于NDK编译的问题,NDK这个项是在defaultConfig或buildTypes里,下面是NdkOption的代码:
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      f4dK9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8Y4N6%4N6#2)9J5k6h3q4H3j5h3y4Z5k6g2)9J5k6h3!0J5k6#2)9J5c8X3I4A6j5$3g2F1M7$3g2K6i4K6u0r3e0p5W2o6c8f1&6e0c8g2)9J5k6o6u0Q4x3X3f1H3
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.gradle.internal.dsl;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.build.gradle.internal.core.NdkConfig;
import com.google.common.collect.Sets;

import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

/**
 * DSL object for NDK-related settings.
 */
public class NdkOptions implements NdkConfig, Serializable {
    private static final long serialVersionUID = 1L;

    private String moduleName;
    private String cFlags;
    private Set<String> ldLibs;
    private Set<String> abiFilters;
    private String stl;

    public NdkOptions() {
    }

    public NdkOptions(@NonNull NdkOptions ndkConfig) {
        moduleName = ndkConfig.moduleName;
        cFlags = ndkConfig.cFlags;
        setLdLibs(ndkConfig.ldLibs);
        setAbiFilters(ndkConfig.abiFilters);
    }

    @Override
    @Input @Optional
    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    @Override
    @Input @Optional
    public String getcFlags() {
        return cFlags;
    }

    public void setcFlags(String cFlags) {
        this.cFlags = cFlags;
    }

    @Override
    @Input @Optional
    public Set<String> getLdLibs() {
        return ldLibs;
    }

    @NonNull
    public NdkOptions ldLibs(String lib) {
        if (ldLibs == null) {
            ldLibs = Sets.newHashSet();
        }
        ldLibs.add(lib);
        return this;
    }

    @NonNull
    public NdkOptions ldLibs(String... libs) {
        if (ldLibs == null) {
            ldLibs = Sets.newHashSetWithExpectedSize(libs.length);
        }
        Collections.addAll(ldLibs, libs);
        return this;
    }

    @NonNull
    public NdkOptions setLdLibs(Collection<String> libs) {
        if (libs != null) {
            if (abiFilters == null) {
                abiFilters = Sets.newHashSetWithExpectedSize(libs.size());
            } else {
                abiFilters.clear();
            }
            for (String filter : libs) {
                abiFilters.add(filter);
            }
        } else {
            abiFilters = null;
        }
        return this;
    }


    @Override
    @Input @Optional
    public Set<String> getAbiFilters() {
        return abiFilters;
    }

    @NonNull
    public NdkOptions abiFilter(String filter) {
        if (abiFilters == null) {
            abiFilters = Sets.newHashSetWithExpectedSize(2);
        }
        abiFilters.add(filter);
        return this;
    }

    @NonNull
    public NdkOptions abiFilters(String... filters) {
        if (abiFilters == null) {
            abiFilters = Sets.newHashSetWithExpectedSize(2);
        }
        Collections.addAll(abiFilters, filters);
        return this;
    }

    @NonNull
    public NdkOptions setAbiFilters(Collection<String> filters) {
        if (filters != null) {
            if (abiFilters == null) {
                abiFilters = Sets.newHashSetWithExpectedSize(filters.size());
            } else {
                abiFilters.clear();
            }
            for (String filter : filters) {
                abiFilters.add(filter);
            }
        } else {
            abiFilters = null;
        }
        return this;
    }

    @Override
    @Nullable
    public String getStl() {
        return stl;
    }

    public void setStl(String stl) {
        this.stl = stl;
    }
}


从代码中基本上可以解决NDK的编译配置问题了。但是我的项目里用到了APP_STL,可无论我怎么设置,他都不会在build后的ndk目录下的Android.mk内生成APP_STL,想请教一下,这个APP_STL该如何生成,顺便想问问如果定义NDK的目标版本?比方说android-19.我也找不到gradle内如何配置才能产生此编译项。

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回