I need to create an Android app that uses the OpenCV library, and I would like to do this using the command line (linux) only. Before I start I wanted to get a feel for creating projects that use libraries, but I am having trouble referencing a sample library I created. I can build the default Android HelloWorld project and run it on my device witout a problem, I just need help with the library.
I created a root directory and folders for the app and library:
mkdir TestProject
mkdir TestProject/TestApp
mkdir TestProject/TestLib
Then I created the project and library:
android create project --target 1 --gradle --gradle-version 2.4 --name TestApp --path ~/Projects/TestProject/TestApp/ --activity MyActivity --package com.example.testapp
android create lib-project --target 1 --gradle --gradle-version 2.4 --name TestLib --path ~/Projects/TestProject/TestLib --package com.example.testlib
I added 'settings.gradle' to the root directory, and in 'settings.gradle' I have include 'TestApp', 'TestLib'
I wasn't sure if I needed a build.gradle file here or not. I also added the 'TestLib' dependency to the 'TestApp/build.gradle' file:
dependencies {
compile project(':TestLib')
}
If I try using ./TestApp/gradlew assembleDebug
and adb install /TestApp/build/output/apk/TestApp-debug.apk
my project builds and I can run the app on my phone (although there are two activities, one of which doesn't work), but I don't think this setup is correct.
If I try to import the library in MyActivity.java for the TestApp...
package com.example.testapp;
import android.app.Activity;
import android.os.Bundle;
import com.example.TestLib;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
I get this error:
/TestProject/TestApp/src/main/java/com/example/testapp/
MyActivity.java:5: error: cannot find symbol
import com.example.TestLib;
^
symbol: class TestLib
location: package com.example
1 error
:TestApp:compileDebugJava FAILED
I'm guessing I didn't set up something that is required to recognize the library. Or maybe I didn't create the library correctly. I tried following the Google Android tutorials for adding a library from command line, but it looks like they use Ant instead of Gradle. I might try doing that instead, but I heard Gradle was supposed to make my life easier somehow..
Edit:
I have tried using Ant but I can't even get the project to build. I receive the following error after running ant debug install
in my project directory:
BUILD FAILED
/home/steven/Libraries/android-sdk-linux/tools/ant/build.xml:649: The
following error occurred while executing this line:
/home/steven/Libraries/android-sdk-linux/tools/ant/build.xml:694:
Execute failed: java.io.IOException: Cannot run program
"/home/steven/Projects/WorkPlz/${aapt}": error=2, No such file or
directory
I was finally able to create a library and use it in my Android project! The following links were helpful:
https://docs.gradle.org/current/userguide/multi_project_builds.html
This whole process made me understand why most people just use Ecipse or Adnroid Studio. But if anyone else wants to try this, I will include a link to my sample project. It should clarify how everything works.
Sample project: https://drive.google.com/folderview?id=0B2nWL2e8v6g-fnVuaEJHMWYyeGd5RXBIT0M5bjc5M3hPaHdnalJpNXVhVXRQVkVHZ2JtOEU&usp=sharing
Now I can finally get to work!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.