4 个答案:
答案 0 :(得分:63)
最后我不需要使用产品口味。
对于图书馆项目,我添加了以下内容:
android {
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
libs文件夹里面有一个名为“armeabi-v7a”的文件夹,因为这是我唯一的目标,所以它有效。
将ndk文件(.so)传播到使用android库项目的android项目中。
答案 1 :(得分:11)
要求:
Android Studio 1.5 +
gradle这个-2.10
gradle这个实验性:0.6.0-素α5
1)默认情况下,您可以将所有共享本机库放在main / jniLibs文件夹中。
项目结构
根文件夹/ android项目
根文件夹/ android_project / app / src / main / jniLibs / x86
根文件夹/ android_project / app / src / main / jniLibs / armeabi-v7a
根文件夹/ android_project / app / src / main / jniLibs / ...
Gradle会自动将它们上传到设备。
然后您可以在应用程序中加载库。
static {
System.loadLibrary("mylibrary");
}
2)您还可以将所有共享本机库放在自定义位置。
带有bin / android / Debug目录路径的示例。
在这种情况下,您应该在build.gradle文件中手动设置库位置。
项目结构
根文件夹/ android项目
根文件夹/ bin / android / Debug / jniLibs / x86
root文件夹/ bin / android / Debug / jniLibs / armeabi-v7a
root folder / bin / android / Debug / jniLibs / ...
根文件夹/ android_project / app / build.gradle
apply plugin: 'com.android.model.application'
model {
android {
sources {
main {
jni {
source {
srcDirs = []
}
}
jniLibs {
source {
srcDirs "/../../bin/android/Debug/jniLibs"
}
}
}
}
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
我正在使用Android Studio 2.1,我发现在我的build.gradle中添加sources或sourceSets条目没有明显的效果。相反,我发现我需要以下内容:
public class Example {
public static void main(String[] args) {
ItemStore store;
Monitor monitor;
Mouse mouse;
store = new ItemStore();
monitor = new Monitor();
monitor.id = 2;
monitor.price = 6;
mouse = new Mouse();
mouse.id = 7;
mouse.buttons = 3;
store.addItem(monitor);
store.addItem(mouse);
System.out.println(store.getItem(2).price); // = 6
System.out.println(((Monitor) store.getItem(2)).dpi);
System.out.println(((Mouse) store.getItem(7)).buttons); //Downcasting ... = 3
}
public static class Item {
String id;
String description;
int price;
// common attributes here!
}
public static class Monitor extends Item {
private int dpi;
// monitor particular atributes here!!
}
public static class Mouse extends Item {
private int buttons;
// mouse particular attributes here!!!
}
public static class ItemStore {
private Hashtable table = new HashTable<>();
public boolean addItem(Item item) {
this.table.put(item.getId(), item);
}
public Item getItem(String id) {
return this.table.get(id);
}
}
}