Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7888599
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: Android平台

2015-12-21 22:52:03

Ubuntu下为AndroidStudio编译并使用FFmpeg(二)AndroidStudio部分
1. 下载并导入项目
$ git clone

下载一个别人写好的ffmpeg example例子,
然后使用android studio的import project进行导入"android-ffmpeg-tutorial01"

Fig-1
2. 修改各种配置
1). 导入工程后,先要对项目进行sdk和ndk路径设置,指定本机的sdk和ndk安装路径;

2). 对app文件夹下的build.gradle添加:

        sourceSets.main {
            // Tell Android Gradle that no jni code need to compile, so we can avoid Ndk compile task,
            // then we will to compile use ndk-build in command-line.
            jni.srcDirs=[]
            jniLibs.srcDir "src/main/libs"
        }

修改后的整个文件如下:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23          //需要设置成自己的sdk版本,否则编译会报出错
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "roman10.tutorial.android_ffmpeg_tutorial01"
        minSdkVersion 19
        targetSdkVersion 23       //需要设置成自己的sdk版本,否则编译会报出错

        sourceSets.main{
            jni.srcDirs=[]
            jniLibs.srcDir "src/main/libs"
        }
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile files('libs/android-support-v4.jar')
}

3). 进入androidffmpegtutorial01->src->jni->Android.mk文件,
添加库 libswresample,
即将下面这行:
LOCAL_SHARED_LIBRARIES := libavformat libavcodec libswcale libavutil
改成:
LOCAL_SHARED_LIBRARIES := libavformat libavcodec libswcale libavutil libswresample

对下面的行:
$(call import-module, ffmpeg-2.4.3/android/arm)
改成自己的ffmpeg版本,我的是ffmpeg-2.8.1
$(call import-module, ffmpeg-2.8.1/android/arm)


4). 采用外部使用ndk-build,
从终端进入项目对应的目录:
$cd  androidffmpegtutorial01/src/main
$ ndk-build
[armeabi] Compile thumb  : tutorial01 <= tutorial01.c
jni/tutorial01.c: In function 'naMain':
jni/tutorial01.c:130:2: warning: 'avcodec_alloc_frame' is deprecated 
(declared at /opt/AndroidStudio/android-ndk-r10e/sources/ffmpeg-2.8.1/android/arm/include/libavcodec/avcodec.h:3927) 
[-Wdeprecated-declarations]
  pFrame=avcodec_alloc_frame();
  ^
jni/tutorial01.c:133:2: warning: 'avcodec_alloc_frame' is deprecated 
(declared at /opt/AndroidStudio/android-ndk-r10e/sources/ffmpeg-2.8.1/android/arm/include/libavcodec/avcodec.h:3927) 
[-Wdeprecated-declarations]
  pFrameRGBA=avcodec_alloc_frame();
  ^
[armeabi] Prebuilt       : libavformat-56.so <= /sources/ffmpeg-2.8.1/android/arm/lib/
[armeabi] Prebuilt       : libavcodec-56.so <= /sources/ffmpeg-2.8.1/android/arm/lib/
[armeabi] Prebuilt       : libswscale-3.so <= /sources/ffmpeg-2.8.1/android/arm/lib/
[armeabi] Prebuilt       : libavutil-54.so <= /sources/ffmpeg-2.8.1/android/arm/lib/
[armeabi] Prebuilt       : libswresample-1.so <= /sources/ffmpeg-2.8.1/android/arm/lib/
[armeabi] SharedLibrary  : libtutorial01.so
[armeabi] Install        : libtutorial01.so => libs/armeabi/libtutorial01.so
[armeabi] Install        : libavformat-56.so => libs/armeabi/libavformat-56.so
[armeabi] Install        : libavcodec-56.so => libs/armeabi/libavcodec-56.so
[armeabi] Install        : libswscale-3.so => libs/armeabi/libswscale-3.so
[armeabi] Install        : libavutil-54.so => libs/armeabi/libavutil-54.so
[armeabi] Install        : libswresample-1.so => libs/armeabi/libswresample-1.so


5). 编译、运行项目并测试
先 Tools->Android->Sync Project with Gradle Files,
再 Build->Clean Project
再 Build->Rebuild Project
最后:
Run->Run'androidffmpegtutorial01'
即可看到运行的效果;

编译报错:
FAILURE: Build failed with an exception.


What went wrong: Task '' not found in root project 'MyProject'.
Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. 
Run with --info or --debug option to get more log output.
BUILD FAILED

解决办法:
先 Tools->Android->Sync Project with Gradle Files,

具体原因:


To answer fast, you may have multiple Gradle plugins in your project.
Synchronize Gradle Wrapper and Plugins
My issue seemed to start with a corrupted IML file. Android Studio (between closing and reopening a project) 
began complaining an IML was gone (it wasn’t) and a module should be deleted, which I declined. It persisted, 
I upgraded to AS 0.8.7 (canary channel) and got stuck on the OP issue (Task '' not found in root project).
This completely blocked builds so I had to dig in to Gradle.

My repair steps on OSX (please adjust for Windows):

i). Upgrade Android Studio to 0.8.7
 . Preferences | Updates | Switch "Beta Channel" to "Canary Channel", then do a Check Now.
 . You might be able to skip this.

ii). Checked the Gradle wrapper (currently 1.12.2; don’t try to use 2.0 at this time).
 .  Assuming you don’t need a particular version, use the latest supported distribution
    $ vi ~/project/gradle-wrapper.properties
    ...
   distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip

 .  This can be set in Android Studio at Preferences | Gradle (but 0.8.7 was giving me ‘invalid location’ errors).

 .  The 'wrapper' is just a copy of Gradle for each Android Studio project.
    It allows you to have Gradle 2 in your OS, and different versions in your projects. 
    The Android Developer docs explain that here.

 .  Then adjust your build.gradle files for the plugin. 
    The Gradle plugin version must be compatible with the distribution/wrapper version, for the whole project.
    As the Tools documentation (tools.android.com/tech-docs/new-build-system/user-guide#TOC-Requirements) 
    is slightly out of date, you can set the plugin version too low (like 0.8.0) 
    and Android Studio will throw an error with the acceptable range for the wrapper.

Example, in build.gradle, you have this plugin:
dependencies {
    classpath "com.android.tools.build:gradle:0.12.+"
}

You can try switching it to the exact version, like this:
dependencies {
    classpath "com.android.tools.build:gradle:0.12.2"
}
and (after recording what version you’re changing from in each case) 
verifying that every build.gradle file in your project pulls 
in the same plugin version. Keeping the “+” should work (for 0.12.0, 0.12.1, 0.12.2, etc), 
but my build succeeded when I updated Google’s Volley library (originally gradle:0.8.+) 
and my main project (originally 0.12.+) to the fixed version: gradle:0.12.2.

Other checks
i). Ensure you don’t have two Android Application modules in the same Project
 .  This may interact with the final solution (different Gradle versions, above), and cause
    UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define (various classes)


 .  To check, Build | Make Project should not pop up a window asking what application you want to make.


ii). Invalidate your caches
 .  File | Invalidate Caches / Restart (stackoverflow.com/a/19223269/513413)


iii). If step 2 doesn't work, delete ~/.gradle/ ()
 .  Quit Android Studio
 .  $ rm -rf ~/.gradle/
 .  Start Android Studio, then sync:
    Tools | Android | Sync Project with Gradle Files
 .  Repeat this entire sequence (quit...sync) a few times before giving up.
 .  Clean the project
Build | Clean Project


If You See This...
In my recent builds, I kept seeing horrible fails (pages of exceptions) 
but within seconds the messages would clear, build succeeded and the app deployed. 
Since I could never explain it and the app worked, I never noticed that 
I had two Gradle plugins in my project. So I think the Gradle plugins fought each other; one crashed, 
the other lost its state and reported the error.


If you have time, the 1-hour video "A Gentle Introduction to Gradle" () 
really helped me approach the Gradle build files, tasks, build decisions, etc.


Disclaimer
I'm learning this entire stack, on a foreign OS, after working a different career...
all at the same time and under pressure. In the last few months I have hit every wall I think Android has;
I've been here quite often and this is my first post. I thought this was a hard fix, 
so I sincerely apologize if the quality of my answer reflects the difficulty I had in getting to it.

阅读(4476) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~